working output

main
Brett 2024-10-05 18:17:47 -04:00
parent 9c4314e4d7
commit 8921671966
5 changed files with 291 additions and 172 deletions

View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="CMakePresetLoader">{
&quot;useNewFormat&quot;: true
}</component>
<component name="CMakeReloadState">
<option name="reloaded" value="true" />
</component>
<component name="CMakeRunConfigurationManager">
<generated>
<config projectName="COSC-4P80-Assignment-1" targetName="BLT" />
<config projectName="COSC-4P80-Assignment-1" targetName="COSC-4P80-Assignment-1" />
</generated>
</component>
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" ENABLED="true" CONFIG_NAME="Debug" />
<configuration PROFILE_NAME="Release" ENABLED="true" CONFIG_NAME="Release" />
<configuration PROFILE_NAME="RelWithDebInfo" ENABLED="true" CONFIG_NAME="RelWithDebInfo" />
<configuration PROFILE_NAME="RelWithDebInfo Addrsan" ENABLED="true" CONFIG_NAME="RelWithDebInfo" GENERATION_OPTIONS="-G Ninja -DENABLE_ADDRSAN=ON" />
<configuration PROFILE_NAME="RelWithDebInfo Clang" ENABLED="true" CONFIG_NAME="RelWithDebInfo" TOOLCHAIN_NAME="Clang" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="c26429cb-3f2d-4b3c-a3dd-031e70b9aeae" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ClangdSettings">
<option name="clangTidyViaClangd" value="false" />
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectApplicationVersion">
<option name="ide" value="CLion" />
<option name="majorVersion" value="2024" />
<option name="minorVersion" value="1.3" />
<option name="productBranch" value="Classic" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 5
}</component>
<component name="ProjectId" id="2mH5CM5ppMWsphz4G4xP4A28USH" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;CMake Application.COSC-4P80-Assignment-1.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.cidr.known.project.marker&quot;: &quot;true&quot;,
&quot;RunOnceActivity.readMode.enableVisualFormatting&quot;: &quot;true&quot;,
&quot;cf.first.check.clang-format&quot;: &quot;false&quot;,
&quot;cidr.known.project.marker&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;main&quot;,
&quot;last_opened_file_path&quot;: &quot;/home/brett/Documents/code/c++/BLT_dev/BLT&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;CPPToolchains&quot;,
&quot;structure.view.defaults.are.configured&quot;: &quot;true&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}</component>
<component name="RunManager" selected="CMake Application.COSC-4P80-Assignment-1">
<configuration name="BLT" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="COSC-4P80-Assignment-1" TARGET_NAME="BLT" CONFIG_NAME="Debug">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="COSC-4P80-Assignment-1" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="COSC-4P80-Assignment-1" TARGET_NAME="COSC-4P80-Assignment-1" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="COSC-4P80-Assignment-1" RUN_TARGET_NAME="COSC-4P80-Assignment-1">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="CMake Application.BLT" />
<item itemvalue="CMake Application.COSC-4P80-Assignment-1" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="c26429cb-3f2d-4b3c-a3dd-031e70b9aeae" name="Changes" comment="" />
<created>1726720532743</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1726720532743</updated>
<workItem from="1726720533749" duration="20189000" />
<workItem from="1727116071317" duration="39656000" />
<workItem from="1727648722710" duration="1807000" />
<workItem from="1727806367161" duration="2606000" />
<workItem from="1727823589121" duration="4802000" />
<workItem from="1728063301012" duration="9392000" />
<workItem from="1728077207859" duration="2250000" />
<workItem from="1728152682974" duration="2000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VCPKGProject">
<isAutomaticCheckingOnLaunch value="false" />
<isAutomaticFoundErrors value="true" />
<isAutomaticReloadCMake value="true" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />
</component>
</project>

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(COSC-4P80-Assignment-1 VERSION 0.0.19) project(COSC-4P80-Assignment-1 VERSION 0.0.20)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -53,55 +53,40 @@ namespace a1
BLT_ASSERT(g1 * g2.transpose() == blt::vec4::dot(one, two) && "MATH DOT FAILURE"); BLT_ASSERT(g1 * g2.transpose() == blt::vec4::dot(one, two) && "MATH DOT FAILURE");
} }
template<typename input_t> template<typename T, blt::u32 size>
float crosstalk(const input_t& i, const input_t& j) struct vec_formatter
{ {
return i * j.transpose(); public:
} explicit vec_formatter(const blt::vec<T, size>& data): data(data)
{}
template<typename T> template<typename Arr>
blt::size_t difference(const std::vector<T>& a, const std::vector<T>& b) std::string format(const Arr& has_index_changed)
{ {
blt::size_t count = 0; using namespace blt::logging;
for (const auto& [a_val, b_val] : blt::in_pairs(a, b)) std::string os;
{ for (auto [index, value] : blt::enumerate(data))
if (a_val != b_val) {
count++; if (value >= 0)
} os += ' ';
return count; if (has_index_changed[index])
} os += ansi::make_color(ansi::UNDERLINE);
os += blt::logging::to_string_stream(value);
if (has_index_changed[index])
os += ansi::make_color(ansi::RESET_UNDERLINE);
template<typename T> if (index != size - 1)
bool equal(const std::vector<T>& a, const std::vector<T>& b) os += ", ";
{ }
return difference(a, b) == 0; return os;
} }
// template<typename weight_t, typename input_t, typename output_t> private:
// std::pair<input_t, output_t> run_step(const weight_t& associated_weights, const input_t& input, const output_t& output) blt::vec<T, size> data;
// { };
// output_t output_recall = input * associated_weights;
// input_t input_recall = output_recall * associated_weights.transpose();
//
// return std::pair{a1::threshold(input_recall, input), a1::threshold(output_recall, output)};
// }
template<typename weight_t, typename T, typename G> template<typename T, blt::u32 size>
void check_recall(const weight_t& weights, const std::vector<G>& inputs, const std::vector<T>& outputs) vec_formatter(const blt::vec<T, size>& data) -> vec_formatter<T, size>;
{
for (const auto& [index, val] : blt::enumerate(inputs))
{
auto result = run_step(weights, val, outputs[index]);
if (result.first != val)
BLT_ERROR("Recall of input #%ld failed", index + 1);
else
BLT_INFO("Recall of input #%ld passed", index + 1);
if (result.second != outputs[index])
BLT_ERROR("Recall of output #%ld failed", index + 1);
else
BLT_INFO("recall of output #%ld passed", index + 1);
}
}
} }
#endif //COSC_4P80_ASSIGNMENT_1_A1_H #endif //COSC_4P80_ASSIGNMENT_1_A1_H

@ -1 +1 @@
Subproject commit ffeab29e5fb4a3e18e7dd8246d9fac4c24efdf42 Subproject commit 3003e424e17a5c639a8b89d5c721c94c47f3402a

View File

@ -29,8 +29,8 @@ class ping_pong
[[nodiscard]] ping_pong run_step() const [[nodiscard]] ping_pong run_step() const
{ {
auto out = input * weights; auto out = (input * weights);
return {weights, threshold(out * weights.transpose()), threshold(out)}; return {weights, (out * weights.transpose()).bipolar(), out.bipolar()};
} }
[[nodiscard]] ping_pong pong() const [[nodiscard]] ping_pong pong() const
@ -38,12 +38,12 @@ class ping_pong
return run_step(); return run_step();
} }
input_t get_input() [[nodiscard]] const input_t& get_input() const
{ {
return input; return input;
} }
output_t get_output() [[nodiscard]] const output_t& get_output() const
{ {
return output; return output;
} }
@ -58,18 +58,6 @@ class ping_pong
return a.input != b.input || a.output != b.output; return a.input != b.input || a.output != b.output;
} }
template<blt::u32 rows, blt::u32 columns>
static a1::matrix_t<rows, columns> threshold(const a1::matrix_t<rows, columns>& y)
{
a1::matrix_t<rows, columns> result;
for (blt::u32 i = 0; i < columns; i++)
{
for (blt::u32 j = 0; j < rows; j++)
result[i][j] = y[i][j] >= 0 ? 1 : -1;
}
return result;
}
private: private:
weight_t weights; weight_t weights;
input_t input; input_t input;
@ -79,9 +67,33 @@ class ping_pong
class executor class executor
{ {
public: public:
executor(weight_t weights, std::vector<input_t> inputs, std::vector<output_t> outputs): executor(const std::vector<input_t>& inputs, const std::vector<output_t>& outputs): weights(), inputs(inputs), outputs(outputs)
weights(std::move(weights)), inputs(std::move(inputs)), outputs(std::move(outputs)) {
{} for (auto [in, out] : blt::in_pairs(inputs, outputs))
weights += in.transpose() * out;
}
std::vector<output_t> crosstalk()
{
std::vector<output_t> crosstalk_data;
crosstalk_data.resize(outputs.size());
for (auto [i, c] : blt::enumerate(crosstalk_data))
{
for (auto [k, data] : blt::in_pairs(inputs, outputs).enumerate())
{
if (i == k)
continue;
auto [a, b] = data;
c += (b * (a.normalize() * inputs[i].normalize().transpose()));
}
BLT_TRACE("Input %ld has crosstalk magnitude: %f || %f", i, c.magnitude());
}
return crosstalk_data;
}
void execute() void execute()
{ {
@ -102,91 +114,109 @@ class executor
} while (steps.rbegin()[0] != steps.rbegin()[1]); } while (steps.rbegin()[0] != steps.rbegin()[1]);
} }
void print_chains() void print()
{ {
using namespace blt::logging; using namespace blt::logging;
std::vector<std::string> input; std::vector<std::string> input_lines;
input.reserve(inputs.size()); std::vector<std::string> output_lines;
input.resize(inputs.size()); input_lines.resize(inputs.size());
std::vector<std::string> output; output_lines.resize(outputs.size());
output.reserve(outputs.size()); BLT_ASSERT(input_lines.size() == output_lines.size());
output.resize(outputs.size());
for (auto [index, ref_pair] : blt::in_pairs(input, output).enumerate()) for (auto [i, data] : blt::in_pairs(input_lines, output_lines).enumerate())
{ {
auto& [i, o] = ref_pair; auto& [is, os] = data;
(((i += "[Input ") += std::to_string(index)) += "]: ") += ansi::RESET;
(((o += "[Output ") += std::to_string(index)) += "]: ") += ansi::RESET; auto ping_ping = steps.back()[i];
auto& current_line = steps[index];
for (auto [pos, ping] : blt::enumerate(current_line)) auto& input_data = inputs[i];
auto& output_data = outputs[i];
((((is += input_data == ping_ping.get_input() ? ansi::make_color(ansi::GREEN) : ansi::make_color(
ansi::RED)) += "[Input ") += std::to_string(i)) += "]: ") += ansi::RESET;
((((os += output_data == ping_ping.get_output() ? ansi::make_color(ansi::GREEN) : ansi::make_color(
ansi::RED)) += "[Output ") += std::to_string(i)) += "]: ") += ansi::RESET;
}
for (auto [step_index, step_data] : blt::enumerate(steps))
{
for (auto [data_index, current_data] : blt::enumerate(step_data))
{ {
auto input_vec = ping.get_input().vec_from_column_row(); auto current_input = current_data.get_input().vec_from_column_row();
auto output_vec = ping.get_output().vec_from_column_row(); auto current_output = current_data.get_output().vec_from_column_row();
auto input_size = input_vec.end() - input_vec.begin();
auto output_size = output_vec.end() - output_vec.begin(); std::array<bool, decltype(current_input)::data_size> has_input_changed{};
if (pos > 0) std::array<bool, decltype(current_output)::data_size> has_output_changed{};
if (step_index > 0)
{ {
((i += "Vec") += blt::logging::to_string_stream(input_size)) += "("; auto& previous_data = steps[step_index - 1][data_index];
((o += "Vec") += blt::logging::to_string_stream(output_size)) += "("; auto previous_input = previous_data.get_input().vec_from_column_row();
auto prev_input_vec = current_line[pos - 1].get_input().vec_from_column_row(); auto previous_output = previous_data.get_output().vec_from_column_row();
auto prev_output_vec = current_line[pos - 1].get_output().vec_from_column_row();
for (auto [j, prev_value_t] : blt::zip(input_vec, prev_input_vec).enumerate())
{
auto [cur_input, prev_input] = prev_value_t;
if (cur_input > 0) for (auto [vec_index, data] : blt::zip(current_input, previous_input).enumerate())
i += ' '; has_input_changed[vec_index] = std::get<0>(data) != std::get<1>(data);
for (auto [vec_index, data] : blt::zip(current_output, previous_output).enumerate())
if (cur_input != prev_input) has_output_changed[vec_index] = std::get<0>(data) != std::get<1>(data);
i += ansi::make_color(ansi::UNDERLINE);
i += blt::logging::to_string_stream(cur_input);
if (cur_input != prev_input)
i += ansi::make_color(ansi::RESET_UNDERLINE);
if (static_cast<blt::ptrdiff_t>(j) != input_size - 1)
i += ", ";
}
for (auto [j, prev_value_t] : blt::zip(output_vec, prev_output_vec).enumerate())
{
auto [cur_output, prev_output] = prev_value_t;
if (cur_output > 0)
o += ' ';
if (cur_output != prev_output)
o += ansi::make_color(ansi::UNDERLINE);
o += blt::logging::to_string_stream(cur_output);
if (cur_output != prev_output)
o += ansi::make_color(ansi::RESET_UNDERLINE);
if (static_cast<blt::ptrdiff_t>(j) != output_size - 1)
o += ", ";
}
i += ")";
o += ")";
} else
{
i += blt::logging::to_string_stream(input_vec);
o += blt::logging::to_string_stream(output_vec);
} }
auto diff_o = (static_cast<blt::ptrdiff_t>(input_size) - static_cast<blt::ptrdiff_t>(output_size)) * 4; auto& is = input_lines[data_index];
auto diff_i = (static_cast<blt::ptrdiff_t>(output_size) - static_cast<blt::ptrdiff_t>(input_size)) * 4; auto& os = output_lines[data_index];
for (blt::ptrdiff_t j = 0; j < diff_i; j++)
i += ' ';
for (blt::ptrdiff_t j = 0; j < diff_o; j++)
o += ' ';
if (pos != current_line.size() - 1) ((is += "Vec") += blt::logging::to_string_stream(decltype(current_input)::data_size)) += "(";
((os += "Vec") += blt::logging::to_string_stream(decltype(current_output)::data_size)) += "(";
is += a1::vec_formatter(current_input).format(has_input_changed);
os += a1::vec_formatter(current_output).format(has_output_changed);
is += ")";
os += ")";
auto diff_o = (static_cast<blt::i32>(decltype(current_input)::data_size)
- static_cast<blt::i32>(decltype(current_output)::data_size)) * 4;
auto diff_i = (static_cast<blt::i32>(decltype(current_output)::data_size)
- static_cast<blt::i32>(decltype(current_input)::data_size)) * 4;
for (blt::i32 j = 0; j < diff_i; j++)
is += ' ';
for (blt::i32 j = 0; j < diff_o; j++)
os += ' ';
if (step_index != steps.size() - 1)
{ {
i += " => "; is += " => ";
o += " => "; os += " => ";
} else
{
is += " || ";
os += " || ";
} }
} }
} }
for (auto [i, o] : blt::in_pairs(input, output)) for (auto [index, ping_ping] : blt::enumerate(steps.back()))
{ {
BLT_TRACE_STREAM << i << "\n"; auto& is = input_lines[index];
BLT_TRACE_STREAM << o << "\n"; auto& os = output_lines[index];
auto& input_data = inputs[index];
auto& output_data = outputs[index];
if (input_data != ping_ping.get_input())
((is += ansi::make_color(ansi::RED)) += "[Failed]") += ansi::RESET;
else
((is += ansi::make_color(ansi::GREEN)) += "[Passed]") += ansi::RESET;
if (output_data != ping_ping.get_output())
((os += ansi::make_color(ansi::RED)) += "[Failed]") += ansi::RESET;
else
((os += ansi::make_color(ansi::GREEN)) += "[Passed]") += ansi::RESET;
}
BLT_TRACE("Changes between ping-pong steps are underlined.");
for (auto [is, os] : blt::in_pairs(input_lines, output_lines))
{
BLT_TRACE_STREAM << is << "\n";
BLT_TRACE_STREAM << os << "\n";
} }
} }
@ -219,14 +249,6 @@ output_t output_5{1, 1, 1, 1};
output_t output_6{1, -1, -1, 1}; output_t output_6{1, -1, -1, 1};
output_t output_7{1, 1, 1, -1}; output_t output_7{1, 1, 1, -1};
const weight_t weight_1 = input_1.transpose() * output_1;
const weight_t weight_2 = input_2.transpose() * output_2;
const weight_t weight_3 = input_3.transpose() * output_3;
const weight_t weight_4 = input_4.transpose() * output_4;
const weight_t weight_5 = input_5.transpose() * output_5;
const weight_t weight_6 = input_6.transpose() * output_6;
const weight_t weight_7 = input_7.transpose() * output_7;
auto part_a_inputs = std::vector{input_1, input_2, input_3}; auto part_a_inputs = std::vector{input_1, input_2, input_3};
auto part_a_outputs = std::vector{output_1, output_2, output_3}; auto part_a_outputs = std::vector{output_1, output_2, output_3};
@ -236,47 +258,32 @@ auto part_c_1_outputs = std::vector{output_1, output_2, output_3, output_4};
auto part_c_2_inputs = std::vector{input_1, input_2, input_3, input_4, input_5, input_6, input_7}; auto part_c_2_inputs = std::vector{input_1, input_2, input_3, input_4, input_5, input_6, input_7};
auto part_c_2_outputs = std::vector{output_1, output_2, output_3, output_4, output_5, output_6, output_7}; auto part_c_2_outputs = std::vector{output_1, output_2, output_3, output_4, output_5, output_6, output_7};
const auto weight_total_a = weight_1 + weight_2 + weight_3;
const auto weight_total_c = weight_total_a + weight_4;
const auto weight_total_c_2 = weight_total_c + weight_5 + weight_6 + weight_7;
crosstalk_t crosstalk_values[num_values_part_a];
void part_a() void part_a()
{ {
blt::log_box_t box(BLT_TRACE_STREAM, "Part A", 8); blt::log_box_t box(BLT_TRACE_STREAM, "Part A", 8);
executor cute(weight_total_a, part_a_inputs, part_a_outputs); executor cute(part_a_inputs, part_a_outputs);
cute.execute(); cute.execute();
cute.print_chains(); cute.print();
} }
void part_b() void part_b()
{ {
blt::log_box_t box(BLT_TRACE_STREAM, "Part B", 8); blt::log_box_t box(BLT_TRACE_STREAM, "Part B", 8);
for (blt::u32 i = 0; i < num_values_part_a; i++) executor cute(part_a_inputs, part_a_outputs);
{ cute.crosstalk();
crosstalk_values[i] = {};
for (blt::u32 k = 0; k < num_values_part_a; k++)
{
if (i == k)
continue;
crosstalk_values[i] += (part_a_outputs[i] * a1::crosstalk(part_a_inputs[i].normalize(), part_a_inputs[k].normalize())).abs();
}
}
for (const auto& crosstalk_value : crosstalk_values)
{
auto vec = crosstalk_value.vec_from_column_row();
BLT_DEBUG_STREAM << vec << " Mag: " << vec.magnitude() << "\n";
}
} }
void part_c() void part_c()
{ {
blt::log_box_t box(BLT_TRACE_STREAM, "Part C", 8); blt::log_box_t box(BLT_TRACE_STREAM, "Part C", 8);
// execute_BAM(weight_total_c, part_c_1_inputs, part_c_1_outputs); executor cute(part_c_1_inputs, part_c_1_outputs);
cute.execute();
cute.print();
BLT_TRACE("--- { Part C with 3 extra pairs } ---"); BLT_TRACE("--- { Part C with 3 extra pairs } ---");
// execute_BAM(weight_total_c_2, part_c_2_inputs, part_c_2_outputs); executor cute2(part_c_2_inputs, part_c_2_outputs);
cute2.execute();
cute2.print();
} }
int main() int main()
@ -287,4 +294,8 @@ int main()
part_a(); part_a();
part_b(); part_b();
part_c(); part_c();
std::vector<input_t> test{input_t{1, -1, -1, -1, -1}, input_t{-1, 1, -1, -1, -1}, input_t{-1, -1, 1, -1, -1}};
executor cute{test, part_a_outputs};
cute.crosstalk();
} }