-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVSH.java
More file actions
127 lines (106 loc) · 3.72 KB
/
VSH.java
File metadata and controls
127 lines (106 loc) · 3.72 KB
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
////////////////////////////////////////////////////////////
/////
///// A Visualisation Tool for
///// Selection Hyper-Heuristics
/////
///// http://code.google.com/p/vch/
/////
///// Group: gp09-exo
///// FullNames: Thomas Barton (txb18u)
///// Zhang Chao (cxz09u)
///// Ben Jenkinson (bxj08u)
///// Alexander Jermstad (asj08u)
///// Lao Jingqi (jxl29u)
/////
///// Module: G52GRP, University of Nottingham
/////
////////////////////////////////////////////////////////////
import java.util.Random;
public class VSH {
HyperHeuristic hyperHeuristic;
VSHMainFrame frame;
HyperHeuristicBuilder HHBuilder;
HyperHeuristicDirector HHDirector;
Random random = new Random();
int candidateSolution [] = new int[HyperHeuristic.DIGIT_NUM];
int newSolution[] = new int[candidateSolution.length];
LowLevelHeuristic lowLevelHeuristic;
int count = 0;
int [][] history = new int[10000][15];
int bestSolution[] = new int[HyperHeuristic.DIGIT_NUM];
int sleepTime = 1;
String functionNmae = "f(x)=x^2", acceptanceMethodName = "Improving or Equal", heuristicSelectionName = "Simple Random";
String [] lowLevelHeuristicNames = {"Reverse","Inverse","Shift","Flip One Bit","Steepest Gradient"};
public static void main(String[] args){
new VSH().start();
}
void start(){
frame =new VSHMainFrame("VSH",this);
for(;;){
if(!frame.start){
frame.stop();
}
count++;
calculate();
while(!frame.panel.m_panel.animationPanel.animationFinished){
if(frame.pause){
frame.stop();
}
if(frame.stop){
frame.panel.m_panel.animationPanel.reset();
break;
}
exhibition();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
acceptanceCheck();
}
}
void buildHyperHeuristic(){
// Print out all the low-level heuristics.
//System.Out.println("Available LowLevelHeuristics:");
for(int i=0;i<lowLevelHeuristicNames.length;i++){
//System.Out.println( " " + lowLevelHeuristicNames[i] );
}
HHBuilder= new HyperHeuristicBuilder();
HHDirector = new HyperHeuristicDirector(HHBuilder);
HHDirector.construct(functionNmae, acceptanceMethodName, lowLevelHeuristicNames, heuristicSelectionName);
hyperHeuristic = HHBuilder.GetHyperHeuristic();
for(int i =0; i<candidateSolution.length;i++){
candidateSolution[i] = random.nextInt(2);
history[0][i]=candidateSolution[i];
bestSolution[i]=candidateSolution[i];
}
}
void calculate(){
lowLevelHeuristic = hyperHeuristic.heuristicsSelection.selectLowLevelHeuristic(candidateSolution);
//System.Out.println( "calculate->" + lowLevelHeuristic.getName() );
if(hyperHeuristic.heuristicsSelection.getName().equals("Greedy Random")){
newSolution = ((GreedyRandom)hyperHeuristic.heuristicsSelection).optimumSolution;
}else{
newSolution= lowLevelHeuristic.generateNewSolution(candidateSolution);
}
for(int i=0;i<newSolution.length;i++){
history[count][i] = newSolution[i];
}
frame.panel.m_panel.animationPanel.animationFinished = false;
}
void exhibition(){
frame.panel.m_panel.animationPanel.repaint();
}
void acceptanceCheck(){
if(hyperHeuristic.acceptanceMethod.checkIfAcceptance(candidateSolution, newSolution)){
candidateSolution = newSolution;
if(hyperHeuristic.heuristicsSelection.getName().equals("Reinforcement Learning"))
((ReinforcementLearning)hyperHeuristic.heuristicsSelection).incrementScore();
}else{
if(hyperHeuristic.heuristicsSelection.getName().equals("Reinforcement Learning"))
((ReinforcementLearning)hyperHeuristic.heuristicsSelection).decrementScore();
}
}
}