Skip to content

Instantly share code, notes, and snippets.

@fabulousduck
Created October 9, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabulousduck/0523ada1f2eae41961cd96ddc677a042 to your computer and use it in GitHub Desktop.
Save fabulousduck/0523ada1f2eae41961cd96ddc677a042 to your computer and use it in GitHub Desktop.
func (algorithm *algorithm) putRandomCandidateSolutions(dataPoints []point, amount int){
for i := 0; i < amount; i++ {
algorithm.candidateSolutions = append(algorithm.candidateSolutions,randomSolution(dataPoints))
}
}
func randomSolution(dataPoints []point) []point {
dpC := dataPoints
//solution with start point
solution := []point{dpC[0]}
//remove start point so it cant be picked to visit
dpC = append(dpC[:0],dpC[1:]...)
//loop over the remaining points
for i := 0; i < len(dpC); i++ {
//grab random index from te dpC slice
randomPoint := rand.Intn(len(dpC))
//add the random point to the solution slice
solution = append(solution, dpC[randomPoint])
//remove it from the dpC slice so it cant be picked again
dpC = append(dpC[:randomPoint], dpC[randomPoint+1:]...)
}
//append the start position back on to the slice so we end at the same position again
solution = append(solution, solution[0]);
fmt.Println(solution)
return solution
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment