1 |
#include "config.h" |
2 |
#include "solverhooks.h" |
3 |
#include "simulation.h" |
4 |
#include "solver.h" |
5 |
#include "solverparameters.h" |
6 |
#include "solverreporter.h" |
7 |
#include "value.h" |
8 |
|
9 |
#include <stdexcept> |
10 |
#include <string> |
11 |
|
12 |
extern "C"{ |
13 |
#include <ascend/utilities/error.h> |
14 |
}; |
15 |
|
16 |
#define SOLVERHOOKS_DEBUG 0 |
17 |
|
18 |
//------------------------------------------------------------------------------ |
19 |
// C-level functions that SolverHooks can pass back to libascend |
20 |
|
21 |
int ascxx_slvreq_set_solver(const char *solvername, void *user_data){ |
22 |
Simulation *S = (Simulation *)user_data; |
23 |
if(NULL==S->getSolverHooks())return SLVREQ_SOLVER_HOOK_NOT_SET; |
24 |
#if SOLVERHOOKS_DEBUG |
25 |
CONSOLE_DEBUG("Got solver hooks at %p from Simulation at %p",S->getSolverHooks(),S); |
26 |
#endif |
27 |
return S->getSolverHooks()->setSolver(solvername, S); |
28 |
} |
29 |
|
30 |
int ascxx_slvreq_set_option(const char *optionname, value_t *val, void *user_data){ |
31 |
Simulation *S = (Simulation *)user_data; |
32 |
if(NULL==S->getSolverHooks())return SLVREQ_OPTION_HOOK_NOT_SET; |
33 |
return S->getSolverHooks()->setOption(optionname, Value(val), S); |
34 |
} |
35 |
|
36 |
int ascxx_slvreq_do_solve(struct Instance *instance, void *user_data){ |
37 |
Simulation *S = (Simulation *)user_data; |
38 |
if(NULL==S->getSolverHooks())return SLVREQ_SOLVE_HOOK_NOT_SET; |
39 |
return S->getSolverHooks()->doSolve(instance, S); |
40 |
} |
41 |
|
42 |
|
43 |
//------------------------------------------------------------------------------ |
44 |
// SOLVER HOOKS (C++ layer implementation) |
45 |
|
46 |
SolverHooks::SolverHooks(SolverReporter *R) : R(R){ |
47 |
#if SOLVERHOOKS_DEBUG |
48 |
CONSOLE_DEBUG("Creating SolverHooks at %p",this); |
49 |
#endif |
50 |
// nothing else to do |
51 |
} |
52 |
|
53 |
SolverHooks::~SolverHooks(){ |
54 |
/* nothing that we own that we need to destroy? */ |
55 |
} |
56 |
|
57 |
SolverHooks::SolverHooks(SolverHooks &old) : R(old.R){ |
58 |
#if SOLVERHOOKS_DEBUG |
59 |
CONSOLE_DEBUG("Creating new SolverHooks at %p (copy of old at %p",this,&old); |
60 |
#endif |
61 |
} |
62 |
|
63 |
int |
64 |
SolverHooks::setSolver(const char *solvername, Simulation *S){ |
65 |
/* note desired return codes from slvreq.h */ |
66 |
try{ |
67 |
Solver solver(solvername); |
68 |
S->build(); |
69 |
S->setSolver(solver); |
70 |
}catch(std::runtime_error *E){ |
71 |
return SLVREQ_UNKNOWN_SOLVER; |
72 |
} |
73 |
CONSOLE_DEBUG("Solver set to '%s'",solvername); |
74 |
return 0; |
75 |
} |
76 |
|
77 |
int |
78 |
SolverHooks::setOption(const char *optionname, Value val, Simulation *S){ |
79 |
/* FIXME need to check if the system is built? */ |
80 |
/* FIXME check if we have got a solver assigned? */ |
81 |
SolverParameters pp = S->getParameters(); |
82 |
|
83 |
try{ |
84 |
SolverParameter p = pp.getParameter(optionname); |
85 |
try{ |
86 |
p.setValueValue(val); |
87 |
}catch(std::runtime_error E){ |
88 |
return SLVREQ_WRONG_OPTION_VALUE_TYPE; |
89 |
} |
90 |
}catch(std::runtime_error E){ |
91 |
return SLVREQ_INVALID_OPTION_NAME; |
92 |
} |
93 |
return 0; |
94 |
} |
95 |
|
96 |
int |
97 |
SolverHooks::doSolve(Instance *i, Simulation *S){ |
98 |
CONSOLE_DEBUG("Solving model..."); |
99 |
|
100 |
try{ |
101 |
/* FIXME do solving of a particular instance? */ |
102 |
if(!getSolverReporter()){ |
103 |
CONSOLE_DEBUG("Creating default SolverReporter"); |
104 |
SolverReporter R; |
105 |
S->solve(S->getSolver(), R); |
106 |
}else{ |
107 |
CONSOLE_DEBUG("Using SolverReporter at %p",getSolverReporter()); |
108 |
S->solve(S->getSolver(), *getSolverReporter()); |
109 |
} |
110 |
}catch(std::runtime_error E){ |
111 |
return SLVREQ_SOLVE_FAIL; |
112 |
} |
113 |
|
114 |
/* solver succeeded */ |
115 |
return 0; |
116 |
} |
117 |
|
118 |
void |
119 |
SolverHooks::assign(Simulation *S){ |
120 |
S->setSolverHooks(this); |
121 |
#if SOLVERHOOKS_DEBUG |
122 |
CONSOLE_DEBUG("Assigning SolverHooks to Simulation..."); |
123 |
#endif |
124 |
slvreq_assign_hooks(S->getInternalType(),&ascxx_slvreq_set_solver, &ascxx_slvreq_set_option, &ascxx_slvreq_do_solve, (void *)S); |
125 |
} |
126 |
|
127 |
SolverReporter * |
128 |
SolverHooks::getSolverReporter(){ |
129 |
#if SOLVERHOOKS_DEBUG |
130 |
CONSOLE_DEBUG("SolverReporter is at %p", R); |
131 |
#endif |
132 |
return R; |
133 |
} |
134 |
|
135 |
//------------------------------------------------------------------------------ |
136 |
// SOLVER HOOKS (Python layer implementation) |
137 |
|
138 |
#if 0 |
139 |
class SolverHooksPython{ |
140 |
private: |
141 |
PyObject *set_solver_py; |
142 |
PyObject *set_param_py; |
143 |
PyObject *do_solve_py; |
144 |
PyObject *context_py; |
145 |
public: |
146 |
SolverHooksPython(PyObject *set_solver_fn, PyObject *set_param_fn, PyObject *do_solve_fn, PyObject *context); |
147 |
virtual int setSolver(const char *solvername, Simulation *S); |
148 |
virtual int setOption(const char *optionname, const char *val, Simulation *S); |
149 |
virtual int doSolve(Instance *i, Simulation *S); |
150 |
}; |
151 |
#endif |
152 |
|
153 |
//------------------------------------------------------------------------------ |
154 |
// SOLVER HOOKS MANAGER (singleton) |
155 |
|
156 |
SolverHooksManager::SolverHooksManager(){ |
157 |
#if SOLVERHOOKS_DEBUG |
158 |
CONSOLE_DEBUG("Creating SolverHooksManager with NULL hooks"); |
159 |
#endif |
160 |
this->hooks = NULL; |
161 |
this->own_hooks = 0; |
162 |
} |
163 |
|
164 |
SolverHooksManager *SolverHooksManager::_instance; |
165 |
|
166 |
SolverHooksManager * |
167 |
SolverHooksManager::Instance(){ |
168 |
if(_instance==0){ |
169 |
_instance = new SolverHooksManager(); |
170 |
} |
171 |
return _instance; |
172 |
} |
173 |
|
174 |
SolverHooksManager::~SolverHooksManager(){ |
175 |
if(own_hooks){ |
176 |
#if SOLVERHOOKS_DEBUG |
177 |
CONSOLE_DEBUG("Delete owned hooks"); |
178 |
#endif |
179 |
delete hooks; |
180 |
} |
181 |
} |
182 |
|
183 |
void |
184 |
SolverHooksManager::setHooks(SolverHooks *H){ |
185 |
#if SOLVERHOOKS_DEBUG |
186 |
CONSOLE_DEBUG("Using hooks at %p",H); |
187 |
#endif |
188 |
if(hooks && own_hooks){ |
189 |
#if SOLVERHOOKS_DEBUG |
190 |
CONSOLE_DEBUG("Deleting previous owned hooks"); |
191 |
#endif |
192 |
delete(hooks); |
193 |
} |
194 |
this->hooks = H; |
195 |
this->own_hooks = 0; |
196 |
} |
197 |
|
198 |
SolverHooks * |
199 |
SolverHooksManager::getHooks(){ |
200 |
if(this->hooks == NULL){ |
201 |
#if SOLVERHOOKS_DEBUG |
202 |
CONSOLE_DEBUG("Creating new default SolverHooks..."); |
203 |
#endif |
204 |
this->hooks = new SolverHooks(); |
205 |
this->own_hooks = 1; |
206 |
} |
207 |
return this->hooks; |
208 |
} |
209 |
|
210 |
|
211 |
|