-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathc_simulatedAnnealing.js
More file actions
119 lines (100 loc) · 3.26 KB
/
c_simulatedAnnealing.js
File metadata and controls
119 lines (100 loc) · 3.26 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
$(document).ready(function() {
//Wrapper class for the diagram
class SimulatedAnnealingDiagram extends HillWorld {
constructor(selector, h, w, sliderSelector) {
super(selector, h, w);
this.sliderElement = $(sliderSelector);
}
init(delay, k) {
this.delay = delay;
this.initial = this.hillClimber.getCurrentState();
this.simulatedAnnealing = new SimulatedAnnealing(this.hill, this.initial, k);
this.colorScale = d3.scaleLinear().domain([0, 50]).range([70, 30]).clamp(true);
this.showAllStates();
this.bindListeners();
this.paintGlobalMaxima();
this.showTemperature(100);
this.sliderElement.val(100);
this.startAnnealing();
}
showAllStates() {
this.hillDiagram.svgRects.transition()
.duration(200)
.style('opacity', 1);
}
paintGlobalMaxima() {
this.hillDiagram.svgRects.classed('hill-maxima', (d) => d.maxima);
}
teleportRobot(currentState) {
let robotLocation = currentState;
let robotStateValue = this.hill.getStates()[currentState];
this.hillClimberDiagram.robot
.attr('x', this.hillClimberDiagram.xScale(robotLocation) - this.hillClimberDiagram.xOffset)
.attr('y', this.h - this.hillClimberDiagram.yScale(robotStateValue) - this.hillClimberDiagram.yOffset);
}
updateAnnealRegion(currentState) {
this.hillDiagram.svgRects.classed('hill-anneal-current-state', d => d.state == currentState);
}
showTemperature(t) {
this.svgTemp = this.hillDiagram.svg.append('text')
.attr('x', this.w - 180)
.attr('y', this.h - 470)
//Displaying temperature with 2 digit precision
.text(`Temperature : ${Math.round(t*100)/100}`);
}
updateTemperature(t) {
this.svgTemp.text(`Temperature : ${Math.round(t*100)/100}`);
}
nextMove(temp) {
let nextNode = this.simulatedAnnealing.anneal(temp);
if (nextNode.temp <= 0) {
this.updateTemperature(0);
return false;
} else {
let nextState = nextNode.state;
this.hillClimber.changeState(nextState);
this.teleportRobot(nextState);
this.updateAnnealRegion(nextState);
return true;
}
}
startAnnealing() {
//Stop annealing if already running.
this.stopAnnealing();
this.annealIntervalFunction = setInterval(() => {
if (!this.nextMove(this.sliderElement.val())) {
this.stopAnnealing();
}
}, this.delay);
}
stopAnnealing() {
clearInterval(this.annealIntervalFunction, this.delay);
}
destroy() {
this.stopAnnealing();
}
bindListeners() {
this.sliderElement.mouseup(() => {
if (this.sliderElement.val() > 0) {
this.startAnnealing();
}
});
this.sliderElement.mousemove(() => {
this.updateTemperature(this.sliderElement.val())
})
}
}
var simulatedAnnealingDiagram;
function init() {
simulatedAnnealingDiagram = new SimulatedAnnealingDiagram('#annealingCanvas', 500, 1000, '#tempSelector');
let delay = 20;
let k = 0.3;
simulatedAnnealingDiagram.init(delay, k);
};
function restart() {
simulatedAnnealingDiagram.destroy();
init();
}
init();
$('#annealingRestart').click(restart);
});