Skip to content

Commit

Permalink
add slight bias to parent selection
Browse files Browse the repository at this point in the history
  • Loading branch information
aThorp96 committed Feb 19, 2019
1 parent 2a8c583 commit e859776
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Golang implementation of the GENTIOR TSP heuristic developed ad Colorado State University with D. Fuquay and D. Whitley

The GENTIOR is described in Fuquay and Whitley's 1990 paper [Genetic Algorithm Solutions for the Traveling Salesman Problem](https://dl.acm.org/citation.cfm?id=99033&dl=ACM&coll=DL)

TODO:
- Add some biased parent selection methods to compare convergence
- Inspect for complexity issues
51 changes: 42 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ func printProgress(i, n int) {
fmt.Printf("%d%% \n", int(percent))
}


func ConnectedGentior(filepath string, populationSize, generations int, bias float64, quiet bool) {
start := time.Now()
// Create graph and population
graph := NewWeightedGraphFromFile(filepath)
population := generatepopulation(graph, populationSize)
graph := NewWeightedGraphFromFile(filepath) //O(n)
population := generatepopulation(graph, populationSize) //O(p)

// genetically develop good solutions (hopefully)
for i := 0; i < generations; i++ {
parents := selectParents(len(population), bias)
pop := population[parents[0]]
mom := population[parents[1]]
child := connectedEdgeRecombination(pop, mom, graph)
population = reconstructPopulation(population, child)
for i := 0; i < generations; i++ { // O(n)
parents := selectParents(len(population), bias) // O(1)
pop := population[parents[0]] // O(1)
mom := population[parents[1]] // O(1)
child := connectedEdgeRecombination(pop, mom, graph) // O(n)
population = reconstructPopulation(population, child) // O(logn)
if !quiet {
printProgress(i, generations)
}
Expand Down Expand Up @@ -393,20 +394,52 @@ func showPopulation(population []Hamiltonian) {
}

func selectParents(populationSize int, bias float64) []int {
return betterParents(populationSize, bias)
}

func randomParents(populationSize int, bias float64) []int {
// Select at Random
// TODO implement bias
mom := rand.Intn(populationSize)
pop := rand.Intn(populationSize)

// ensure parents are different
for mom == pop {
pop = rand.Intn(populationSize)
pop = rand.Intn(populationSize)
}

parentList := []int{pop, mom}
return parentList
}

func betterParents(populationSize int, bias float64) []int {
// Select at Random
// TODO implement bias
mom := betterRand(populationSize, bias)
pop := betterRand(populationSize, bias)

// ensure parents are different
for mom == pop {
pop = betterRand(populationSize, bias)
}

parentList := []int{pop, mom}
return parentList
}

func betterRand(max int, bias float64) int {
// chose two indices
n1 := rand.Intn(max)
n2 := rand.Intn(max)

// return the lesser of the two
if n1 < n2 {
return n1
} else {
return n2
}
}

//TODO?
func applyBias(i int, max int, b float64) int {
//probabilities := make(
Expand Down
10 changes: 8 additions & 2 deletions data/XQF131/tsp2wdat.py → tools/tsp2wdat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import math

outFilename = "./xqf131.wdat"
outFile = open(outFilename, "w")
# tsp2wdat transforms a .tsp file into a .wdat (weighted data) file.
# A .wdat has a header: the number of vertices in the graph, followed by a
# number of lines descriving each edge. Edges are described by the vertex numbers
# of each end of the edge, followed by the distance spanned by the edge.
# Finally, the EOF indicator is the footer "-1 -1 -1"

filename = "./xqf131.tsp"
edgeFile = open(filename)

outFilename = "./xqf131.wdat"
outFile = open(outFilename, "w")

string = ""

numPoints = 0
Expand Down

0 comments on commit e859776

Please sign in to comment.