Skip to content

The assessment Implementation #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions SimulatedAnnealing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

/*****************************************************************/
/* This solution is utilizing on (Simulated Annealing) Algorithm */
/****************************************************************/

const Tour = require('./Tour')

const Params = {
MAX_TEMP: 10000,
MIN_TEMP: 1,
COOLING_RATE: 0.005
}

class SimulatedAnnealing {

constructor(theGraph) {
this.graph = theGraph;
this.selectedTour = {};
}

simulate() {
let temp = Params.MAX_TEMP;
let currentTour = new Tour(this.graph);
currentTour.startTrip();

let bestTour = new Tour(this.graph)
bestTour.startTrip();

while (temp > Params.MIN_TEMP) {
let newTour = new Tour(this.graph);
newTour = newTour.changeTourRoute().startTrip();
let currentEnergy = currentTour.getTotalHunts();
let newEnergy = currentTour.getTotalHunts();
if (this.acceptanceProbability(currentEnergy, newEnergy, temp) > Math.random())
currentTour = newTour;

if (currentEnergy > bestTour.getTotalHunts())
bestTour = currentTour

temp *= 1 - Params.COOLING_RATE;
}
this.selectedTour = bestTour;
return this;
}

acceptanceProbability(currentEnergy, newEnergy, temp) {
if (newEnergy > currentEnergy)
return 1;
return Math.exp((currentEnergy - newEnergy) / temp)
}

getSelectedTour() {
return this.selectedTour;
}

}

module.exports = SimulatedAnnealing
105 changes: 105 additions & 0 deletions Tour.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

class Tour {

constructor(theGraph) {
this.prey = 0;
this.stamina = 3
this.graph = { ...theGraph }
this.tripPath = [];
}

startTrip(theGraph) {
let graph = theGraph || this.graph;
this.tripPath = []
/* visited contains the amount of boars on each node. */
let visited = {
A: 3,
B: 3,
C: 3,
D: 3,
E: 3,
F: 3,
G: 3,
H: 3,
I: 3,
J: 3,
K: 3
};
/* queue contains the next node to explore */
let queue = [];
let node = 'A';
this.tripPath.push(node)
queue.push(node)
this.huntRoutine(visited, queue, node)

while (queue.length != 0) {
node = queue.pop();
if (!graph[node] || graph[node].length == 0) break;
let e = graph[node][0]
queue.push(e)
this.tripPath.push(e)
if (this.stamina < 3) {
this.restRoutine(visited, queue, e)
} else {
this.huntRoutine(visited, queue, e)
}
}
return this;
}

restRoutine(visited, queue, e) {
this.stamina -= 1
this.stamina += 2;
}

huntRoutine(visited, queue, e) {
this.stamina -= 1

if (visited[e] == 1) {
visited[e] = visited[e] - 1;
this.prey += 1

} else if (visited[e] >= 2) {
visited[e] = visited[e] - 2;
this.prey += 2
}
}

changeTourRoute() {
let nameArr = Object.entries(this.graph).map(([key, val]) => key)
let rand = Math.floor(Math.random() * (nameArr.length - 1));
let nodeTitle = nameArr[rand];
let edges = this.graph[nodeTitle];

/*
* When shuffle edges for given node
* it will eventually simulate as if we go to different route when
* calling generate method.
*/

this.graph[nodeTitle] = this.shuffle(edges);
return this;
}

getTotalHunts() {
return this.prey;
}

getTripPath() {
return this.tripPath;
}

shuffle(arr) {
return [...arr].map((_, i, orgArr) => {
var rand = i + (Math.floor(Math.random() * (orgArr.length - i)));
[orgArr[rand], orgArr[i]] = [orgArr[i], orgArr[rand]]
return orgArr[i]
})
}
}

module.exports = Tour




41 changes: 28 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@

const SimulatedAnnealing = require('./SimulatedAnnealing')

prey = 0
hunting_map = {
'A':['B','C','K'],
'B':['D','E'],
'C':['E','G','H'],
'D':['E','F'],
'E':['G','I','F'],
'F':['I','J'],
'G':['I','K'],
'H':['I','F'],
'I':['K'],
'J':['K'],
'K':[]
'A': ['B', 'C', 'K'],
'B': ['D', 'E'],
'C': ['E', 'G', 'H'],
'D': ['E', 'F'],
'E': ['G', 'I', 'F'],
'F': ['I', 'J'],
'G': ['I', 'K'],
'H': ['I', 'F'],
'I': ['K'],
'J': ['K'],
'K': []
};
console.log(hunting_map);
console.log(prey);
// console.log(hunting_map);
// console.log(prey);


let tourSimulation = new SimulatedAnnealing(hunting_map)
.simulate();

let tour = tourSimulation.getSelectedTour();

let selectTourPath = tour.getTripPath();
let numberOfBoars = tour.getTotalHunts();

console.log(numberOfBoars)
console.log(selectTourPath.join(" "))