Passed
Push — master ( e49ba9...b59a28 )
by Alessandro
06:09
created

compare(SearchSpaceNode,SearchSpaceNode)   A

Complexity

Conditions 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
dl 0
loc 14
rs 9.3333
c 1
b 0
f 0
1
package it.cnr.istc.pst.platinum.ai.deliberative.strategy;
2
3
import java.util.Map;
4
5
import it.cnr.istc.pst.platinum.ai.deliberative.solver.SearchSpaceNode;
6
import it.cnr.istc.pst.platinum.ai.framework.domain.component.DomainComponent;
7
8
/**
9
 * 
10
 * @author alessandro
11
 *
12
 */
13
public class StandardDeviationMinimizationSearchStrategy extends SearchStrategy {
14
	
15
	/**
16
	 * 
17
	 */
18
	protected StandardDeviationMinimizationSearchStrategy() {
19
		super("TimelineSDMinimizationSearchStrategy");
20
	}
21
	
22
	/**
23
	 * 
24
	 */
25
	@Override
26
	public void enqueue(SearchSpaceNode node) {
27
		
28
		// compute a pessimistic estimation of flaw resolution cost
29
		Map<DomainComponent, Double[]> h = this.computeHeuristicCost(node);
30
		// set heuristic estimation
31
		node.setHeuristicCost(h);
32
		// add the node to the priority queue
33
		this.fringe.offer(node);
34
	}
35
	
36
	/**
37
	 * 
38
	 */
39
	@Override
40
	public int compare(SearchSpaceNode o1, SearchSpaceNode o2) {
41
		
42
		// get plan horizon
43
		long horizon = this.pdb.getHorizon();
44
		
45
		double sd1 = this.standardDevitation(horizon, o1);
46
		double sd2 = this.standardDevitation(horizon, o2);
47
		
48
		// greedy search checking standard deviation to the horizon
49
		return o1.getDepth() > o2.getDepth() ? -1 : o1.getDepth() < o2.getDepth() ? 1 :
50
			// check standard deviations
51
			sd1 < sd2 ? -1 : sd1 > sd2 ? 1 :
52
				0;
53
	}
54
	
55
	/**
56
	 * 
57
	 * @param horizon
58
	 * @param node
59
	 * @return
60
	 */
61
	public double standardDevitation(long horizon, SearchSpaceNode node) {
62
		
63
		// initialize standard deviation with horizon
64
		double sd = horizon;
65
		
66
		// get estimated makespan of the plan
67
		Map<DomainComponent, Double[]> mk = node.getEstimatedMakespan();
68
		double N = mk.keySet().size();
69
		double quadSum = 0;
70
		for (DomainComponent comp : mk.keySet()) {
0 ignored issues
show
Performance introduced by
When you need both the keys and the value of a Map, iterating over entrySet() instead of keySet() is more readable.
Loading history...
71
			
72
			quadSum += Math.pow((mk.get(comp)[0] - horizon), 2.0);
73
		}
74
		
75
		// check data
76
		if (N > 0 && quadSum > 0) {
77
			// compute standard deviation
78
			sd = Math.sqrt(quadSum / N);
79
		}
80
		
81
		// return standard deviation
82
		return sd;
83
		
84
	}
85
	
86
}
87