-
Notifications
You must be signed in to change notification settings - Fork 6
/
Lab.java
427 lines (349 loc) · 14.2 KB
/
Lab.java
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
425
426
427
package tools;
import java.io.IOException;
import java.util.*;
import java.util.logging.*;
import com.google.common.collect.Sets;
import ch.unisg.ics.interactions.wot.td.ThingDescription;
import ch.unisg.ics.interactions.wot.td.ThingDescription.TDFormat;
import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance;
import ch.unisg.ics.interactions.wot.td.affordances.Form;
import ch.unisg.ics.interactions.wot.td.affordances.PropertyAffordance;
import ch.unisg.ics.interactions.wot.td.clients.TDHttpRequest;
import ch.unisg.ics.interactions.wot.td.clients.TDHttpResponse;
import ch.unisg.ics.interactions.wot.td.io.TDGraphReader;
import ch.unisg.ics.interactions.wot.td.schemas.DataSchema;
import ch.unisg.ics.interactions.wot.td.schemas.BooleanSchema;
import ch.unisg.ics.interactions.wot.td.schemas.ObjectSchema;
import ch.unisg.ics.interactions.wot.td.vocabularies.TD;
/**
* An class that extends {@link LearningEnvironment} for representing a learning
* environment which can be used for Q learning.
*
* <p> A Lab instance manages the state space and the action space of
* environments that are similar to the lab of the Interactions group
* (simulated or real environments), given the W3C Web of Things Thing
* Description of the lab environment.
* </p>
*/
public class Lab extends LearningEnvironment {
/**
* The W3C Web of Things Thing Description used for interacting with the
* lab environment
*/
private ThingDescription td;
/**
* The current state of the lab (and of every state of the lab) is formed as a
* a list of 7 integer values: [z1Level, z2Level, z1Light, z2Light, z1Blinds, z2Blinds, sunshine]:
* <ul>
* <li> z1Level: the level of light in Zone 1
* <li> z2Level: the level of light in Zone 2
* <li> z1Light: the status of the lights in Zone 1
* <li> z2Light: the status of the lights in Zone 2
* <li> z1Blinds: the status of the blinds in Zone 1
* <li> z2Blinds: the status of the blinds in Zone 2
* <li> sunshine: the level of sunshine out of the lab
* </ul>
*/
protected List<Integer> currentState = Arrays.asList(new Integer[7]);
/**
* The state of the lab depends on the values of
* z1Level, z2Level, z1Light, z2Light, z1Blinds, z2Blinds, sunshine
*/
/**
* z1Level: the level of light in Zone 1
* Possible values: 0,1,2,3
* Respective keys: 0,1,2,3
*/
private static final HashMap<Integer,Integer> z1Level = new HashMap<>();
/**
* z2Level: the level of light in Zone 2
* Possible values: 0,1,2,3
* Respective keys: 0,1,2,3
*/
private static final HashMap<Integer,Integer> z2Level = new HashMap<>();
/**
* z1Light: the status of the lights in Zone 1
* Possible values: false, true
* Respective keys: 0,1
*/
private static final HashMap<Integer,Boolean> z1Light = new HashMap<>();
/**
* z2Light: the status of the lights in Zone 2
* Possible values: false, true
* Respective keys: 0,1
*/
private static final HashMap<Integer,Boolean> z2Light = new HashMap<>();
/**
* z1Blinds: the status of the blinds in Zone 1
* Possible values: false, true
* Respective keys: 0,1
*/
private static final HashMap<Integer,Boolean> z1Blinds = new HashMap<>();
/**
* z2Blinds: the status of the blinds in Zone 2
* Possible values: false, true
* Respective keys: 0,1
*/
private static final HashMap<Integer,Boolean> z2Blinds = new HashMap<>();
/**
* sunshine: the level of sunshine out of the lab
* Possible values: 0,1,2,3
* Respective keys: 0,1,2,3
*/
private static final HashMap<Integer,Integer> sunshine = new HashMap<>();
private static final Logger LOGGER = Logger.getLogger(Lab.class.getName());
static {
// possible substates for z1Level, z2Level, sunshine
for (int i=0; i<4; i++) {
z1Level.put(i,i);
z2Level.put(i,i);
sunshine.put(i,i);
}
// possible substates for z1Light
z1Light.put(0, false);
z1Light.put(1, true);
// possible substates for z2Light
z2Light.put(0, false);
z2Light.put(1, true);
// possible substates for z1Blinds
z1Blinds.put(0, false);
z1Blinds.put(1, true);
// possible substates for z2Blinds
z2Blinds.put(0, false);
z2Blinds.put(1, true);
};
/**
* A {@link Lab} instance is constructed based on the URL of the W3C Web of Things
* Thing Description of a lab (simulated or real)
*
* @param url The location of the W3C Web of Things Thing Description
*/
public Lab(String url) {
try {
// Read the Thing Description from the URL
this.td = TDGraphReader.readFromURL(TDFormat.RDF_TURTLE, url);
// Create the state space
createStateSpace();
LOGGER.info("The lab has a state space of n="+ stateSpace.size());
// Print the states of the state space
LOGGER.info(String.valueOf(stateSpace));
// Create the action space
createActionSpace();
LOGGER.info("The lab has an action space of m=" + actionSpace.size());
// Print the actions of the action space
for (Action action : actionSpace.values()) {
LOGGER.info(action.toString());
}
// Read the current state
readCurrentState();
LOGGER.info("The lab current state: " + this.currentState);
} catch (IOException e) {
LOGGER.severe(e.getMessage());
}
}
/**
* @see {@link LearningEnvironment#getCompatibleStates(List)}
*/
@Override
public List<Integer> getCompatibleStates(List<Object> stateDescription) {
List<Integer> compatibleStates = new ArrayList<>();
List<List<Integer>> stateList = new ArrayList<>(stateSpace);
for (int i=0; i<stateList.size(); i++) {
List<Integer> state = stateList.get(i);
List<Object> substates = new ArrayList<>();
substates.add(Lab.z1Level.get(state.get(0)));
substates.add(Lab.z2Level.get(state.get(1)));
substates.add(Lab.z1Light.get(state.get(2)));
substates.add(Lab.z2Light.get(state.get(3)));
substates.add(Lab.z1Blinds.get(state.get(4)));
substates.add(Lab.z2Blinds.get(state.get(5)));
substates.add(Lab.sunshine.get(state.get(6)));
if (Collections.indexOfSubList(substates, stateDescription) != -1){
compatibleStates.add(i);
System.out.println(state);
};
}
return compatibleStates;
}
/**
* @see {@link LearningEnvironment#readCurrentState()}
*/
@Override
public int readCurrentState() {
Optional<PropertyAffordance> p = this.td.getFirstPropertyBySemanticType("https://example.org/was#Status");
if (p.isPresent()) {
Optional<Form> f = p.get().getFirstFormForOperationType(TD.readProperty);
DataSchema ds = p.get().getDataSchema();
if (f.isPresent()) {
TDHttpRequest request = new TDHttpRequest(f.get(), TD.readProperty);
try {
TDHttpResponse response = request.execute();
Map<String, Object> status = response.getPayloadAsObject((ObjectSchema) ds);
int z1Level = discretizeLightLevel((Double) status.get("http://example.org/was#Z1Level"));
int z2Level = discretizeLightLevel((Double) status.get("http://example.org/was#Z2Level"));
boolean z1Light = (Boolean) status.get("http://example.org/was#Z1Light");
boolean z2Light = (Boolean) status.get("http://example.org/was#Z2Light");
boolean z1Blinds = (Boolean) status.get("http://example.org/was#Z1Blinds");
boolean z2Blinds = (Boolean) status.get("http://example.org/was#Z2Blinds");
int sunshine = discretizeSunshine((Double) status.get("http://example.org/was#Sunshine"));
currentState.set(0, z1Level);
currentState.set(1, z2Level);
currentState.set(2, z1Light ? 1 : 0);
currentState.set(3, z2Light ? 1 : 0);
currentState.set(4, z1Blinds ? 1 : 0);
currentState.set(5, z2Blinds ? 1 : 0);
currentState.set(6, sunshine);
} catch (IOException e) {
LOGGER.severe(e.getMessage());
}
}
}
List<List<Integer>> stateList = new ArrayList<>(stateSpace);
return stateList.indexOf(this.currentState);
}
/**
* @see {@link LearningEnvironment#getApplicableActions(int)}
*/
@Override
public List<Integer> getApplicableActions(int state) {
List<Integer> applicableActions = new ArrayList<>();
List<List<Integer>> stateList = new ArrayList<>(stateSpace);
List<Integer> st = stateList.get(state);
for (int action : actionSpace.keySet()) {
Action a = actionSpace.get(action);
int stateAxis = a.getApplicableOnStateAxis();
int stateValue = a.getApplicableOnStateValue();
if (st.get(stateAxis) == stateValue) {
applicableActions.add(action);
}
}
return applicableActions;
}
/**
* @see {@link LearningEnvironment#performAction(int)}
*/
@Override
public void performAction(int action) {
Action a = actionSpace.get(action);
try {
a.getRequest().execute();
LOGGER.info(a.getRequest().toString());
} catch (IOException e) {
LOGGER.severe(e.getMessage());
}
}
/**
* Creates the action space of the lab
*/
private void createActionSpace() {
this.affordanceTypes = Arrays.asList(
"http://example.org/was#SetZ1Light",
"http://example.org/was#SetZ2Light",
"http://example.org/was#SetZ1Blinds",
"http://example.org/was#SetZ2Blinds"
);
for (String affType : affordanceTypes) {
Optional<ActionAffordance> a = this.td.getFirstActionBySemanticType(affType);
if (a.isPresent()) {
Optional<Form> f = a.get().getFirstFormForOperationType(TD.invokeAction);
Optional<DataSchema> ds = a.get().getInputSchema();
if (f.isPresent() && ds.isPresent()) {
Map<String, DataSchema> props = ((ObjectSchema) ds.get()).getProperties();
Map<String, Object> payload = new HashMap<>();
for (String propName : props.keySet()) {
DataSchema propDs = props.get(propName);
if (propDs instanceof BooleanSchema) {
for (boolean propValue : Arrays.asList(false, true)) {
payload.put(propName, propValue);
TDHttpRequest request = new TDHttpRequest(f.get(), TD.invokeAction);
request.setObjectPayload((ObjectSchema) ds.get(), payload);
Action action = new Action(affType, new Object[]{propName}, new Object[]{propValue}, request);
actionSpace.put(actionSpace.size(), action);
}
}
}
}
}
}
setApplicableActions();
}
/**
* Maps lux values to light levels:
* lux < 50 -> level 0
* lux in [50,100) -> level 1
* lux in [100,300) -> level 2
* lux >= 300 -> level 3
*/
private int discretizeLightLevel(Double value) {
if (value < 50) {
return 0;
} else if (value < 100) {
return 1;
} else if (value < 300) {
return 2;
}
return 3;
}
/**
* Maps lux values to light levels:
* lux < 50 -> level 0
* lux in [50,200) -> level 1
* lux in [200,700) -> level 2
* lux >= 700 -> level 3
*/
private int discretizeSunshine(Double value) {
if (value < 50) {
return 0;
} else if (value < 200) {
return 1;
} else if (value < 700) {
return 2;
}
return 3;
}
/**
* Creates the state space of the lab
*/
private void createStateSpace() {
this.stateSpace = Sets.cartesianProduct(
Collections.unmodifiableSet(z1Level.keySet()),
Collections.unmodifiableSet(z2Level.keySet()),
Collections.unmodifiableSet(z1Light.keySet()),
Collections.unmodifiableSet(z2Light.keySet()),
Collections.unmodifiableSet(z1Blinds.keySet()),
Collections.unmodifiableSet(z2Blinds.keySet()),
Collections.unmodifiableSet(sunshine.keySet())
);
}
/**
* Returns the action that is applicable based on a given substate
*/
private Action getApplicableAction(String stateAxis, Boolean stateValue) {
return actionSpace.values()
.stream().filter( v ->
stateAxis.equals(v.getActionTag()) &&
Arrays.asList(v.getPayload()).contains(stateValue))
.findFirst().get();
}
/**
* Set the applicable actions for each substate
*/
private void setApplicableActions() {
Action z1LightOnValidAction = getApplicableAction("http://example.org/was#SetZ1Light", true);
Action z1LightOffValidAction = getApplicableAction("http://example.org/was#SetZ1Light", false);
Action z2LightOnValidAction = getApplicableAction("http://example.org/was#SetZ2Light", true);
Action z2LightOffValidAction = getApplicableAction("http://example.org/was#SetZ2Light", false);
Action z1BlindsUpValidAction = getApplicableAction("http://example.org/was#SetZ1Blinds", true);
Action z1BlindsDownValidAction = getApplicableAction("http://example.org/was#SetZ1Blinds", false);
Action z2BlindsUpValidAction = getApplicableAction("http://example.org/was#SetZ2Blinds", true);
Action z2BlindsDownValidAction = getApplicableAction("http://example.org/was#SetZ2Blinds", false);
z1LightOnValidAction.setApplicableOn(2, 0);
z1LightOffValidAction.setApplicableOn(2, 1);
z2LightOnValidAction.setApplicableOn(3, 0);
z2LightOffValidAction.setApplicableOn(3, 1);
z1BlindsUpValidAction.setApplicableOn(4, 0);
z1BlindsDownValidAction.setApplicableOn(4, 1);
z2BlindsUpValidAction.setApplicableOn(5, 0);
z2BlindsDownValidAction.setApplicableOn(5, 1);
}
}