-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.go
84 lines (73 loc) · 2.5 KB
/
tsp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Author : Muhammad Arizal Saputro
* StudentId: 1301178325
*/
package main
import (
"flag"
"os"
"fmt"
"time"
"github.com/arizalsaputro/ant-system-tsp/util"
"github.com/arizalsaputro/ant-system-tsp/swarm"
)
func main() {
// Sub commands
runCommand := flag.NewFlagSet("run",flag.ExitOnError)
// Calculate sub command flag pointer
runTextFileName := runCommand.String("file","","File .tsp to read. (Required)")
runIntIteration := runCommand.Int("iteration",5,"Number of iteration. (Optional)")
runIntColonySize := runCommand.Int("ant",20,"Number of ant size. (Optional)")
runFloatEvap := runCommand.Float64("evap",0.001,"Number of evaporation (1-p). (Optional)")
runFloatAlpha := runCommand.Float64("alpha",15,"Alpha constant variable. (Optional)")
runFloatBeta := runCommand.Float64("beta",20,"Beta constant variable. (Optional)")
runFloatInitPherom := runCommand.Float64("init-pheromone",0.1,"Initial Pheromone. (Optional)")
runFloatInitQ := runCommand.Float64("q",0.1,"Q constant variable. (Optional)")
runBoolSetLog := runCommand.Bool("log",false,"Set logging. (Optional)")
runIntAvgOf := runCommand.Int("avg-of",0,"Run n-times and get average")
if len(os.Args) < 2 {
fmt.Println("run subcommand is required")
os.Exit(1)
}
switch os.Args[1] {
case "run":
runCommand.Parse(os.Args[2:])
default:
fmt.Printf("Unknown Command %v",os.Args[1])
os.Exit(1)
}
if runCommand.Parsed(){
if *runTextFileName == ""{
runCommand.PrintDefaults()
os.Exit(1)
}
start := time.Now()
fmt.Println("Run...")
list,err := util.ReadFile(*runTextFileName)
if err != nil{
fmt.Printf("Error read file '%v'",err.Error())
os.Exit(1)
}
if *runIntAvgOf != 0{
avg := 0.0
for i:=0;i<*runIntAvgOf;i++{
pso := swarm.NewAntCycle(*runIntIteration,*runIntColonySize,*runFloatEvap,*runFloatAlpha,*runFloatBeta,*runFloatInitPherom,*runFloatInitQ,list)
pso.SetLog(*runBoolSetLog)
pso.Process()
avg += pso.BestAnt.TotalCost
fmt.Printf("best cost %v %v \n",pso.BestAnt.TotalCost , pso.BestAnt.GetPath())
}
fmt.Println("average cost:",avg/float64(*runIntAvgOf))
}else{
pso := swarm.NewAntCycle(*runIntIteration,*runIntColonySize,*runFloatEvap,*runFloatAlpha,*runFloatBeta,*runFloatInitPherom,*runFloatInitQ,list)
pso.SetLog(*runBoolSetLog)
pso.Process()
fmt.Printf("best cost -> %v \npath -> %v \n",pso.BestAnt.TotalCost , pso.BestAnt.GetPath())
}
elapsed := time.Since(start)
fmt.Printf("Ant System took %s", elapsed)
} else{
runCommand.PrintDefaults()
os.Exit(1)
}
}