Skip to content

Commit c93b5b6

Browse files
committedNov 20, 2015
Add selector for choosing problem
1 parent 5c3550e commit c93b5b6

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed
 

‎client/js/app.es6

+38-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var problemFilename = utils.getQueryParam('problem') || null;
1212
var seed = utils.getQueryParam('seed') || 'random!';
1313
var problem = null;
1414
var rng = seedrandom(seed);
15-
var nodeCount = parseInt(utils.getQueryParam('nodes'),10) || 25;
15+
var nodeCount = parseInt(utils.getQueryParam('nodes'),10) || 20;
1616
var iterationLog = bows('Iteration');
1717
var bestLog = bows('Optimum');
1818
var size = 500;
@@ -41,6 +41,8 @@ function create(problem) {
4141
localStorage.removeItem('debug');
4242
}
4343

44+
setProblemSelector()
45+
4446
tsp = problem || new RandomTSP(nodeCount, size, size, rng);
4547
display = new Display(size, size);
4648
display.clearBest();
@@ -72,7 +74,7 @@ function onNewBest(i, walk, length) {
7274
*/
7375
function onIteration(i, pheromones) {
7476
iterationLog(i);
75-
d3.select('.iterationCount').text('Iteration: ' + i);
77+
d3.select('.iterationCount').text(i);
7678
var matrix = [];
7779
for(let i = 0; i < tsp.distances.length; i++) {
7880
matrix[i] = [];
@@ -91,6 +93,37 @@ function onIteration(i, pheromones) {
9193
display.printHeatMap(matrix, 1, 'pheromoneMatrix');
9294
}
9395

96+
function updateProblem() {
97+
let val = d3.select('.js-problem').property('value');
98+
if (val.indexOf('random') === 0) {
99+
var size = val.split('-').map(x=>x.trim())[1];
100+
window.location = `/?nodes=${size}`;
101+
} else {
102+
window.location = `/?problem=${val}.tsp`;
103+
}
104+
}
105+
106+
function setProblemSelector() {
107+
let checkOption = function(option, i) {
108+
if (problemFilename) {
109+
if (d3.select(this).attr('value') === problemFilename.split('.')[0]) {
110+
d3.select(this).attr('selected', 'selected');
111+
}
112+
} else if (nodeCount) {
113+
var split = d3.select(this).attr('value').split('-');
114+
console.log(parseInt(split[1]) === nodeCount, parseInt(split[1]), nodeCount)
115+
if (parseInt(split[1]) === nodeCount) {
116+
d3.select(this).attr('selected', 'selected');
117+
}
118+
}
119+
}
120+
121+
var x = d3
122+
.selectAll('.js-problem option')
123+
.each(checkOption);
124+
125+
}
126+
94127
/**
95128
* Starts the process!
96129
*/
@@ -125,5 +158,8 @@ d3.select('#refresh')
125158
d3.select('#run')
126159
.on('click', () => run());
127160

161+
d3.select('.js-problem')
162+
.on('change', () => updateProblem());
163+
128164

129165

0 commit comments

Comments
 (0)
Please sign in to comment.