-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMPLSolver.cpp
424 lines (350 loc) · 17.1 KB
/
AMPLSolver.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*==============================================================================
AMPL Solver
This file provides the implementation of the methods of the AMLP Solver actor
that is instantiated by the Solution Manager and used to obtain solutions for
optimisation problems in the queue managed by the Solution Manager.
Author and Copyright: Geir Horn, University of Oslo
Contact: [email protected]
License: MPL2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
==============================================================================*/
#include <fstream> // For file I/O
#include <sstream> // For formatted errors
#include <stdexcept> // Standard exceptions
#include <system_error> // Error codes
#include "Utility/ConsolePrint.hpp"
#include "AMPLSolver.hpp"
namespace NebulOuS
{
// -----------------------------------------------------------------------------
// Utility functions
// -----------------------------------------------------------------------------
//
// There are two situations when it is necessary to store a file from a message:
// Firstly when the AMPL model is defined, and second every time a data file
// is received updating AMPL model parameters. Hence the common file creation
// is taken care of by a dedicated function.
std::string AMPLSolver::SaveFile( std::string_view TheName,
std::string_view TheContent,
const std::source_location & Location )
{
std::string TheFileName = ProblemFileDirectory / TheName;
std::fstream TheFile( TheFileName, std::ios::out | std::ios::binary );
if( TheFile.is_open() )
{
Theron::ConsoleOutput Output;
Output << "AMPL Solver saving the file: " << TheFileName << std::endl;
TheFile << TheContent;
TheFile.close();
return TheFileName;
}
else
{
std::ostringstream ErrorMessage;
ErrorMessage << "[" << Location.file_name() << " at line "
<< Location.line()
<< "in function " << Location.function_name() <<"] "
<< "The AMPL file at "
<< TheFileName
<< " could not be opened for output!";
throw std::system_error( static_cast< int >( std::errc::io_error ),
std::system_category(), ErrorMessage.str() );
}
}
// Setting named AMPL parameters from JSON objects requires that the JSON object
// is converted to the same type as the AMPL parameter. This conversion
// requires that the type of the parameter is tested, and there is a shared
// function to set a named parameter from the JSON object.
void AMPLSolver::SetAMPLParameter( const std::string & ParameterName,
const JSON & ParameterValue )
{
ampl::Parameter
TheParameter = ProblemDefinition.getParameter( ParameterName );
switch ( ParameterValue.type() )
{
case JSON::value_t::number_integer :
case JSON::value_t::number_unsigned :
case JSON::value_t::boolean :
TheParameter.set( ParameterValue.get< long >() );
break;
case JSON::value_t::number_float :
TheParameter.set( ParameterValue.get< double >() );
break;
case JSON::value_t::string :
TheParameter.set( ParameterValue.get< std::string >() );
break;
default:
{
std::source_location Location = std::source_location::current();
std::ostringstream ErrorMessage;
ErrorMessage << "[" << Location.file_name() << " at line "
<< Location.line()
<< "in function " << Location.function_name() <<"] "
<< "The JSON value " << ParameterValue
<< " has JSON type "
<< static_cast< int >( ParameterValue.type() )
<< " which is not supported"
<< std::endl;
throw std::invalid_argument( ErrorMessage.str() );
}
break;
}
}
// -----------------------------------------------------------------------------
// Problem definition
// -----------------------------------------------------------------------------
//
// The first step in solving an optimisation problem is to define the problme
// involving the decision variables, the parameters, and the constraints over
// these entities. The AMPL Domoain Specific Language (DSL) defining the
// problem is received as a JSON message where the File Name and the File
// Content is managed by the file reader utility function.
//
// After reading the file the name of the default objective function is taken
// from the message. Not that this is a mandatory field and the solver will
// throw an exception if the field does not exist.
//
// Finally, the optimisation happens relative to the current configuration as
// baseline aiming to improve the variable values. However, this may need that
// candidate variable values are compared with the current values of the same
// variables. Hence, the current variable values are defined to be "constants"
// of the optimisation problem. These constants must be set by the solver for
// a found solution that will be deployed, and this requires a mapping between
// the name of a constant and the name of the variable used to initialise the
// constant. This map is initialised from the message, if it is provided, and
// the initial values are set for the corresponding "constant" parameters in
// the problem definition. The constant field holds a JSON map where the keys
// are the names of the constants defined as parameters in the problem
// definition, and the value is again a map with two fields: The variable name
// and the variable's intial value.
void AMPLSolver::DefineProblem(const Solver::OptimisationProblem & TheProblem,
const Address TheOracle)
{
Theron::ConsoleOutput Output;
Output << "AMPL Solver: Optimisation problem received " << std::endl
<< TheProblem.dump(2) << std::endl;
// First storing the AMPL problem file from its definition in the message
// and read the file back to the AMPL interpreter.
ProblemDefinition.read( SaveFile(
TheProblem.at(
OptimisationProblem::Keys::ProblemFile ).get< std::string >() ,
TheProblem.at(
OptimisationProblem::Keys::ProblemDescription ).get< std::string >() ) );
// The next is to read the label of the default objective function and
// store this. An invalid argument exception is thrown if the field is missing
if( TheProblem.contains(OptimisationProblem::Keys::DefaultObjectiveFunction) )
DefaultObjectiveFunction
= TheProblem.at( OptimisationProblem::Keys::DefaultObjectiveFunction );
else
{
std::source_location Location = std::source_location::current();
std::ostringstream ErrorMessage;
ErrorMessage << "[" << Location.file_name() << " at line "
<< Location.line()
<< "in function " << Location.function_name() <<"] "
<< "The problem definition must contain a default objective "
<< "function under the key ["
<< OptimisationProblem::Keys::DefaultObjectiveFunction
<< "]" << std::endl;
throw std::invalid_argument( ErrorMessage.str() );
}
// The default values for the data will be loaded from the data file. This
// operation is the same as the one done for data messages, and to avoid
// code duplication the handler is just invoked using the address of this
// solver Actor as the the sender is not important for this update. However,
// if the information is missing from the message, no data file should be
// loaded. It is necessary to convert the content to a string since the
// JSON library only sees the string and not its length before it has been
// unwrapped.
if( TheProblem.contains( DataFileMessage::Keys::DataFile ) &&
TheProblem.contains( DataFileMessage::Keys::NewData ) )
{
std::string FileContent
= TheProblem.at( DataFileMessage::Keys::NewData ).get< std::string >();
if( !FileContent.empty() )
DataFileUpdate( DataFileMessage(
TheProblem.at( DataFileMessage::Keys::DataFile ).get< std::string >(),
FileContent ),
GetAddress() );
}
// The set of constants will be processed storing the mapping from a variable
// value to a constant.
if( TheProblem.contains( OptimisationProblem::Keys::Constants ) &&
TheProblem.at( OptimisationProblem::Keys::Constants ).is_object() )
for( const auto & [ ConstantName, ConstantRecord ] :
TheProblem.at( OptimisationProblem::Keys::Constants ).items() )
{
VariablesToConstants.emplace(
ConstantRecord.at( OptimisationProblem::Keys::VariableName ),
ConstantName );
SetAMPLParameter( ConstantName,
ConstantRecord.at( OptimisationProblem::Keys::InitialConstantValue ) );
}
// Finally, the problem has been defined and the flag is set to allow
// the search for solutions for this problem.
ProblemUndefined = false;
}
// -----------------------------------------------------------------------------
// Optimimsation parameter values
// -----------------------------------------------------------------------------
//
// The data file(s) corresponding to the current optimisation problem will be
// sent in the same way and separately file by file. The logic is the same as
// the Define Problem message handler: The save file is used to store the
// received file, which is then loaded as the data problem.
void AMPLSolver::DataFileUpdate( const DataFileMessage & NewData,
const Address TheOracle )
{
ProblemDefinition.readData( SaveFile(
NewData.at( DataFileMessage::Keys::DataFile ).get< std::string >(),
NewData.at( DataFileMessage::Keys::NewData ).get< std::string >() ) );
}
// -----------------------------------------------------------------------------
// Solving
// -----------------------------------------------------------------------------
//
// The solver function is more involved as must set the metric values received
// in the application execution context message as parameter values for the
// optimisation problem, then solve for the optimal objective value, and finally
// report the solution back to the entity requesting the solution, typically an
// instance of the Solution Manager actor.
void AMPLSolver::SolveProblem(
const ApplicationExecutionContext & TheContext, const Address TheRequester )
{
Theron::ConsoleOutput Output;
Output << "AMPL Solver: Application Execution Context received. Problem Undefined = "
<< std::boolalpha << ProblemUndefined << std::endl
<< TheContext.dump(2) << std::endl;
// There is nothing to do if the application model is missing.
if( ProblemUndefined ) return;
// Setting the metric values one by one. In the setting of NebulOuS a metric
// is either a numerical value or a string. Vectors are currently not
// supported as values.
for( const auto & [ TheName, MetricValue ] :
Solver::MetricValueType( TheContext.at(
Solver::ApplicationExecutionContext::Keys::ExecutionContext ) ) )
SetAMPLParameter( TheName, MetricValue );
// Setting the given objective as the active objective and all other
// objective functions as 'dropped'. Note that this is experimental code
// as the multi-objective possibilities in AMPL are not well documented.
std::string OptimisationGoal;
if( TheContext.contains(
Solver::ApplicationExecutionContext::Keys::ObjectiveFunctionLabel ) )
OptimisationGoal = TheContext.at(
Solver::ApplicationExecutionContext::Keys::ObjectiveFunctionLabel );
else if( !DefaultObjectiveFunction.empty() )
OptimisationGoal = DefaultObjectiveFunction;
else
{
std::source_location Location = std::source_location::current();
std::ostringstream ErrorMessage;
ErrorMessage << "[" << Location.file_name() << " at line "
<< Location.line()
<< "in function " << Location.function_name() <<"] "
<< "No default objective function is defined and "
<< "the Application Execution Context message did "
<< "not define an objective function:"
<< std::endl << TheContext.dump(2)
<< std::endl;
throw std::invalid_argument( ErrorMessage.str() );
}
// The objective function name given must correspond to a function
// defined in the model, which implies that one function must be
// activated.
bool ObjectiveFunctionActivated = false;
for( auto TheObjective : ProblemDefinition.getObjectives() )
if( TheObjective.name() == OptimisationGoal )
{
TheObjective.restore();
ObjectiveFunctionActivated = true;
}
else
TheObjective.drop();
// An exception is thrown if there is no objective function activated
if( !ObjectiveFunctionActivated )
{
std::source_location Location = std::source_location::current();
std::ostringstream ErrorMessage;
ErrorMessage << "[" << Location.file_name() << " at line "
<< Location.line()
<< "in function " << Location.function_name() <<"] "
<< "The objective function label " << OptimisationGoal
<< " does not correspond to any objective function in the "
<< "model" << std::endl;
throw std::invalid_argument( ErrorMessage.str() );
}
// The problem is valid and can then be solved.
Optimize();
// Once the problem has been optimised, the objective values can be
// be obtained from the objectives
Solver::Solution::ObjectiveValuesType ObjectiveValues;
for( auto TheObjective : ProblemDefinition.getObjectives() )
ObjectiveValues.emplace( TheObjective.name(), TheObjective.value() );
// The variable values are obtained in the same way, but each variable
// is checked to see if there is a constant that has to be initialised
// with the variable value. The AMPL parameter whose name corresponds
// with the constant name mapped from the variable name, will then
// be initialised. The constant values are only to be updated if the
// application execution context has the deployment flag set.
Solver::Solution::VariableValuesType VariableValues;
bool DeploymentFlagSet
= TheContext.at( Solver::Solution::Keys::DeploymentFlag ).get<bool>();
for( auto Variable : ProblemDefinition.getVariables() )
{
VariableValues.emplace( Variable.name(), Variable.value() );
if( DeploymentFlagSet && VariablesToConstants.contains( Variable.name() ) )
SetAMPLParameter( VariablesToConstants.at( Variable.name() ),
JSON( Variable.value() ) );
}
// The found solution can then be returned to the requesting actor or topic
Send( Solver::Solution(
TheContext.at(
Solver::Solution::Keys::TimeStamp ).get< Solver::TimePointType >(),
OptimisationGoal, ObjectiveValues, VariableValues,
DeploymentFlagSet
), TheRequester );
Output << "Solver found a solution" << std::endl;
}
// -----------------------------------------------------------------------------
// Constructor and destructor
// -----------------------------------------------------------------------------
//
// The constructor initialises the base classes and sets the AMPL installation
// directory and the path for the problem related files. The message handlers
// for the data file updates must be registered since the inherited handlers
// for the application execution context and the problem definition were already
// defined by the generic solver. Note that no publisher is defined for the
// solution since the solution message is just returned to the requester actor,
// which is assumed to be a Solution Manager on the local endpoint because
// multiple solvers may run in parallel. The external publication of solutions
// will be made by the Solution Manager for all solvers on this endpoint.
AMPLSolver::AMPLSolver( const std::string & TheActorName,
const ampl::Environment & InstallationDirectory,
const std::filesystem::path & ProblemPath,
const std::string TheSolverType )
: Actor( TheActorName ),
StandardFallbackHandler( Actor::GetAddress().AsString() ),
NetworkingActor( Actor::GetAddress().AsString() ),
Solver( Actor::GetAddress().AsString() ),
ProblemFileDirectory( ProblemPath ),
ProblemDefinition( InstallationDirectory ),
ProblemUndefined( true ),
DefaultObjectiveFunction(), VariablesToConstants()
{
RegisterHandler( this, &LSolver::DataFileUpdate );
ProblemDefinition.setOption( "solver", TheSolverType );
Send( Theron::AMQ::NetworkLayer::TopicSubscription(
Theron::AMQ::NetworkLayer::TopicSubscription::Action::Subscription,
DataFileMessage::AMQTopic
), GetSessionLayerAddress() );
}
// In case the network is still running when the actor is closing, the data file
// subscription should be closed.
AMPLSolver::~AMPLSolver()
{
if( HasNetwork() )
Send( Theron::AMQ::NetworkLayer::TopicSubscription(
Theron::AMQ::NetworkLayer::TopicSubscription::Action::CloseSubscription,
DataFileMessage::AMQTopic
), GetSessionLayerAddress() );
}
} // namespace NebulOuS