Ant System (pheromone trail) [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How do I implement this in JavaScript? Thank you.
Ant System (AS) was the first ACO algorithm to be proposed in the literature (Dorigo et al. 1991, Dorigo et al. 1996). Its main characteristic is that the pheromone values are updated by all of the ants that have completed the tour. Solution components c_{i,j} are the edges of the graph, the pheromone update for \tau_{i,j}, that is, for the pheromone associated to the edge joining the cities i, and j, is performed as follows:
\tau_{i,j} <- (1-\rho)*\tau_{i,j} + \sum\limits_{k=1}^{m} \Delta\tau_{i,j}^{k}
Where the evaporation rate rho is in the interval (0,1], m is the number of ants, and \Delta*\tau_{i,j}^{k} is the quantity of pheromone laid on the the edge (i,j) by the k-th ant. If an ant uses an edge on its tour, the quantity of pheromone laid on that edge is calculated as follows,
\Delta\tau_{i,j}^{k} = 1/L_{k}
Where L_{k} is the tour length of the k-th ant. If an ant does not use the edge (i,j), then \Delta\tau_{i,j}^{k} is zero.
I'm trying....
pher[i][j]= pher[i][j] *(1.0-rho) + numAnts*(.......

So this is a sum example. But I don't understand the rest of the algorithm. Do you have any pheromone matrix for example?
function sumk2m(k0,m,f){
var result = 0;
for(var k = k0; k<m ; k++){
result = result+f(k);
}
return result
}
function isedgeintour(k){
//I don't know what's this!
}
function L(k){
//I don't know what's this either!
}
var deltapher = function (k){
if(isedgeintour(k)){
return 1/L(k);
} else {
return 0;
}
}
pher[i][j]= pher[i][j] *(1.0-rho) + sumk2m(1,numAnts,deltapher)

Related

Can i convert math.js object into JavaScript function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm developing simple js neural network and need to create javascript function (derivative of entered by user) before learning starts.
I know about evaluate(), but i think it'll be slowly than simple function.
That is what i want:
const derivative = math.derivative('x^2/sin(2x)', 'x');
const derivative_func = derivative.please_stay_func();
...
while(1) alert(derivate_func(12)) //:) alert result of (2 * 12 * Math.sin(2*12) - 2 * Math.pow(12, 2) * Math.cos(2*12)) / Math.pow(sin(2*12), 2)
Is it real? And maybe i'm not right about speed. Maybe there is some better ways - write here.
i am on mobile, please edit this
maybe this is want you want to do
const countThings = (customValue) ==>{
let x = 'x^2/sin(x)';
let y = 'x';
x = math.parse(x)
y = math.parse(y)
return math.derivative(x, y).evaluate({x:customValue});}
console.log(countThings(12))
and don't do while(1) alert because it will keep alerting

I would like to find out how to get the remaining points a user needs to level up [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Sorry, I'm really bad at math and I also don't know the English terms so I really don't know how to ask this question properly.
I'm using this to calculate a users level
const curLevel = Math.floor(0.2 * Math.sqrt(score.points));
I would like to find out how to get the remaining points a user needs to level up. I didn't really try anything yet because I have no idea where to begin, might even be something super simple...
You can find the number of points needed to reach the next integer level by solving for points in the equation curLevel + 1 = 0.2 * Math.sqrt(nextLevelPoint).
You get that you can calculate the total points needed for the next level with:
let nextLevelPoint = Math.pow(5*curLevel + 5, 2)
Subtract score.point from this nextLevelPoint and you get the remaining points a user needs to level up.

Integer comparison within score board app, score difference equal two [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm writting an score board app.
Got two integers, i need to compare them, for example
to know when game should be ended, eg. score = 20:22, 24:26 (score difference to end the game should be equal to two)
How i can make such comparison with js?
You can use split() method to separate scores into two different elements, and then afterwards just compare them.
function isGameOver(score, differenceToWin) {
var scoreArray = score.split(":");
return Math.abs(scoreArray[0] - scoreArray[1]) > differenceToWin;
}
isGameOver('24:26', 2)
EDIT: In case you only need to compare two integers, go ahead and use only the return statement line:
var score1 = 24;
var score2 = 26;
var differenceToWin = 2;
var isGameOver = Math.abs(score1 - score2) > differenceToWin;

Javascript - Sum solution [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am getting below line in my source file and I would like to sum those values separated by pipe ("|")
There is no limit for the values coming in the line (might be 100 values)
10|20|30|40|[no limit for values] - Separator is pipe "|"
The ouptput would be 100
Please help to write a javascript function for the above query.
Regards
Jay
You should take a look at JavaScript's built-in functions: With split you can split your string into an array, with reduce you can 'reduce' your array to a single value, in that case via summation. These two links should provide you enough information for building your code.
You could try something like below:
function sum(value){
var total = 0;
var array = value.split(",").map(Number);
for( var i = 0; i < array.length; i++) {
total += array[i];
}
alert(total);
}
var value = "10|20|30|40".replace(/\|/g, ',');
console.log(sum(value));
https://jsfiddle.net/f7uqw7cL/

How do I rearrange an array with similar items so that similar items will never neighbor each other? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So for example if I have this array:
var input = [1,1,2,4,6,7,7,1];
I want the output to be something like:
[1,2,1,4,6,7,1,7]
The order of the new array does not matter, as long as similar items will never (or at least as seldom as possible) neighbor each other.
I can use plain JavaScript as well as underscore.js.
Try the following:
var input = [1,1,2,4,6,7,7,1];
input.sort()
var output = [];
var len = input.length;
for (var i = 0; i < Math.floor((len / 2)); i++) {
output.push(input[i]);
output.push(input[len - i - 1]);
}
if (len % 2) {
var left_over = input[Math.floor(len / 2)];
if (left_over == output[0]) {
output.push(left_over);
} else {
output.unshift(left_over);
}
}
Or see http://jsfiddle.net/d0j3Lfa3/1.
The solution sorts the numbers then alternates high and low. It deals with an odd number of elements, including corner cases such as [1,1,2] and [1,2,2] where it needs to push the middle element differently to pass. Since the input is sorted, input order doesn't affect the output.
This answer may help simplify things a bit.

Categories

Resources