0

I am trying to do research on the problem of Traveling salsman by using Leaflet Routing Machine. for experiment I tried to include some Latlang points as waypoints

let titikA = L.latLng(0.385818, 109.955803);
let titikB = L.latLng(0.38497, 109.954333);
let titikC = L.latLng(0.409132, 109.959386);
let titikD = L.latLng(0.379097, 109.95886);
let titikE = L.latLng(0.379097, 109.95886);
let wp1 = new L.Routing.Waypoint(titikA);
let wp2 = new L.Routing.Waypoint(titikB);
let wp3 = new L.Routing.Waypoint(titikC);
let wp4 = new L.Routing.Waypoint(titikD);
let wp5 = new L.Routing.Waypoint(titikE);

I concatenate these variables into 1 in array form code:

let array = [wp1, wp2, wp3, wp4, wp5];

My hopes are: random wp1, wp2, wp3, wp4, wp5 Like :

0[wp2, wp4, wp3, wp5, wp1]
1[wp1, wp4, wp3, wp5, wp2]
2[random, random, random, random, random]
3[random, random, random, random, random]
5[random, random, random, random, random]

I really don't understand how the code I've been trying for a while is

function createPopulation(array) {
  let matrix = Array.from(new Array(array.length), (_) => Array().fill(0));
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  for (let k = 0; k < matrix.length; k++) {
    matrix[k].push(array);
  }
  return matrix;
}
console.log(createPopulation(array));

and this is the result i got image

i need to get array like this result tapi dengan posisi 0 yang sudah teracak

Zaky
  • 71
  • 1
  • 2
  • 1
    If I see correctly, your desired output is from Python, and yet you are trying with JS? – aca Jul 26 '22 at 10:17
  • 1
    What does the image in the desired result have to do with your described array? – derpirscher Jul 26 '22 at 10:19
  • @aca yes, that's what i want – Zaky Jul 26 '22 at 10:38
  • @derpirscher so far, for experimentation, I only get 1 array at random, but what I want is randomization of each loop. – Zaky Jul 26 '22 at 10:52
  • Please present a comprehensible example with a given input and the expected output from that input ... Especially, as I have very limited experice with python and the comments are in a language I don't understand, I have no idea what the code in your "desired result" really does ... – derpirscher Jul 26 '22 at 10:55
  • But from the looks of it, it creates 10 random permutations for the sequence [0...9] and puts them in a matrix. So I'm only wondering more, what that has to do with your mentioned latitue and longitues ... If you just need a simple random permutation/shuffling algorithm, search for "Fisher-Yates" – derpirscher Jul 26 '22 at 11:04
  • @derpirscher Sorry my question can't make you understand, I've updated it – Zaky Jul 26 '22 at 13:50
  • Again, if you want random shuffling of an array (and your example seems pretty much like it) "Fisher Yates" algorithm (or one of its variants) is probably what you are searching for https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – derpirscher Jul 26 '22 at 14:50
  • @derpirscher i know, but its cannt to answare my question – Zaky Jul 26 '22 at 17:29
  • Then please explain why your question is more than just the shuffling of an array ... – derpirscher Jul 27 '22 at 08:04
  • @derpirscher cause i want to follw this stap https://github.com/GustiMuhammadAdzaky/ga/blob/master/main.ipynb – Zaky Jul 27 '22 at 08:19

0 Answers0