diff --git a/.idea/workspace (conflicted copy 2024-10-03 130251).xml b/.idea/workspace (conflicted copy 2024-10-03 130251).xml
deleted file mode 100644
index 71433f8..0000000
--- a/.idea/workspace (conflicted copy 2024-10-03 130251).xml
+++ /dev/null
@@ -1,119 +0,0 @@
-
-
-
-
-
- {
- "useNewFormat": true
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- {
- "associatedIndex": 5
-}
-
-
-
-
-
- {
- "keyToString": {
- "CMake Application.COSC-4P80-Assignment-1.executor": "Run",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "RunOnceActivity.cidr.known.project.marker": "true",
- "RunOnceActivity.readMode.enableVisualFormatting": "true",
- "cf.first.check.clang-format": "false",
- "cidr.known.project.marker": "true",
- "git-widget-placeholder": "main",
- "last_opened_file_path": "/home/brett/Documents/code/c++/BLT_dev/BLT",
- "node.js.detected.package.eslint": "true",
- "node.js.detected.package.tslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "node.js.selected.package.tslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "settings.editor.selected.configurable": "CPPToolchains",
- "vue.rearranger.settings.migration": "true"
- }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1726720532743
-
-
- 1726720532743
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace (conflicted copy 2024-10-05 141837).xml b/.idea/workspace (conflicted copy 2024-10-05 144943).xml
similarity index 91%
rename from .idea/workspace (conflicted copy 2024-10-05 141837).xml
rename to .idea/workspace (conflicted copy 2024-10-05 144943).xml
index d0eaa7a..504fc45 100644
--- a/.idea/workspace (conflicted copy 2024-10-05 141837).xml
+++ b/.idea/workspace (conflicted copy 2024-10-05 144943).xml
@@ -25,7 +25,9 @@
-
+
+
+
@@ -74,6 +76,11 @@
}
}
+
+
+
+
+
@@ -85,8 +92,8 @@
-
+
@@ -104,7 +111,7 @@
-
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 01bba3b..08fa53d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
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_UBSAN "Enable the ub sanitizer" OFF)
diff --git a/build.sh b/build.sh
old mode 100644
new mode 100755
diff --git a/run.sh b/run.sh
old mode 100644
new mode 100755
diff --git a/src/main.cpp b/src/main.cpp
index b3cea4e..787b7bf 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,7 +2,8 @@
#include
#include
#include
-#include "blt/std/assert.h"
+#include
+#include
#include
#include
#include
@@ -16,13 +17,20 @@ constexpr blt::u32 output_vec_size = 4;
using input_t = a1::matrix_t<1, input_vec_size>;
using output_t = a1::matrix_t<1, output_vec_size>;
using weight_t = decltype(std::declval().transpose() * std::declval());
-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
{
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))
{}
@@ -31,6 +39,7 @@ class ping_pong
{
auto out = (input * weights);
return {weights, (out * weights.transpose()).bipolar(), out.bipolar()};
+// return {weights, (output * weights.transpose()).bipolar(), input * weights};
}
[[nodiscard]] ping_pong pong() const
@@ -68,12 +77,59 @@ class executor
{
public:
executor(const std::vector& inputs, const std::vector& 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))
weights += in.transpose() * out;
}
- std::vector crosstalk()
+ void print_weights() const
+ {
+ BLT_TRACE_STREAM << "Weight Matrix: \n" << weights << "\n";
+ }
+
+ void print_crosstalk_table()
+ {
+ using namespace blt::logging;
+ std::vector lines;
+ lines.resize(inputs.size() + 2);
+ std::vector 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 crosstalk() const
{
std::vector crosstalk_data;
crosstalk_data.resize(outputs.size());
@@ -88,15 +144,29 @@ class executor
c += (b * (a.normalize() * inputs[i].normalize().transpose()));
}
-
- BLT_TRACE("Input %ld has crosstalk magnitude: %f || %f", i, c.magnitude());
}
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()
{
+ steps.clear();
std::vector initial_pings;
initial_pings.reserve(inputs.size());
for (auto [input, output] : blt::in_pairs(inputs, outputs))
@@ -114,7 +184,55 @@ class executor
} 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(data.correct_input * 100) / static_cast(data.incorrect_input + data.correct_input));
+ BLT_TRACE("Correct outputs %ld Incorrect outputs %ld | (%lf%%)", data.correct_output, data.incorrect_output,
+ static_cast(data.correct_output * 100) / static_cast(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(data.correct_input + data.correct_output) * 100 /
+ static_cast(data.correct_input + data.correct_output + data.incorrect_input + data.incorrect_output));
+ }
+
+ void print_execution_results()
{
using namespace blt::logging;
std::vector input_lines;
@@ -219,6 +337,16 @@ class executor
BLT_TRACE_STREAM << os << "\n";
}
}
+
+ std::vector& get_results()
+ {
+ return steps.back();
+ }
+
+ std::vector& get_inputs()
+ {
+ return inputs;
+ }
private:
weight_t weights;
@@ -264,14 +392,16 @@ void part_a()
executor cute(part_a_inputs, part_a_outputs);
cute.execute();
- cute.print();
+ cute.print_execution_results();
+ cute.print_correctness();
}
void part_b()
{
blt::log_box_t box(BLT_TRACE_STREAM, "Part B", 8);
executor cute(part_a_inputs, part_a_outputs);
- cute.crosstalk();
+ cute.print_crosstalk();
+// cute.print_crosstalk_table();
}
void part_c()
@@ -279,11 +409,58 @@ void part_c()
blt::log_box_t box(BLT_TRACE_STREAM, "Part C", 8);
executor cute(part_c_1_inputs, part_c_1_outputs);
cute.execute();
- cute.print();
+ cute.print_execution_results();
+ cute.print_correctness();
BLT_TRACE("--- { Part C with 3 extra pairs } ---");
executor cute2(part_c_2_inputs, part_c_2_outputs);
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::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()
@@ -294,8 +471,9 @@ int main()
part_a();
part_b();
part_c();
+ part_d();
std::vector 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();
+ cute.print_crosstalk();
}