-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrunner.h
More file actions
229 lines (161 loc) · 5.77 KB
/
Copy pathrunner.h
File metadata and controls
229 lines (161 loc) · 5.77 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* This file is part of the ResOpt project.
*
* Copyright (C) 2011-2012 Aleksander O. Juell <aleksander.juell@ntnu.no>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef RUNNER_H
#define RUNNER_H
/**
* @brief Class that controls the main execution of ResOpt
*
*/
#include <tr1/memory>
#include <time.h>
#include <QString>
#include <QFile>
#include <QVector>
#include <QObject>
class QThread;
using std::tr1::dynamic_pointer_cast;
using std::tr1::shared_ptr;
namespace ResOpt
{
class Model;
class ReservoirSimulator;
class ModelReader;
class Optimizer;
class Launcher;
class CaseQueue;
class Case;
class Component;
class Logger;
/**
* @brief Main execution class.
*
*/
class Runner : public QObject
{
Q_OBJECT
private:
QVector<Launcher*> m_launchers;
QVector<bool> m_launcher_running;
QVector<QThread*> m_threads;
ModelReader *p_reader;
Model *p_model;
ReservoirSimulator *p_simulator;
Optimizer *p_optimizer;
QFile *p_summary;
QFile *p_debug;
int m_number_of_runs;
int m_number_of_res_sim_runs;
time_t m_start_time;
CaseQueue *p_cases;
Launcher *p_last_run_launcher;
bool m_paused;
QString m_debug_filename;
bool m_debug;
int m_debug_case;
Case *p_best_case;
Logger *p_logger;
/**
* @brief Writes the problem definition to the summary file
* @details A list of all the variables and constraints are printed to the summary file.
*/
void writeProblemDefToSummary();
/**
* @brief Writes the results from all the cases to the summary file
* @details The variable, constraints and objective values are printed as a new line to the summary file.
*/
void writeCasesToSummary();
public:
explicit Runner(const QString &driver_file, QObject *parent = 0);
~Runner();
/**
* @brief Initializes the model and optimizer.
* @details The Model is first read from the driver file. Then the ReservoirSimulator and Optimizer are initialized.
*
*/
void initialize();
void initializeLaunchers();
void printDebug(Launcher *l);
void transferModelStateFromLauncher();
/**
* @brief Checks if any of the constraints are broken. Returns true if the solution is feasible.
*
* @param c
* @return bool
*/
bool isFeasible(Case *c);
// set functions
void setSummaryFile(const QString &f);
void setDebugFileName(const QString &f);
void setOptimizer(Optimizer *o) {p_optimizer = o;}
void setReservoirSimulator(ReservoirSimulator *s) {p_simulator = s;}
// get functions
Model* model() {return p_model;}
Optimizer* optimizer() {return p_optimizer;}
ReservoirSimulator* reservoirSimulator() {return p_simulator;}
bool hasDebugFile() const {return m_debug;}
QString debugFileName() const {return m_debug_filename;}
ModelReader* modelReader() {return p_reader;}
Logger* logger() {return p_logger;}
public slots:
/**
* @brief This slot is called whenever a Launcher finishes with its model evaluation.
* @details If there are more cases to run in the queue, the launcher is sent a new case from the queue to evaluate. If there are no more cases to run, this function checks if all the other
* launchers have finished. If all the launchers have finished, casesFinished() is emitted, and the results are written to the summary file.
*
* @param l
*/
void onLauncherFinished(Launcher *l, Component *comp, Case *finished_case);
/**
* @brief Starts the Optimizer
* @details The function first checks if the Model has been initialized. If not initialize() is called. Then the Optimizer is started
* through Optimizer::start().
*
*/
void run();
bool runFromCase(Case *starting_point);
/**
* @brief Evaluates a list of cases.
* @details This function is called by an Optimizer, or another class that needs to evaluate the Model.
* The cases in the queue is distrubuted among the Launchers for model evaluation. When a Launcher finishes the evaluation,
* it emits a signal that is connected to the onLauncherFinished() slot. Further distrubution of cases is take from there. When
* calling this function, it should be done within an event loop. The event loop should wait for the casesFinished() signal before
* proceeding.
*
* @param cases A list of the cases that should be run
* @param comp A pointer to the component of the Model that should be evaluated. If this is a null pointer, the entire Model is evaluated.
*/
void evaluate(CaseQueue *cases, Component *comp);
void writeBestCaseToSummary(Case *c);
/**
* @brief Increases the number of simulator runs by 1
*
*/
void incrementReservoirSimRuns() {++m_number_of_res_sim_runs;}
void setPaused(bool paused);
void onOptimizationFinished();
signals:
void runnerFinished(Runner *r, Case *c);
void casesFinished();
void sendCase(Case *c, Component *comp);
void newCaseFinished(Case *c);
void resumePaused();
};
} // namespace ResOpt
#endif // RUNNER_H