예제 #1
0
func main() {
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Println("Usage: go run samples/tsp.go ROUTEFILEPATH")
		return
	}
	var routeFileName = flag.Arg(0)
	if !File.Exists(routeFileName) {
		fmt.Println("file " + routeFileName + " does not exist.")
		return
	}
	fmt.Println("using route file: " + routeFileName)

	idToPointLookup := readPoints(routeFileName)
	fmt.Println("read " + strconv.Itoa(len(idToPointLookup)) + " points...")

	calc := func(candidate string) int {
		return getFitness(candidate, idToPointLookup)
	}

	if File.Exists(routeFileName + ".opt.tour") {
		fmt.Println("found optimal solution file: " + routeFileName + ".opt")
		optimalRoute := readOptimalRoute(routeFileName+".opt.tour", len(idToPointLookup))
		fmt.Println("read " + strconv.Itoa(len(optimalRoute)) + " segments in the optimal route")
		points := getPointsInOptimalOrder(idToPointLookup, optimalRoute)
		genes := genericGeneSet[0:len(idToPointLookup)]
		idToPointLookup = make(map[string]Point, len(idToPointLookup))
		for i, v := range points {
			idToPointLookup[genericGeneSet[i:i+1]] = v
		}
		fmt.Print("optimal route: " + genes)
		fmt.Print("\t")
		fmt.Println(getFitness(genes, idToPointLookup))
	}

	geneSet := genericGeneSet[0:len(idToPointLookup)]

	start := time.Now()

	disp := func(candidate string) {
		fmt.Print(candidate)
		fmt.Print("\t")
		fmt.Print(getFitness(candidate, idToPointLookup))
		fmt.Print("\t")
		fmt.Println(time.Since(start))
	}

	var solver = new(genetic.Solver)
	solver.MaxSecondsToRunWithoutImprovement = 20
	solver.LowerFitnessesAreBetter = true

	var best = solver.GetBest(calc, disp, geneSet, len(idToPointLookup), 1)
	fmt.Println()
	fmt.Println(best, "\t", getFitness(best, idToPointLookup))
	fmt.Print("Total time: ")
	fmt.Println(time.Since(start))
}
예제 #2
0
func main() {
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Println("Usage: go run standard.go RESOURCEFILEPATH")
		return
	}
	var resourceFileName = flag.Arg(0)
	if !File.Exists(resourceFileName) {
		fmt.Println("file " + resourceFileName + " does not exist.")
		return
	}
	fmt.Println("using resource file: " + resourceFileName)

	resources, maxWeight, solution := loadResources(resourceFileName)

	optimalFitness := 0
	for resource, count := range solution {
		optimalFitness += resource.value * count
	}

	calc := func(candidate string) int {
		decoded := decodeGenes(candidate, resources)
		return getFitness(decoded, maxWeight, optimalFitness)
	}

	start := time.Now()

	disp := func(candidate string) {
		decoded := decodeGenes(candidate, resources)
		fitness := getFitness(decoded, maxWeight, optimalFitness)
		display(decoded, fitness, time.Since(start), true)
	}

	var solver = new(genetic.Solver)
	solver.MaxSecondsToRunWithoutImprovement = 5
	solver.MaxRoundsWithoutImprovement = 3

	var best = solver.GetBestUsingHillClimbing(calc, disp, hexLookup, 10, numberOfGenesPerChromosome, optimalFitness)

	fmt.Print("\nFinal: ")
	decoded := decodeGenes(best, resources)
	fitness := getFitness(decoded, maxWeight, optimalFitness)
	display(decoded, fitness, time.Since(start), false)
	if fitness == optimalFitness {
		fmt.Println("-- that's the optimal solution!")
	} else {
		percentOptimal := float32(100) * float32(fitness) / float32(optimalFitness)
		fmt.Printf("-- that's %f%% optimal\n", percentOptimal)
	}
}