-
Notifications
You must be signed in to change notification settings - Fork 11
/
simulatedAnnealing.js
46 lines (34 loc) · 1.21 KB
/
simulatedAnnealing.js
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
var knapsack = {items: [], maxWeight: 17}; // NP-hard
var items = [ // allowed multiple of each
{name:'apple', weight:3, value:20},
{name:'blanket', weight:4, value:40},
{name:'lantern', weight:5, value:10},
{name:'radio', weight:6, value:30}
];
function generateRandomSolution(){
return solution; // array of items, must be <= maxWeight
};
function generateNeighboringSolution(oldSolution){
// add, swap, or remove item randomly
return solution; // array of items, must be <= maxWeight
}
function calculateCost(solution){
return cost; // sum of values of items
}
function acceptance_probability(old_cost, new_cost, temperature){
return Math.pow(Math.E, (new_cost - old_cost)/temperature); // probability to jump
}
function simulateAnnealing(){
// check out readme's pseudocode for how to to implement this
return solution; // array of items, must be <= maxWeight
};
///////////////////////////////////
// HELPER FUNCTIONS //
// don't modify, but you can use //
///////////////////////////////////
function randomIndex(list){
return Math.floor(Math.random()*list.length);
}
function weigh(solution){
return solution.reduce(function(total, item){ return total + item.weight}, 0);
}