working on refactor

main
Brett 2024-10-03 19:56:11 -04:00
parent 6a06c17b10
commit a713ea1778
5 changed files with 237 additions and 39 deletions

View File

@ -0,0 +1,119 @@
<?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

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(COSC-4P80-Assignment-1 VERSION 0.0.16) project(COSC-4P80-Assignment-1 VERSION 0.0.17)
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

@ -22,14 +22,20 @@
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/math/matrix.h> #include <blt/math/matrix.h>
#include <blt/math/log_util.h> #include <blt/math/log_util.h>
#include <algorithm>
#include <numeric>
#include <vector>
namespace a1 namespace a1
{ {
template<blt::size_t rows, blt::size_t columns>
using matrix_t = blt::generalized_matrix<float, rows, columns>;
void test_math() void test_math()
{ {
blt::generalized_matrix<float, 1, 4> input{1, -1, -1, 1}; matrix_t<1, 4> input{1, -1, -1, 1};
blt::generalized_matrix<float, 1, 3> output{1, 1, 1}; matrix_t<1, 3> output{1, 1, 1};
blt::generalized_matrix<float, 4, 3> expected{ matrix_t<4, 3> expected{
blt::vec4{1, -1, -1, 1}, blt::vec4{1, -1, -1, 1},
blt::vec4{1, -1, -1, 1}, blt::vec4{1, -1, -1, 1},
blt::vec4{1, -1, -1, 1} blt::vec4{1, -1, -1, 1}
@ -41,8 +47,8 @@ namespace a1
blt::vec4 one{5, 1, 3, 0}; blt::vec4 one{5, 1, 3, 0};
blt::vec4 two{9, -5, -8, 3}; blt::vec4 two{9, -5, -8, 3};
blt::generalized_matrix<float, 1, 4> g1{5, 1, 3, 0}; matrix_t<1, 4> g1{5, 1, 3, 0};
blt::generalized_matrix<float, 1, 4> g2{9, -5, -8, 3}; matrix_t<1, 4> g2{9, -5, -8, 3};
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");
} }
@ -53,21 +59,8 @@ namespace a1
return i * j.transpose(); return i * j.transpose();
} }
template<typename T, blt::u32 rows, blt::u32 columns> template<typename T>
blt::generalized_matrix<T, rows, columns> threshold(const blt::generalized_matrix<T, rows, columns>& y, blt::size_t difference(const std::vector<T>& a, const std::vector<T>& b)
const blt::generalized_matrix<T, rows, columns>& base)
{
blt::generalized_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] > 1 ? 1 : (y[i][j] < -1 ? -1 : base[i][j]);
}
return result;
}
template<typename T, blt::size_t size>
blt::size_t difference(const std::array<T, size>& a, const std::array<T, size>& b)
{ {
blt::size_t count = 0; blt::size_t count = 0;
for (const auto& [a_val, b_val] : blt::in_pairs(a, b)) for (const auto& [a_val, b_val] : blt::in_pairs(a, b))
@ -78,8 +71,8 @@ namespace a1
return count; return count;
} }
template<typename T, blt::size_t size> template<typename T>
bool equal(const std::array<T, size>& a, const std::array<T, size>& b) bool equal(const std::vector<T>& a, const std::vector<T>& b)
{ {
return difference(a, b) == 0; return difference(a, b) == 0;
} }
@ -90,15 +83,11 @@ namespace a1
output_t output_recall = input * associated_weights; output_t output_recall = input * associated_weights;
input_t input_recall = output_recall * associated_weights.transpose(); input_t input_recall = output_recall * associated_weights.transpose();
// BLT_DEBUG_STREAM << "Input: " << input.vec_from_column_row() << "\nOutput: " << output.vec_from_column_row() << '\n';
// BLT_DEBUG_STREAM << "Recalled Input: " << a1::threshold(input_recall, input).vec_from_column_row() << "\nRecalled Output: "
// << a1::threshold(output_recall, output).vec_from_column_row() << '\n';
return std::pair{a1::threshold(input_recall, input), a1::threshold(output_recall, output)}; return std::pair{a1::threshold(input_recall, input), a1::threshold(output_recall, output)};
} }
template<typename weight_t, typename T, typename G, blt::size_t size> template<typename weight_t, typename T, typename G>
void check_recall(const weight_t& weights, const std::array<G, size>& inputs, const std::array<T, size>& outputs) void check_recall(const weight_t& weights, const std::vector<G>& inputs, const std::vector<T>& outputs)
{ {
for (const auto& [index, val] : blt::enumerate(inputs)) for (const auto& [index, val] : blt::enumerate(inputs))
{ {

@ -1 +1 @@
Subproject commit bfcb35705910851a6a7466406609793bae3eb2be Subproject commit 7fa4fd0af29e7c9eb7c6ab1adf48cef0b6d38850

View File

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#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"
@ -12,10 +13,99 @@ constexpr blt::u32 num_values_part_c2 = 7;
constexpr blt::u32 input_vec_size = 5; constexpr blt::u32 input_vec_size = 5;
constexpr blt::u32 output_vec_size = 4; constexpr blt::u32 output_vec_size = 4;
using input_t = blt::generalized_matrix<float, 1, input_vec_size>; using input_t = a1::matrix_t<1, input_vec_size>;
using output_t = blt::generalized_matrix<float, 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 = blt::generalized_matrix<float, 1, output_vec_size>; using crosstalk_t = a1::matrix_t<1, output_vec_size>;
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, output_t output): weights(std::move(weights)), input(std::move(input)), output(std::move(output))
{}
[[nodiscard]] ping_pong run_step() const
{
auto out = input * weights;
return {weights, threshold(out * weights.transpose()), threshold(out)};
}
[[nodiscard]] ping_pong pong() const
{
return run_step();
}
input_t get_input()
{
return input;
}
output_t get_output()
{
return output;
}
friend bool operator==(const ping_pong& a, const ping_pong& b)
{
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:
weight_t weights;
input_t input;
output_t output;
};
class executor
{
public:
executor(weight_t weights, std::vector<input_t> inputs, std::vector<output_t> outputs):
weights(std::move(weights)), inputs(std::move(inputs)), outputs(std::move(outputs))
{}
void execute()
{
std::vector<ping_pong> initial_pings;
for (auto [input, output] : blt::in_pairs(inputs, outputs))
initial_pings.emplace_back(weights, input, output);
steps.emplace_back(std::move(initial_pings));
// execute while the entries don't equal each other (no stability in the system)
do
{
auto& prev = steps.rbegin()[0];
std::vector<ping_pong> next_pongs;
for (auto& ping : prev)
next_pongs.emplace_back(ping.pong());
steps.emplace_back(std::move(next_pongs));
} while (!(steps.rbegin()[0] == steps.rbegin()[1]));
}
void print_chains()
{
}
private:
weight_t weights;
std::vector<input_t> inputs;
std::vector<output_t> outputs;
std::vector<std::vector<ping_pong>> steps;
};
// part a // part a
input_t input_1{-1, 1, 1, 1, -1}; input_t input_1{-1, 1, 1, 1, -1};
@ -47,14 +137,14 @@ const weight_t weight_5 = input_5.transpose() * output_5;
const weight_t weight_6 = input_6.transpose() * output_6; const weight_t weight_6 = input_6.transpose() * output_6;
const weight_t weight_7 = input_7.transpose() * output_7; const weight_t weight_7 = input_7.transpose() * output_7;
auto part_a_inputs = std::array{input_1, input_2, input_3}; auto part_a_inputs = std::vector{input_1, input_2, input_3};
auto part_a_outputs = std::array{output_1, output_2, output_3}; auto part_a_outputs = std::vector{output_1, output_2, output_3};
auto part_c_1_inputs = std::array{input_1, input_2, input_3, input_4}; auto part_c_1_inputs = std::vector{input_1, input_2, input_3, input_4};
auto part_c_1_outputs = std::array{output_1, output_2, output_3, output_4}; auto part_c_1_outputs = std::vector{output_1, output_2, output_3, output_4};
auto part_c_2_inputs = std::array{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::array{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_a = weight_1 + weight_2 + weight_3;
const auto weight_total_c = weight_total_a + weight_4; const auto weight_total_c = weight_total_a + weight_4;