From d213341d9cf572ccc567bf8a0ab1a1921f7c38e7 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 22 Jan 2018 21:39:23 -0500 Subject: [PATCH] Introduces the counters upon which I expect dynamic analysis to rest. --- DynamicAnalyser/ConfidenceCounter.cpp | 28 ++++++++++++ DynamicAnalyser/ConfidenceCounter.hpp | 45 +++++++++++++++++++ DynamicAnalyser/ConfidenceSource.hpp | 26 +++++++++++ DynamicAnalyser/ConfidenceSummary.cpp | 28 ++++++++++++ DynamicAnalyser/ConfidenceSummary.hpp | 44 ++++++++++++++++++ .../Clock Signal.xcodeproj/project.pbxproj | 27 +++++++++++ 6 files changed, 198 insertions(+) create mode 100644 DynamicAnalyser/ConfidenceCounter.cpp create mode 100644 DynamicAnalyser/ConfidenceCounter.hpp create mode 100644 DynamicAnalyser/ConfidenceSource.hpp create mode 100644 DynamicAnalyser/ConfidenceSummary.cpp create mode 100644 DynamicAnalyser/ConfidenceSummary.hpp diff --git a/DynamicAnalyser/ConfidenceCounter.cpp b/DynamicAnalyser/ConfidenceCounter.cpp new file mode 100644 index 000000000..2a7ebfae9 --- /dev/null +++ b/DynamicAnalyser/ConfidenceCounter.cpp @@ -0,0 +1,28 @@ +// +// ConfidenceCounter.cpp +// Clock Signal +// +// Created by Thomas Harte on 21/01/2018. +// Copyright © 2018 Thomas Harte. All rights reserved. +// + +#include "ConfidenceCounter.hpp" + +using namespace DynamicAnalyser; + +float ConfidenceCounter::get_probability() { + return static_cast(hits_) / static_cast(hits_ + misses_); +} + +void ConfidenceCounter::add_hit() { + hits_++; +} + +void ConfidenceCounter::add_miss() { + misses_++; +} + +void ConfidenceCounter::add_equivocal() { + hits_++; + misses_++; +} diff --git a/DynamicAnalyser/ConfidenceCounter.hpp b/DynamicAnalyser/ConfidenceCounter.hpp new file mode 100644 index 000000000..68f8ef91e --- /dev/null +++ b/DynamicAnalyser/ConfidenceCounter.hpp @@ -0,0 +1,45 @@ +// +// ConfidenceCounter.hpp +// Clock Signal +// +// Created by Thomas Harte on 21/01/2018. +// Copyright © 2018 Thomas Harte. All rights reserved. +// + +#ifndef ConfidenceCounter_hpp +#define ConfidenceCounter_hpp + +#include "ConfidenceSource.hpp" + +namespace DynamicAnalyser { + +/*! + Provides a confidence source that calculates its probability by virtual of a history of events. + + The initial value of the confidence counter is 0.5. +*/ +class ConfidenceCounter: public ConfidenceSource { + public: + /*! @returns The computed probability, based on the history of events. */ + float get_probability() override; + + /*! Records an event that implies this is the appropriate class — pushes probability up towards 1.0. */ + void add_hit(); + + /*! Records an event that implies this is not the appropriate class — pushes probability down towards 0.0. */ + void add_miss(); + + /*! + Records an event that provides no strong evidence either way — one that + could be a hit but could be a miss — pushes probability up or down towards 0.5. + */ + void add_equivocal(); + + private: + int hits_ = 1; + int misses_ = 1; +}; + +} + +#endif /* ConfidenceCounter_hpp */ diff --git a/DynamicAnalyser/ConfidenceSource.hpp b/DynamicAnalyser/ConfidenceSource.hpp new file mode 100644 index 000000000..0f959f9d1 --- /dev/null +++ b/DynamicAnalyser/ConfidenceSource.hpp @@ -0,0 +1,26 @@ +// +// ConfidenceSource.hpp +// Clock Signal +// +// Created by Thomas Harte on 21/01/2018. +// Copyright © 2018 Thomas Harte. All rights reserved. +// + +#ifndef ConfidenceSource_hpp +#define ConfidenceSource_hpp + +namespace DynamicAnalyser { + +/*! + Provides an abstract interface through which objects can declare the probability + that they are the proper target for their input; e.g. if an Acorn Electron is asked + to run an Atari 2600 program then its confidence should shrink towards 0.0; if the + program is handed to an Atari 2600 then its confidence should grow towards 1.0. +*/ +struct ConfidenceSource { + virtual float get_probability() = 0; +}; + +} + +#endif /* ConfidenceSource_hpp */ diff --git a/DynamicAnalyser/ConfidenceSummary.cpp b/DynamicAnalyser/ConfidenceSummary.cpp new file mode 100644 index 000000000..f2888a4d8 --- /dev/null +++ b/DynamicAnalyser/ConfidenceSummary.cpp @@ -0,0 +1,28 @@ +// +// ConfidenceSummary.cpp +// Clock Signal +// +// Created by Thomas Harte on 21/01/2018. +// Copyright © 2018 Thomas Harte. All rights reserved. +// + +#include "ConfidenceSummary.hpp" + +#include +#include + +using namespace DynamicAnalyser; + +ConfidenceSummary::ConfidenceSummary(const std::vector &sources, const std::vector &weights) : + sources_(sources), weights_(weights) { + assert(weights.size() == sources.size()); + weight_sum_ = std::accumulate(weights.begin(), weights.end(), 0.0f); +} + +float ConfidenceSummary::get_probability() { + float result = 0.0f; + for(std::size_t index = 0; index < sources_.size(); ++index) { + result += sources_[index]->get_probability() * weights_[index]; + } + return result / weight_sum_; +} diff --git a/DynamicAnalyser/ConfidenceSummary.hpp b/DynamicAnalyser/ConfidenceSummary.hpp new file mode 100644 index 000000000..a3f8f2fb7 --- /dev/null +++ b/DynamicAnalyser/ConfidenceSummary.hpp @@ -0,0 +1,44 @@ +// +// ConfidenceSummary.hpp +// Clock Signal +// +// Created by Thomas Harte on 21/01/2018. +// Copyright © 2018 Thomas Harte. All rights reserved. +// + +#ifndef ConfidenceSummary_hpp +#define ConfidenceSummary_hpp + +#include "ConfidenceSource.hpp" + +#include + +namespace DynamicAnalyser { + +/*! + Summaries a collection of confidence sources by calculating their weighted sum. +*/ +class ConfidenceSummary: public ConfidenceSource { + public: + /*! + Instantiates a summary that will produce the weighted sum of + @c sources, each using the corresponding entry of @c weights. + + Requires that @c sources and @c weights are of the same length. + */ + ConfidenceSummary( + const std::vector &sources, + const std::vector &weights); + + /*! @returns The weighted sum of all sources. */ + float get_probability() override; + + private: + std::vector sources_; + std::vector weights_; + float weight_sum_; +}; + +} + +#endif /* ConfidenceSummary_hpp */ diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 7a0cc8a1a..42dd15821 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -215,6 +215,10 @@ 4B54C0C51F8D91D90050900F /* Keyboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B54C0C41F8D91D90050900F /* Keyboard.cpp */; }; 4B54C0C81F8D91E50050900F /* Keyboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B54C0C61F8D91E50050900F /* Keyboard.cpp */; }; 4B54C0CB1F8D92590050900F /* Keyboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B54C0CA1F8D92580050900F /* Keyboard.cpp */; }; + 4B5539FF201583AD00027510 /* ConfidenceCounter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5539FD201583AD00027510 /* ConfidenceCounter.cpp */; }; + 4B553A00201583AD00027510 /* ConfidenceCounter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5539FD201583AD00027510 /* ConfidenceCounter.cpp */; }; + 4B553A032015855900027510 /* ConfidenceSummary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B553A012015855900027510 /* ConfidenceSummary.cpp */; }; + 4B553A042015855900027510 /* ConfidenceSummary.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B553A012015855900027510 /* ConfidenceSummary.cpp */; }; 4B55CE5D1C3B7D6F0093A61B /* CSOpenGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B55CE5C1C3B7D6F0093A61B /* CSOpenGLView.m */; }; 4B55CE5F1C3B7D960093A61B /* MachineDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B55CE5E1C3B7D960093A61B /* MachineDocument.swift */; }; 4B58601E1F806AB200AEE2E3 /* MFMSectorDump.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B58601C1F806AB200AEE2E3 /* MFMSectorDump.cpp */; }; @@ -822,6 +826,11 @@ 4B54C0C71F8D91E50050900F /* Keyboard.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Keyboard.hpp; path = Electron/Keyboard.hpp; sourceTree = ""; }; 4B54C0C91F8D92580050900F /* Keyboard.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Keyboard.hpp; path = ZX8081/Keyboard.hpp; sourceTree = ""; }; 4B54C0CA1F8D92580050900F /* Keyboard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Keyboard.cpp; path = ZX8081/Keyboard.cpp; sourceTree = ""; }; + 4B5539F62015820B00027510 /* ConfidenceSource.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ConfidenceSource.hpp; sourceTree = ""; }; + 4B5539FD201583AD00027510 /* ConfidenceCounter.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ConfidenceCounter.cpp; sourceTree = ""; }; + 4B5539FE201583AD00027510 /* ConfidenceCounter.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ConfidenceCounter.hpp; sourceTree = ""; }; + 4B553A012015855900027510 /* ConfidenceSummary.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ConfidenceSummary.cpp; sourceTree = ""; }; + 4B553A022015855900027510 /* ConfidenceSummary.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ConfidenceSummary.hpp; sourceTree = ""; }; 4B55CE5B1C3B7D6F0093A61B /* CSOpenGLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSOpenGLView.h; sourceTree = ""; }; 4B55CE5C1C3B7D6F0093A61B /* CSOpenGLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSOpenGLView.m; sourceTree = ""; }; 4B55CE5E1C3B7D960093A61B /* MachineDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MachineDocument.swift; sourceTree = ""; }; @@ -1855,6 +1864,19 @@ path = 1540; sourceTree = ""; }; + 4B5539F52015820B00027510 /* DynamicAnalyser */ = { + isa = PBXGroup; + children = ( + 4B5539F62015820B00027510 /* ConfidenceSource.hpp */, + 4B5539FD201583AD00027510 /* ConfidenceCounter.cpp */, + 4B5539FE201583AD00027510 /* ConfidenceCounter.hpp */, + 4B553A012015855900027510 /* ConfidenceSummary.cpp */, + 4B553A022015855900027510 /* ConfidenceSummary.hpp */, + ); + name = DynamicAnalyser; + path = ../../DynamicAnalyser; + sourceTree = ""; + }; 4B55CE551C3B7D360093A61B /* Documents */ = { isa = PBXGroup; children = ( @@ -2431,6 +2453,7 @@ 4BC9DF4A1D04691600F44158 /* Components */, 4B3940E81DA83C8700427841 /* Concurrency */, 4B31B88E1FBFBCD800C140D5 /* Configurable */, + 4B5539F52015820B00027510 /* DynamicAnalyser */, 4B055A761FAE78210060FFFF /* Frameworks */, 4B86E2581F8C628F006FAA45 /* Inputs */, 4BB73EDC1B587CA500552FC2 /* Machines */, @@ -3317,6 +3340,7 @@ 4B055AA51FAE85EF0060FFFF /* Encoder.cpp in Sources */, 4B055AEA1FAE9B990060FFFF /* 6502Storage.cpp in Sources */, 4B055A8A1FAE855B0060FFFF /* Tape.cpp in Sources */, + 4B553A042015855900027510 /* ConfidenceSummary.cpp in Sources */, 4B055AA71FAE85EF0060FFFF /* SegmentParser.cpp in Sources */, 4B055AC11FAE98DC0060FFFF /* MachineForTarget.cpp in Sources */, 4B055AD81FAE9B180060FFFF /* Video.cpp in Sources */, @@ -3391,6 +3415,7 @@ 4B055AAD1FAE85FD0060FFFF /* PCMTrack.cpp in Sources */, 4B055A841FAE85450060FFFF /* Disk.cpp in Sources */, 4B055A831FAE85410060FFFF /* Tape.cpp in Sources */, + 4B553A00201583AD00027510 /* ConfidenceCounter.cpp in Sources */, 4B055AC61FAE9AEE0060FFFF /* TIASound.cpp in Sources */, 4B055AA81FAE85EF0060FFFF /* Shifter.cpp in Sources */, 4B055AC81FAE9AFB0060FFFF /* C1540.cpp in Sources */, @@ -3531,6 +3556,7 @@ 4B55CE5D1C3B7D6F0093A61B /* CSOpenGLView.m in Sources */, 4BB697CB1D4B6D3E00248BDF /* TimedEventLoop.cpp in Sources */, 4B54C0C21F8D91CD0050900F /* Keyboard.cpp in Sources */, + 4B553A032015855900027510 /* ConfidenceSummary.cpp in Sources */, 4BBC951E1F368D83008F4C34 /* i8272.cpp in Sources */, 4BF1354C1D6D2C300054B2EA /* StaticAnalyser.cpp in Sources */, 4B4A76301DB1A3FA007AAE2E /* AY38910.cpp in Sources */, @@ -3565,6 +3591,7 @@ 4BFE7B871FC39BF100160B38 /* StandardOptions.cpp in Sources */, 4B5FADC01DE3BF2B00AEC565 /* Microdisc.cpp in Sources */, 4B54C0C81F8D91E50050900F /* Keyboard.cpp in Sources */, + 4B5539FF201583AD00027510 /* ConfidenceCounter.cpp in Sources */, 4B79A5011FC913C900EEDAD5 /* MSX.cpp in Sources */, 4BEE0A701D72496600532C7B /* PRG.cpp in Sources */, 4B8334861F5DA3780097E338 /* 6502Storage.cpp in Sources */,