main
Brett 2024-10-07 02:06:15 -04:00
parent 8921671966
commit e3ef31ffc2
6 changed files with 202 additions and 136 deletions

View File

@ -1,119 +0,0 @@
<?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;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="1727974969818" 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

@ -25,7 +25,9 @@
</configurations> </configurations>
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="c26429cb-3f2d-4b3c-a3dd-031e70b9aeae" name="Changes" comment="" /> <list default="true" id="c26429cb-3f2d-4b3c-a3dd-031e70b9aeae" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/src/main.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main.cpp" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -74,6 +76,11 @@
} }
}</component> }</component>
<component name="RunManager" selected="CMake Application.COSC-4P80-Assignment-1"> <component name="RunManager" selected="CMake Application.COSC-4P80-Assignment-1">
<configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method>
</configuration>
<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"> <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"> <method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
@ -85,8 +92,8 @@
</method> </method>
</configuration> </configuration>
<list> <list>
<item itemvalue="CMake Application.BLT" />
<item itemvalue="CMake Application.COSC-4P80-Assignment-1" /> <item itemvalue="CMake Application.COSC-4P80-Assignment-1" />
<item itemvalue="CMake Application.BLT" />
</list> </list>
</component> </component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
@ -104,7 +111,7 @@
<workItem from="1727823589121" duration="4802000" /> <workItem from="1727823589121" duration="4802000" />
<workItem from="1728063301012" duration="9392000" /> <workItem from="1728063301012" duration="9392000" />
<workItem from="1728077207859" duration="2250000" /> <workItem from="1728077207859" duration="2250000" />
<workItem from="1728152682974" duration="2000" /> <workItem from="1728089514014" duration="3711000" />
</task> </task>
<servers /> <servers />
</component> </component>

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.20) project(COSC-4P80-Assignment-1 VERSION 0.0.21)
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)

0
build.sh Normal file → Executable file
View File

0
run.sh Normal file → Executable file
View File

View File

@ -2,7 +2,8 @@
#include <utility> #include <utility>
#include <blt/math/matrix.h> #include <blt/math/matrix.h>
#include <blt/math/log_util.h> #include <blt/math/log_util.h>
#include "blt/std/assert.h" #include <blt/std/assert.h>
#include <blt/std/random.h>
#include <blt/format/boxing.h> #include <blt/format/boxing.h>
#include <blt/iterator/iterator.h> #include <blt/iterator/iterator.h>
#include <a1.h> #include <a1.h>
@ -16,13 +17,20 @@ constexpr blt::u32 output_vec_size = 4;
using input_t = a1::matrix_t<1, input_vec_size>; using input_t = a1::matrix_t<1, input_vec_size>;
using output_t = a1::matrix_t<1, output_vec_size>; using output_t = a1::matrix_t<1, output_vec_size>;
using weight_t = decltype(std::declval<input_t>().transpose() * std::declval<output_t>()); using weight_t = decltype(std::declval<input_t>().transpose() * std::declval<output_t>());
using crosstalk_t = a1::matrix_t<1, output_vec_size>;
struct correctness_t
{
blt::size_t correct_input = 0;
blt::size_t correct_output = 0;
blt::size_t incorrect_input = 0;
blt::size_t incorrect_output = 0;
};
class ping_pong class ping_pong
{ {
public: public:
ping_pong(weight_t weights, input_t input): weights(std::move(weights)), input(std::move(input)) // ping_pong(weight_t weights, input_t input): weights(std::move(weights)), input(std::move(input))
{} // {}
ping_pong(weight_t weights, input_t input, output_t output): weights(std::move(weights)), input(std::move(input)), output(std::move(output)) ping_pong(weight_t weights, input_t input, output_t output): weights(std::move(weights)), input(std::move(input)), output(std::move(output))
{} {}
@ -31,6 +39,7 @@ class ping_pong
{ {
auto out = (input * weights); auto out = (input * weights);
return {weights, (out * weights.transpose()).bipolar(), out.bipolar()}; return {weights, (out * weights.transpose()).bipolar(), out.bipolar()};
// return {weights, (output * weights.transpose()).bipolar(), input * weights};
} }
[[nodiscard]] ping_pong pong() const [[nodiscard]] ping_pong pong() const
@ -68,12 +77,59 @@ class executor
{ {
public: public:
executor(const std::vector<input_t>& inputs, const std::vector<output_t>& outputs): weights(), inputs(inputs), outputs(outputs) executor(const std::vector<input_t>& inputs, const std::vector<output_t>& outputs): weights(), inputs(inputs), outputs(outputs)
{
generate_weights();
}
void add_pattern(input_t input, output_t output)
{
inputs.push_back(std::move(input));
outputs.push_back(std::move(output));
}
void generate_weights()
{ {
for (auto [in, out] : blt::in_pairs(inputs, outputs)) for (auto [in, out] : blt::in_pairs(inputs, outputs))
weights += in.transpose() * out; weights += in.transpose() * out;
} }
std::vector<output_t> crosstalk() void print_weights() const
{
BLT_TRACE_STREAM << "Weight Matrix: \n" << weights << "\n";
}
void print_crosstalk_table()
{
using namespace blt::logging;
std::vector<std::string> lines;
lines.resize(inputs.size() + 2);
std::vector<output_t> crosstalk_data;
crosstalk_data.resize(outputs.size());
for (auto [k, data] : blt::enumerate(inputs))
(lines[k] += "Input ") += std::to_string(k) += ": | ";
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;
(((lines[k] += "b=") += to_string_stream(b.vec_from_column_row()) += ",a=") += to_string_stream(
a.normalize().vec_from_column_row()) += ",cos=") += to_string_stream(
a.normalize() * inputs[i].normalize().transpose()) += " |";
c += (b * (a.normalize() * inputs[i].normalize().transpose()));
}
}
for (const auto& line : lines)
BLT_TRACE_STREAM << line << "\n";
}
[[nodiscard]] std::vector<output_t> crosstalk() const
{ {
std::vector<output_t> crosstalk_data; std::vector<output_t> crosstalk_data;
crosstalk_data.resize(outputs.size()); crosstalk_data.resize(outputs.size());
@ -88,15 +144,29 @@ class executor
c += (b * (a.normalize() * inputs[i].normalize().transpose())); c += (b * (a.normalize() * inputs[i].normalize().transpose()));
} }
BLT_TRACE("Input %ld has crosstalk magnitude: %f || %f", i, c.magnitude());
} }
return crosstalk_data; return crosstalk_data;
} }
void print_crosstalk() const
{
auto talk = crosstalk();
float total_talk = 0;
for (auto [i, c] : blt::enumerate(talk))
{
BLT_TRACE_STREAM << "Input " << i << " [" << inputs[i].vec_from_column_row() << "] has crosstalk magnitude: " << c.magnitude()
<< "\n";
total_talk += c.magnitude();
}
BLT_TRACE("Total Crosstalk: %f", total_talk);
}
void execute() void execute()
{ {
steps.clear();
std::vector<ping_pong> initial_pings; std::vector<ping_pong> initial_pings;
initial_pings.reserve(inputs.size()); initial_pings.reserve(inputs.size());
for (auto [input, output] : blt::in_pairs(inputs, outputs)) for (auto [input, output] : blt::in_pairs(inputs, outputs))
@ -114,7 +184,55 @@ class executor
} while (steps.rbegin()[0] != steps.rbegin()[1]); } while (steps.rbegin()[0] != steps.rbegin()[1]);
} }
void print() void print_execution_summary()
{
using namespace blt::logging;
for (auto [i, data] : blt::zip(inputs, outputs, steps.back()).enumerate())
{
auto& [input_data, output_data, ping_ping] = data;
BLT_TRACE_STREAM << (input_data == ping_ping.get_input() ? ansi::make_color(ansi::GREEN) : ansi::make_color(ansi::RED))
<< "[Input: " << std::to_string(i) << "]: " << (input_data == ping_ping.get_input() ? "Passed" : "Failed") << "\n";
BLT_TRACE_STREAM << (output_data == ping_ping.get_output() ? ansi::make_color(ansi::GREEN) : ansi::make_color(ansi::RED))
<< "[Output: " << std::to_string(i) << "]: " << (output_data == ping_ping.get_output() ? "Passed" : "Failed")
<< "\n";
}
}
[[nodiscard]] correctness_t correctness() const
{
correctness_t results;
for (auto [i, data] : blt::zip(inputs, outputs, steps.back()).enumerate())
{
auto& [input_data, output_data, ping_ping] = data;
if (input_data == ping_ping.get_input())
results.correct_input++;
else
results.incorrect_input++;
if (output_data == ping_ping.get_output())
results.correct_output++;
else
results.incorrect_output++;
}
return results;
}
void print_correctness() const
{
auto data = correctness();
BLT_TRACE("Correct inputs %ld Incorrect inputs %ld | (%lf%%)", data.correct_input, data.incorrect_input,
static_cast<double>(data.correct_input * 100) / static_cast<double>(data.incorrect_input + data.correct_input));
BLT_TRACE("Correct outputs %ld Incorrect outputs %ld | (%lf%%)", data.correct_output, data.incorrect_output,
static_cast<double>(data.correct_output * 100) / static_cast<double>(data.incorrect_output + data.correct_output));
BLT_TRACE("Total correct %ld Total incorrect %ld | (%lf%%)", data.correct_input + data.correct_output,
data.incorrect_input + data.incorrect_output,
static_cast<double>(data.correct_input + data.correct_output) * 100 /
static_cast<double>(data.correct_input + data.correct_output + data.incorrect_input + data.incorrect_output));
}
void print_execution_results()
{ {
using namespace blt::logging; using namespace blt::logging;
std::vector<std::string> input_lines; std::vector<std::string> input_lines;
@ -219,6 +337,16 @@ class executor
BLT_TRACE_STREAM << os << "\n"; BLT_TRACE_STREAM << os << "\n";
} }
} }
std::vector<ping_pong>& get_results()
{
return steps.back();
}
std::vector<input_t>& get_inputs()
{
return inputs;
}
private: private:
weight_t weights; weight_t weights;
@ -264,14 +392,16 @@ void part_a()
executor cute(part_a_inputs, part_a_outputs); executor cute(part_a_inputs, part_a_outputs);
cute.execute(); cute.execute();
cute.print(); cute.print_execution_results();
cute.print_correctness();
} }
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);
executor cute(part_a_inputs, part_a_outputs); executor cute(part_a_inputs, part_a_outputs);
cute.crosstalk(); cute.print_crosstalk();
// cute.print_crosstalk_table();
} }
void part_c() void part_c()
@ -279,11 +409,58 @@ 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);
executor cute(part_c_1_inputs, part_c_1_outputs); executor cute(part_c_1_inputs, part_c_1_outputs);
cute.execute(); cute.execute();
cute.print(); cute.print_execution_results();
cute.print_correctness();
BLT_TRACE("--- { Part C with 3 extra pairs } ---"); BLT_TRACE("--- { Part C with 3 extra pairs } ---");
executor cute2(part_c_2_inputs, part_c_2_outputs); executor cute2(part_c_2_inputs, part_c_2_outputs);
cute2.execute(); cute2.execute();
cute2.print(); cute2.print_execution_results();
cute2.print_correctness();
}
blt::size_t hdist(const input_t& a, const input_t& b)
{
blt::size_t diff = 0;
for (auto [av, bv] : blt::in_pairs(a.vec_from_column_row(), b.vec_from_column_row()))
diff += (av == bv ? 1 : 0);
return diff;
}
void part_d()
{
blt::log_box_t box(BLT_TRACE_STREAM, "Part D", 8);
blt::random::random_t random(std::random_device{}());
blt::size_t number_of_runs = 20;
for (blt::size_t run = 0; run < number_of_runs; run++)
{
auto inputs = part_a_inputs;
executor cute(part_a_inputs, part_a_outputs);
auto pos = random.get_size_t(0, inputs.size());
auto& input = inputs[pos];
auto original = input;
for (blt::size_t i = 0; i < std::remove_reference_t<decltype(input)>::data_columns; i++)
{
if (random.choice(0.8))
{
// flip value of this location
auto& d = input[i][0];
if (d >= 0)
d = -1;
else
d = 1;
}
}
cute.get_inputs()[pos] = input;
cute.execute();
auto corrected = cute.get_results()[pos].get_input();
auto dist_o_m = hdist(original, input);
auto dist_m_c = hdist(input, corrected);
BLT_TRACE("Run %ld mutated difference: %ld corrected difference: %ld", run, dist_o_m, dist_m_c);
}
} }
int main() int main()
@ -294,8 +471,9 @@ int main()
part_a(); part_a();
part_b(); part_b();
part_c(); part_c();
part_d();
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}}; 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}; executor cute{test, part_a_outputs};
cute.crosstalk(); cute.print_crosstalk();
} }