aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--oop2w5/Blackboard.cpp48
-rw-r--r--oop2w5/Blackboard.h49
-rw-r--r--oop2w5/BlackboardStrategy.cpp82
-rw-r--r--oop2w5/BlackboardStrategy.h47
-rw-r--r--oop2w5/main.cpp9
l---------oop2w5/makefile1
6 files changed, 236 insertions, 0 deletions
diff --git a/oop2w5/Blackboard.cpp b/oop2w5/Blackboard.cpp
new file mode 100644
index 0000000..9c98992
--- /dev/null
+++ b/oop2w5/Blackboard.cpp
@@ -0,0 +1,48 @@
+#include <algorithm>
+
+#include "Blackboard.h"
+
+using std::max;
+using std::lock_guard;
+
+Blackboard::Blackboard() {
+ _bbs = new BlackboardStrategy();
+}
+
+Blackboard::~Blackboard() {
+ delete _bbs;
+}
+
+void Blackboard::run() {
+ _subthreads[0] = new thread([this] { spawn_subthreads(0, 3); }); // 1, 2, 3
+ _subthreads[1] = new thread([this] { spawn_subthreads(3, 3); }); // 4, 5, 6
+
+ _subthreads[0]->join();
+ _subthreads[1]->join();
+
+ _bbs->setBestResult(max(_results[0], _results[1]));
+}
+
+void Blackboard::spawn_subthreads(unsigned start_index, unsigned length) {
+ unsigned end_index = start_index + length;
+ // bereken geschiktheid strategie
+ for (unsigned i = start_index; i < end_index; i++)
+ _subsubthreads[i] = new thread([this, i] { _fitness[i] = (_bbs->*fitnessStrategies[i])(); });
+
+ // join alles
+ for (unsigned i = start_index; i < end_index; i++)
+ _subsubthreads[i]->join();
+
+
+ // beslis beste strategie (lelijk linear zoeken want maar 3 elementen)
+ lock_guard<mutex>* bbs_lock = new lock_guard<mutex>(_bbs_lock); // lock guard
+ _bbs->setBestStrategy(_fitness[start_index]); // beste fitness altijd in _fitness
+ for (unsigned i = start_index; i < end_index; i++)
+ _bbs->setBestStrategy(max(_fitness[i], _bbs->getBestStrategy()));
+ unsigned best_index = start_index;
+ for (unsigned i = start_index; i < end_index; i++)
+ if (_fitness[i] == _bbs->getBestStrategy()) { best_index = i; break; }
+ delete bbs_lock; // handmatig mutex vrijgeven (want volgende regel code duurt lang)
+
+ _results.push_back((_bbs->*resultStrategies[best_index])());
+}
diff --git a/oop2w5/Blackboard.h b/oop2w5/Blackboard.h
new file mode 100644
index 0000000..30be0be
--- /dev/null
+++ b/oop2w5/Blackboard.h
@@ -0,0 +1,49 @@
+#pragma once
+
+#include <thread>
+#include <vector>
+#include <mutex>
+
+#include "BlackboardStrategy.h"
+
+using std::thread;
+using std::vector;
+using std::mutex;
+
+unsigned gert();
+
+class Blackboard {
+private:
+ thread* _subthreads[6]; // first fork (2 split)
+ thread* _subsubthreads[6]; // second fork (3 split)
+ BlackboardStrategy* _bbs = nullptr; // BlackboardStrategy instance
+ mutex _bbs_lock; // BlackboardStrategy lock (set/getBest functions)
+ unsigned _fitness[6]; // fitness return values
+ vector<unsigned> _results; // result return values
+
+ unsigned (BlackboardStrategy::* fitnessStrategies[6])() const = {
+ &BlackboardStrategy::fitnessStrategy1,
+ &BlackboardStrategy::fitnessStrategy2,
+ &BlackboardStrategy::fitnessStrategy3,
+ &BlackboardStrategy::fitnessStrategy4,
+ &BlackboardStrategy::fitnessStrategy5,
+ &BlackboardStrategy::fitnessStrategy6,
+ };
+
+ unsigned (BlackboardStrategy::* resultStrategies[6])() const = {
+ &BlackboardStrategy::resultStrategy1,
+ &BlackboardStrategy::resultStrategy2,
+ &BlackboardStrategy::resultStrategy3,
+ &BlackboardStrategy::resultStrategy4,
+ &BlackboardStrategy::resultStrategy5,
+ &BlackboardStrategy::resultStrategy6,
+ };
+
+public:
+ Blackboard();
+ virtual ~Blackboard();
+ /** @brief start subthreads for strategy[start_index] until strategy[start_index + length] */
+ virtual void spawn_subthreads(unsigned start_index, unsigned length);
+ virtual void run();
+};
+
diff --git a/oop2w5/BlackboardStrategy.cpp b/oop2w5/BlackboardStrategy.cpp
new file mode 100644
index 0000000..5a828b2
--- /dev/null
+++ b/oop2w5/BlackboardStrategy.cpp
@@ -0,0 +1,82 @@
+#include "BlackboardStrategy.h"
+
+/*
+ * DO NOT ADAPT THIS FILE!!!
+ */
+
+#include <climits>
+#include <ctime>
+#include <random>
+
+BlackboardStrategy::BlackboardStrategy()
+{
+ initialize();
+}
+
+BlackboardStrategy::~BlackboardStrategy()
+{
+}
+
+unsigned BlackboardStrategy::fitnessStrategy1() const { return fitnessStrategy(1); }
+unsigned BlackboardStrategy::fitnessStrategy2() const { return fitnessStrategy(2); }
+unsigned BlackboardStrategy::fitnessStrategy3() const { return fitnessStrategy(3); }
+unsigned BlackboardStrategy::fitnessStrategy4() const { return fitnessStrategy(4); }
+unsigned BlackboardStrategy::fitnessStrategy5() const { return fitnessStrategy(5); }
+unsigned BlackboardStrategy::fitnessStrategy6() const { return fitnessStrategy(6); }
+
+unsigned BlackboardStrategy::resultStrategy1() const { return resultStrategy(1); }
+unsigned BlackboardStrategy::resultStrategy2() const { return resultStrategy(2); }
+unsigned BlackboardStrategy::resultStrategy3() const { return resultStrategy(3); }
+unsigned BlackboardStrategy::resultStrategy4() const { return resultStrategy(4); }
+unsigned BlackboardStrategy::resultStrategy5() const { return resultStrategy(5); }
+unsigned BlackboardStrategy::resultStrategy6() const { return resultStrategy(6); }
+
+void BlackboardStrategy::initialize()
+{
+ m_uBestStrategy = 0;
+ m_uBestResult = 0;
+}
+
+void BlackboardStrategy::setBestStrategy( unsigned uBestStrategy )
+{
+ m_uBestStrategy = uBestStrategy;
+}
+
+void BlackboardStrategy::setBestResult( unsigned uBestResult )
+{
+ m_uBestResult = uBestResult;
+}
+
+unsigned BlackboardStrategy::getBestStrategy() const
+{
+ return m_uBestStrategy;
+}
+
+unsigned BlackboardStrategy::getBestResult() const
+{
+ return m_uBestResult;
+}
+
+unsigned BlackboardStrategy::fitnessStrategy( unsigned uStrategy ) const
+{
+ return value( uStrategy, 10000 );
+}
+
+unsigned BlackboardStrategy::resultStrategy( unsigned uStrategy ) const
+{
+ return value( uStrategy, 1000000 );
+}
+
+unsigned BlackboardStrategy::value( unsigned uStrategy, unsigned uTotal ) const
+{
+ std::default_random_engine cRandomEngine( unsigned(time(nullptr))+uStrategy );
+ std::uniform_real_distribution<double> cDistribution( 0, 10 );
+
+ unsigned nResult = 0;
+ double dStep = double(0xffffffffUL) / ( uTotal+uStrategy );
+
+ for ( unsigned n = 0; n < uTotal; n++ )
+ nResult += unsigned( dStep * cDistribution( cRandomEngine ) );
+
+ return nResult;
+}
diff --git a/oop2w5/BlackboardStrategy.h b/oop2w5/BlackboardStrategy.h
new file mode 100644
index 0000000..9ab391a
--- /dev/null
+++ b/oop2w5/BlackboardStrategy.h
@@ -0,0 +1,47 @@
+#pragma once
+
+/*
+ * DO NOT ADAPT THIS FILE!!!
+ */
+
+class BlackboardStrategy
+{
+public:
+ BlackboardStrategy();
+ virtual ~BlackboardStrategy();
+
+public:
+ virtual unsigned fitnessStrategy1() const;
+ virtual unsigned fitnessStrategy2() const;
+ virtual unsigned fitnessStrategy3() const;
+ virtual unsigned fitnessStrategy4() const;
+ virtual unsigned fitnessStrategy5() const;
+ virtual unsigned fitnessStrategy6() const;
+
+public:
+ virtual unsigned resultStrategy1() const;
+ virtual unsigned resultStrategy2() const;
+ virtual unsigned resultStrategy3() const;
+ virtual unsigned resultStrategy4() const;
+ virtual unsigned resultStrategy5() const;
+ virtual unsigned resultStrategy6() const;
+
+public:
+ virtual void initialize();
+ virtual void setBestStrategy( unsigned );
+ virtual void setBestResult( unsigned );
+
+public:
+ virtual unsigned getBestStrategy() const;
+ virtual unsigned getBestResult() const;
+
+private:
+ virtual unsigned fitnessStrategy( unsigned ) const;
+ virtual unsigned resultStrategy( unsigned ) const;
+
+ virtual unsigned value( unsigned, unsigned ) const;
+
+private:
+ unsigned m_uBestStrategy; // contains the best strategy, i.e. value 1 to 6
+ unsigned m_uBestResult; // contains the best (i.e. highest) value of the result
+};
diff --git a/oop2w5/main.cpp b/oop2w5/main.cpp
new file mode 100644
index 0000000..be71bde
--- /dev/null
+++ b/oop2w5/main.cpp
@@ -0,0 +1,9 @@
+#include "Blackboard.h"
+
+int main() {
+ Blackboard cBlackboard;
+
+ cBlackboard.run();
+
+ return 0;
+}
diff --git a/oop2w5/makefile b/oop2w5/makefile
new file mode 120000
index 0000000..a4e84c6
--- /dev/null
+++ b/oop2w5/makefile
@@ -0,0 +1 @@
+../week.mk \ No newline at end of file