-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsp.hs
65 lines (54 loc) · 1.76 KB
/
tsp.hs
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
module Main(
main
) where
import RGEP
import SymReg
import PGEP
import EA
import EAMonad
import Postfix
import Selection
import Operators
import Data.List
exps = 1
ps = 500
is = 200
gens = 500
pm = 0.005
pr = 0.005
pc1 = 0.7
pc2 = 0.7
worst = 21*7
pairs = [(j, i) | j <- [0.0,5.0], i <- [0.0..5.0]]
swapped [] = []
swapped ((a, b):rest) = (b, a):swapped rest
cities = pairs `union` swapped pairs
catpath (a:b:rest) = (a `union` b) : rest
cat = OP { eats=2, leaves=1, applyOp=catpath, name="cat" }
revpath (a:rest) = reverse a:rest
rev = OP { eats=1, leaves=1, applyOp=revpath, name="rev" }
invert stack@([a]:rest) = stack
invert (as:rest) = (last as : (init (tail as) ++ [head as])) : rest
inv = OP { eats=1, leaves=1, applyOp=invert, name="inv" }
city point = OP { eats=0, leaves=1, applyOp=([point]:), name=show point}
ops = [cat, rev, inv]--, dup, drp, over, swap, rot, nip, tuck]
terms = map city $ delete (0, 0) cities
pathLength (Just cits) = (7.0*(19.0 - fromIntegral (length cits))) + routelength cits' 0 where
cits' = [(0.0, 0.0)] ++ cits
routelength [] n = worst
routelength [end] n = n + distance end (0, 0)
routelength (a:b:rest) n = routelength (b:rest) $ n + distance a b
distance (a, b) (a', b') = sqrt ((a-a')^2 + (b-b')^2)
evalPath Nothing = return $ Min worst
evalPath path = return $ Min $ pathLength path
run = rgep ps is ops terms pm pr pc1 pc2 evalPath gens
main = do
result <- experiment exps ""
writeFile "traveling" result
experiment 0 l = return l
experiment times result = do
(p, e, l, g) <- runEAIO run ()
let (i, f) = best p
let route = rgepEval (cdns2symbols ops terms i)
let result' = result ++ l ++ "\n" ++ show route ++ "\n with fitness: " ++ show f ++ "\nand length " ++ show (fmap length route) ++ "\n"
experiment (times-1) result'