1 | package dk.deepthought.sidious.rules; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Arrays; |
5 | import java.util.Collection; |
6 | |
7 | import dk.deepthought.sidious.goalhandler.Goal; |
8 | import dk.deepthought.sidious.greenhouse.ClimaticState; |
9 | import dk.deepthought.sidious.greenhouse.SensorInput; |
10 | import dk.deepthought.sidious.supportsystem.State; |
11 | import dk.deepthought.sidious.supportsystem.Step; |
12 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
13 | |
14 | /** |
15 | * Rule to keep conventionally unconstrained sensors constrained. I.e. adds cost |
16 | * to otherwise "free" adjustments. |
17 | * |
18 | * @author Deepthought |
19 | * |
20 | */ |
21 | public class ConstrainingRule extends Rule { |
22 | |
23 | /** |
24 | * A list of sensor id's which will be constrained by this. |
25 | */ |
26 | private Collection<SuperLinkID> idsToConstrain; |
27 | |
28 | /** |
29 | * Constructs a constraining rule that constrains sensors with input IDs. |
30 | * |
31 | * @param sensorIDs |
32 | * the ids of the sensors to constrain |
33 | */ |
34 | public ConstrainingRule(SuperLinkID... sensorIDs) { |
35 | idsToConstrain = Arrays.asList(sensorIDs); |
36 | } |
37 | |
38 | /* |
39 | * (non-Javadoc) |
40 | * |
41 | * @see dk.deepthought.sidious.rules.Rule#desire(dk.deepthought.sidious.supportsystem.State, |
42 | * dk.deepthought.sidious.supportsystem.State, dk.deepthought.sidious.supportsystem.Step) |
43 | */ |
44 | @Override |
45 | public double desire(State currentState, State newState, Step step) { |
46 | ClimaticState currentClima = null; |
47 | ClimaticState newClima = null; |
48 | if (currentState instanceof ClimaticState) { |
49 | currentClima = (ClimaticState) currentState; |
50 | } |
51 | if (newState instanceof ClimaticState) { |
52 | newClima = (ClimaticState) newState; |
53 | } |
54 | if (currentClima == null || newClima == null) { |
55 | throw new IllegalArgumentException( |
56 | "Only climaticStates allowed as parameters!"); |
57 | } |
58 | double returnValue = 0; |
59 | for (SuperLinkID id : idsToConstrain) { |
60 | SensorInput currentSensor = null; |
61 | SensorInput newSensor = null; |
62 | for (SensorInput sensor : currentClima.getSensors()) { |
63 | if (sensor.getID().equals(id)) { |
64 | currentSensor = sensor; |
65 | break; |
66 | } |
67 | } |
68 | for (SensorInput sensor : newClima.getSensors()) { |
69 | if (sensor.getID().equals(id)) { |
70 | newSensor = sensor; |
71 | break; |
72 | } |
73 | } |
74 | returnValue += normalizedDistance(currentSensor, newSensor); |
75 | } |
76 | return returnValue; |
77 | } |
78 | |
79 | /* |
80 | * (non-Javadoc) |
81 | * |
82 | * @see dk.deepthought.sidious.rules.Rule#getGoals() |
83 | */ |
84 | @Override |
85 | public Collection<Goal> getGoals() { |
86 | return new ArrayList<Goal>(); |
87 | } |
88 | |
89 | /** |
90 | * Calculates the normalized distance between two sensors. |
91 | * <p> |
92 | * It takes two sensors and normalizes the distance between them to a value |
93 | * between 0 and 1. |
94 | * |
95 | * @param currentSensor |
96 | * the first sensor |
97 | * @param newSensor |
98 | * the second sensor |
99 | * @return the normalized value of the distance. |
100 | */ |
101 | private double normalizedDistance(SensorInput currentSensor, |
102 | SensorInput newSensor) { |
103 | double newValue = newSensor == null ? 0 : newSensor.getValue(); |
104 | double currentValue = currentSensor == null ? 0 : currentSensor |
105 | .getValue(); |
106 | double normalizer; |
107 | double distance = Math.abs(newValue - currentValue); |
108 | // Normalizes to log 10 value |
109 | if (Math.abs(newValue) > 0 && Math.abs(currentValue) > 0 |
110 | && Math.abs(distance) > 0) { |
111 | double flooredNorm = Math.floor(Math.min(Math.log10(distance), Math |
112 | .min(Math.log10(newValue), Math.log10(currentValue)))); |
113 | normalizer = flooredNorm > 0 ? flooredNorm : 0; |
114 | } else { |
115 | normalizer = 0; |
116 | } |
117 | if (normalizer >= 0) { |
118 | normalizer += 1; |
119 | double div = Math.pow(10, normalizer); |
120 | newValue /= div; |
121 | currentValue /= div; |
122 | distance = Math.abs(newValue - currentValue); |
123 | } |
124 | if (distance > 1) { |
125 | distance = 1; |
126 | } |
127 | return distance; |
128 | } |
129 | |
130 | } |