Beispiel #1
0
func main() {
	// Setup data generator
	n := 8
	gen := data.New(n, 100000)

	// Generate data
	dist, err := gen.Distance()
	if err != nil {
		panic(err)
	}
	flow, err := gen.Flow(1 / 3)
	if err != nil {
		panic(err)
	}
	cost, err := dist.Combine(flow)
	if err != nil {
		panic(err)
	}

	logger.Println("Distance matrix:")
	logger.Println(dist)
	logger.Println()
	logger.Println("Flow matrix:")
	logger.Println(flow)

	// Setup runner
	maxTime := time.NewTimer(15 * time.Minute)
	runner := &search.Runner{
		NumCPU:    runtime.NumCPU(),
		Cost:      cost,
		VarCutoff: 0,
		ProbSize:  fact(n),
	}

	// Run on all 4 cores
	quit := make(chan int)
	results := make(chan *search.Result)
	completed := make(chan bool)
	go runner.Run(quit, results, completed)

loop:
	for {
		select {
		case res := <-results:
			if res != nil {
				fmt.Println(res.Score, res.Perm)
			}
		case <-completed:
			// Bug: may lose last few solutions due to race condition
			fmt.Println("Completed entire search.")
			break loop
		case <-maxTime.C:
			quit <- 1
			fmt.Println("Time out.")
			break loop
		}
	}
}
Beispiel #2
0
func testGen() {
	gen := data.New(5, 10000)

	dist, err := gen.Distance()
	if err != nil {
		panic(err)
	}
	flow, err := gen.Flow(1 / 3)
	if err != nil {
		panic(err)
	}

	fmt.Println()
	fmt.Println(dist)
	fmt.Println()
	fmt.Println(flow)
}