-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsngpworker.h
139 lines (108 loc) · 2.82 KB
/
sngpworker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef SNGPWORKER_H
#define SNGPWORKER_H
#include <QThread>
#include <QMutex>
#include <vector>
#include "snode.h"
#include "sevalengine.h"
#include "problem.h"
/*
* Worker thread and interface to the Single Node GP engine.
*/
class SNGPWorker : public QThread
{
Q_OBJECT
public:
SNGPWorker();
virtual ~SNGPWorker();
/*
* Set the problem. Ownership is passed to this class.
* Object is deleted next time setProblem is called.
* Also called when SNGPWorker is destroyed.
*/
void setProblem(Problem* problem);
/*
* Set the number of times to run the GP engine to
* the termination condition.
* Current run stats and solutions can be found in
* getStats().
*/
void setNumTimesToRun(int times);
/*
* Set the max number of generations to run the
* GP engine.
*/
void setNumMaxGenerations(int maxGenerations);
/*
* Reset the current stats.
*/
void reset();
/*
* Pause the current run.
*/
void pause();
/*
* Resume the current run, or start a new one (when
* resume is first called or after a reset()).
*/
void resume();
/*
* Return true if the engine is still running.
*/
bool isRunning();
/*
* Run one generation.
*/
void step();
/*
* Get the stats for the current gp run.
*/
const SNodeStats& getStats() { return _stats; }
/*
* Get the list of nodes, only updated when stopped.
*/
const std::vector<SNode>& getNodes();
/*
* Get the fitness of all individual nodes, only
* update when stopped.
*/
const std::vector<int> &getFitness();
/*
* Get a somewhat readable output of the
* program for the given node.
*/
QString getProgramAsText(int i);
private:
/*
* QThread::run override.
*/
virtual void run();
void runGeneration();
void resetFitness();
// True when running, false when stopped.
// Note: that the thread may still be running, but it won't
// muck with private member variables.
bool _bRunning;
// The GP evaluation engine
SEvalEngine _evalEngine;
// Copy of the nodes, updates only when stopped.
std::vector<SNode> _nodesCopy;
// Current fitness values
std::vector<int> _fitness;
// Copy of the fitness values, updates only when
// stopped
std::vector<int> _fitnessCopy;
// Memory barrier and lock for communication between
// UI and worker thread.
QMutex _mutex;
// Current stats, updated during execution.
SNodeStats _stats;
// The current problem set
Problem* _problem;
// Number of times to run to completion
int _times;
// The max number of generations to allow before
// terminating the current run.
int _maxGenerations;
};
#endif // SNGPWORKER_H