summaryrefslogtreecommitdiff
path: root/src/explicitsimulation.h
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2012-07-17 09:11:40 -0600
committerMike Buland <mike@xagasoft.com>2012-07-17 09:11:40 -0600
commita2679e3bc2f407fae6b97908aeebb215c6678ebc (patch)
treeac220ef865dc38b6b4a10a03e5d48aad261cddd3 /src/explicitsimulation.h
parenta48556c1c5c01892cf493cd3eb4ff969a2f548fd (diff)
downloadlibgenetic-a2679e3bc2f407fae6b97908aeebb215c6678ebc.tar.gz
libgenetic-a2679e3bc2f407fae6b97908aeebb215c6678ebc.tar.bz2
libgenetic-a2679e3bc2f407fae6b97908aeebb215c6678ebc.tar.xz
libgenetic-a2679e3bc2f407fae6b97908aeebb215c6678ebc.zip
It all works multi-threaded.
It restarts the threads every generation. Maybe not the worst thing every, but it seems like it would be better to stay in the thread and notify the main thread that they're done with that iteration.
Diffstat (limited to '')
-rw-r--r--src/explicitsimulation.h35
1 files changed, 33 insertions, 2 deletions
diff --git a/src/explicitsimulation.h b/src/explicitsimulation.h
index ebddd62..c2aeed9 100644
--- a/src/explicitsimulation.h
+++ b/src/explicitsimulation.h
@@ -4,6 +4,10 @@
4#include "genetic/population.h" 4#include "genetic/population.h"
5#include "genetic/config.h" 5#include "genetic/config.h"
6 6
7#include <bu/thread.h>
8#include <bu/mutex.h>
9#include <bu/synchroqueue.h>
10
7namespace Genetic 11namespace Genetic
8{ 12{
9 class Operator; 13 class Operator;
@@ -13,7 +17,8 @@ namespace Genetic
13 { 17 {
14 public: 18 public:
15 ExplicitSimulation( Operator *pOper, FitnessFunction *pFunc, 19 ExplicitSimulation( Operator *pOper, FitnessFunction *pFunc,
16 int iPopSize, float fKeep, float fRandom, bool bKeepBest=true ); 20 int iThreads, int iPopSize, float fKeep, float fRandom,
21 bool bKeepBest=true );
17 virtual ~ExplicitSimulation(); 22 virtual ~ExplicitSimulation();
18 23
19 void timestep(); 24 void timestep();
@@ -25,11 +30,11 @@ namespace Genetic
25 30
26 protected: 31 protected:
27 void updateFitness(); 32 void updateFitness();
33 void setFitness( Genetic::PhenotypeId, double dFitness );
28 34
29 protected: 35 protected:
30 Population xPop; 36 Population xPop;
31 Operator *pOper; 37 Operator *pOper;
32 FitnessFunction *pFunc;
33 38
34 private: 39 private:
35 int iPopSize; 40 int iPopSize;
@@ -42,6 +47,32 @@ namespace Genetic
42 double dMaxFitness; 47 double dMaxFitness;
43 double dTotalFitness; 48 double dTotalFitness;
44 PhenotypeId uMaxFitness; 49 PhenotypeId uMaxFitness;
50
51 Bu::Mutex mFitness;
52 typedef Bu::SynchroQueue<Phenotype *> WorkQueue;
53 WorkQueue qWork;
54
55 class Processor : public Bu::Thread
56 {
57 public:
58 Processor( ExplicitSimulation &rSim, FitnessFunction *pFunc,
59 WorkQueue &rqWork, int iId );
60 virtual ~Processor();
61
62 void setRunning( bool b ) { bRunning = b; }
63
64 protected:
65 virtual void run();
66
67 ExplicitSimulation &rSim;
68 FitnessFunction *pFunc;
69 WorkQueue &rqWork;
70 int iId;
71 bool bRunning;
72 };
73
74 typedef Bu::List<Processor *> ProcessorList;
75 ProcessorList lProcessor;
45 }; 76 };
46}; 77};
47 78