Scheduler
testLinearProgrammingSolver.cpp
Go to the documentation of this file.
1 
11 
12 #include <cassert>
13 #include <iostream>
14 #include <vector>
15 
16 #include "constraintList.h"
17 #include "horizon.h"
18 #include "mdpConfiguration.h"
19 #include "policy.h"
20 #include "rewards.h"
21 #include "transitionMatrix.h"
22 
23 
24 using namespace Mdp;
25 
28 std::vector<Rewards*> generateEqualityConstraints();
29 std::vector<double> generateEqualityValues();
30 std::vector<Rewards*> generateInequalityConstraints();
31 std::vector<double> generateInequalityValues();
33 
34 const size_t S = 9;
35 const size_t A = 3;
36 
37 const double C1 = 1.0;
38 const double C2 = 200.0;
39 const double f1 = 5.0;
40 const double f2 = 8.0;
41 const double co = 0.0;
42 const double cl = C1*f1*f1 + C2;
43 const double ch = C1*f2*f2 + C2;
44 const double f3 = 2.0;
45 const double ctr = 0.0;
46 
47 const double pol = 0.5;
48 const double plo = 0.5;
49 const double poh = 0.5;
50 const double pho = 0.5;
51 const double phl = 0.5;
52 const double plh = 0.5;
53 
54 
55 
56 
57 
58 
59 
60 
61 int main()
62 {
63  Policy *policy = new Policy(S, A, nullptr);
64  Rewards *rewards = generateRewards();
65  ConstraintList *constraintList = new ConstraintList;
67  constraintList->equalityValues = generateEqualityValues();
69  constraintList->inequalityValues = generateInequalityValues();
71  Utils::Configuration *conf = new Utils::Configuration("configuration.conf");
72  Horizon *horizon = generateHorizon();
73 
74  LinearProgramming solver(conf);
75  solver.solve(policy, rewards, constraintList, matrix, horizon);
76 
77  policy->print(std::cout);
78  //matrix->print(std::cout);
79 
80  return 0;
81 }
82 
83 
84 
86 {
87  Horizon *h = new Horizon;
88  h->discountFactor = 1.0;
89  h->finiteHorizon = false;
90  std::vector<double> vect(S, 1.0/S);
91  h->initialStateDistribution = vect;
92  return h;
93 }
94 
95 
96 
98 {
99  Rewards *rewards = new Rewards(S, A);
100 
101  rewards->setReward(0, 0, -co - 0.0001);
102  rewards->setReward(0, 1, -co);
103  rewards->setReward(0, 2, -co);
104 
105  rewards->setReward(1, 0, -ctr);
106  rewards->setReward(1, 1, -ctr);
107  rewards->setReward(1, 2, -ctr);
108 
109  rewards->setReward(2, 0, -ctr);
110  rewards->setReward(2, 1, -ctr);
111  rewards->setReward(2, 2, -ctr);
112 
113  rewards->setReward(3, 0, -cl );
114  rewards->setReward(3, 1, -cl );
115  rewards->setReward(3, 2, -cl );
116 
117  rewards->setReward(4, 0, -ctr);
118  rewards->setReward(4, 1, -ctr);
119  rewards->setReward(4, 2, -ctr);
120 
121  rewards->setReward(5, 0, -ctr);
122  rewards->setReward(5, 1, -ctr);
123  rewards->setReward(5, 2, -ctr);
124 
125  rewards->setReward(6, 0, -ch );
126  rewards->setReward(6, 1, -ch );
127  rewards->setReward(6, 2, -ch );
128 
129  rewards->setReward(7, 0, -ctr);
130  rewards->setReward(7, 1, -ctr);
131  rewards->setReward(7, 2, -ctr);
132 
133  rewards->setReward(8, 0, -ctr);
134  rewards->setReward(8, 1, -ctr);
135  rewards->setReward(8, 2, -ctr);
136 
137  return rewards;
138 }
139 
140 
141 
142 
143 
144 
145 
146 
148 {
149  TransitionMatrix *matrix = new TransitionMatrix(S, A);
150 
151  for (state_t i = 0; i < S; i++)
152  {
153  for (state_t j = 0; j < S; j++)
154  {
155  for (action_t k = 0; k < A; k++)
156  {
157  matrix->set(i, j, k, 0.0);
158  }
159  }
160  }
161  matrix->set(0, 0, 0, 1.0);
162  matrix->set(0, 1, 1, 1.0);
163  matrix->set(0, 4, 2, 1.0);
164  matrix->set(1, 1, 0, pol);
165  matrix->set(1, 3, 0, 1.0-pol);
166  matrix->set(1, 1, 1, pol);
167  matrix->set(1, 3, 1, 1.0-pol);
168  matrix->set(1, 1, 2, pol);
169  matrix->set(1, 3, 2, 1.0-pol);
170  matrix->set(2, 0, 0, 1.0-plo);
171  matrix->set(2, 2, 0, plo);
172  matrix->set(2, 0, 1, 1.0-plo);
173  matrix->set(2, 2, 1, plo);
174  matrix->set(2, 0, 2, 1.0-plo);
175  matrix->set(2, 2, 2, plo);
176  matrix->set(3, 2, 0, 1.0);
177  matrix->set(3, 3, 1, 1.0);
178  matrix->set(3, 8, 2, 1.0);
179  matrix->set(4, 4, 0, poh);
180  matrix->set(4, 6, 0, 1.0-poh);
181  matrix->set(4, 4, 1, poh);
182  matrix->set(4, 6, 1, 1.0-poh);
183  matrix->set(4, 4, 2, poh);
184  matrix->set(4, 6, 2, 1.0-poh);
185  matrix->set(5, 0, 0, 1.0-pho);
186  matrix->set(5, 5, 0, pho);
187  matrix->set(5, 0, 1, 1.0-pho);
188  matrix->set(5, 5, 1, pho);
189  matrix->set(5, 0, 2, 1.0-pho);
190  matrix->set(5, 5, 2, pho);
191  matrix->set(6, 5, 0, 1.0);
192  matrix->set(6, 7, 1, 1.0);
193  matrix->set(6, 6, 2, 1.0);
194  matrix->set(7, 3, 0, 1.0-phl);
195  matrix->set(7, 7, 0, phl);
196  matrix->set(7, 3, 1, 1.0-phl);
197  matrix->set(7, 7, 1, phl);
198  matrix->set(7, 3, 2, 1.0-phl);
199  matrix->set(7, 7, 2, phl);
200  matrix->set(8, 6, 0, 1.0-plh);
201  matrix->set(8, 8, 0, plh);
202  matrix->set(8, 6, 1, 1.0-plh);
203  matrix->set(8, 8, 1, plh);
204  matrix->set(8, 6, 2, 1.0-plh);
205  matrix->set(8, 8, 2, plh);
206 
207  return matrix;
208 }
209 
210 
211 
212 
213 
214 std::vector<Rewards*> generateEqualityConstraints()
215 {
216  std::vector<Rewards*> v;
217  return v;
218 }
219 
220 
221 
222 
223 
224 
225 std::vector<double> generateEqualityValues()
226 {
227  std::vector<double> v;
228  return v;
229 }
230 
231 
232 
233 
234 
235 std::vector<Rewards*> generateInequalityConstraints()
236 {
237  std::vector<Rewards*> constraint(1);
238  constraint[0] = new Rewards(S, A);
239  for (state_t i = 0; i < S; i++)
240  {
241  for (action_t j = 0; j < A; j++)
242  {
243  constraint[0]->setReward(i, j, 0.0);
244  if (i == 3)
245  constraint[0]->setReward(i, j, f1);
246  if (i == 6)
247  constraint[0]->setReward(i, j, f2);
248  }
249  }
250 
251  for (state_t i = 0; i < S; i++)
252  {
253  for (action_t j = 0; j<A; j++)
254  {
255  constraint[0]->setReward(i, j, -constraint[0]->getReward(i, j));
256  }
257  }
258 
259 
260  return constraint;
261 }
262 
263 
264 
265 
266 std::vector<double> generateInequalityValues()
267 {
268  std::vector<double> value(1, f3);
269  value[0] = -value[0];
270  return value;
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 
std::vector< Rewards * > equalityConstraints
const double plo
const size_t A
const size_t S
void set(state_t from, state_t to, action_t action, double proba)
std::vector< double > equalityValues
Horizon * generateHorizon()
std::vector< double > inequalityValues
const double phl
const double C2
const double cl
const double f1
std::vector< Rewards * > generateInequalityConstraints()
bool finiteHorizon
Definition: horizon.h:19
void setReward(state_t state, action_t action, double reward)
Definition: rewards.cpp:28
const double ch
std::vector< double > generateEqualityValues()
const double pho
size_t action_t
Definition: action_impl.h:18
const double C1
Definition: action.h:18
const double poh
const double co
const double plh
Rewards * generateRewards()
const double pol
TransitionMatrix * generateTransitionMatrix()
std::vector< Rewards * > generateEqualityConstraints()
std::vector< double > initialStateDistribution
Definition: horizon.h:21
const double f2
double discountFactor
Definition: horizon.h:20
size_t state_t
Definition: state.h:19
const double ctr
std::vector< Rewards * > inequalityConstraints
void print(std::ostream &stream)
Definition: policy.cpp:132
std::vector< double > generateInequalityValues()
void solve(Policy *policy, Rewards *rewards, ConstraintList *constraintList, TransitionMatrix *matrix, Horizon *horizon)
const double f3