Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Change in fitness calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai Maruseac committed May 15, 2011
1 parent 5db5e62 commit 3cb81b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 36 deletions.
11 changes: 3 additions & 8 deletions Maze/GUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ notFinished = (False, 0, 0)

-- simulation ended
end :: IORType -> FinishInfo
end r = (True, cGuy r,
fitness (guyPos r) (guyTime r) (endPoint r) (endTime r) (guysBestDist r)
(guyBlocks r) ((plans r) V.! (cGuy r)))
end r = (True, cGuy r, fitness (guyPos r) (guyTime r) (endPoint r) (endTime r)
(guysBestDist r))

{-
Type of the IORef used.
Expand All @@ -63,7 +62,6 @@ data IORType = IORCT
, guyPos :: Point
, guyTime :: Time
, guysBestDist :: Int
, guyBlocks :: Int
, plans :: V.Vector Plan
, gen :: Maybe StdGen
, model :: Maybe (ListStore ListStoreType)
Expand All @@ -72,7 +70,7 @@ data IORType = IORCT
, generation :: Int
, mRate :: Double
}
empty = IORCT Nothing (0, 0) 0 0 (0, 0) 0 1000 0 V.empty Nothing Nothing Nothing (-100) 0 0.0
empty = IORCT Nothing (0, 0) 0 0 (0, 0) 0 1000 V.empty Nothing Nothing Nothing (-100) 0 0.0

{-
Real evolution function. Will update IORType record.
Expand All @@ -86,7 +84,6 @@ evolveFunc r@(IORCT
, guyPos = pos
, guyTime = t
, guysBestDist = bd
, guyBlocks = bl
, plans = ps
})
-- normal case: in the middle of simulation
Expand All @@ -95,15 +92,13 @@ evolveFunc r@(IORCT
{ guyPos = p
, guyTime = t'
, guysBestDist = min bd $ manhattan p endp
, guyBlocks = if p == pos then bl + 1 else bl
} , notFinished)
-- simulation ended
| t == endt || pos == endp = (r
{ guyPos = (1, 1)
, guyTime = 0
, cGuy = guy + 1
, guysBestDist = (snd endp) * (snd endp)
, guyBlocks = 0
}, end r)

{-
Expand Down
35 changes: 7 additions & 28 deletions Maze/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ doStep (m, p) (pos, t) = (takeAct (m A.! pos) (p V.! t) pos, t + 1)
Take one action.
-}
takeAct :: Cell -> Cardinal -> Point -> Point
takeAct (C l) d p = if d `elem` l then move d p else p
--takeAct (C l) d p = if d `elem` l then move d p else p
takeAct c@(C l) d p
= if d `elem` l then move d p else if E `elem` l then move E p
else if S `elem` l then move S p else p

{-
Move in one direction.
Expand Down Expand Up @@ -76,44 +79,20 @@ Fitness weights
fTIME = 100
fDIST = -3
fBDIST = -5
fBLOCKS = -1
fLOOPS = -20
fLINE = -5
fCOL = -2
fAREA = 1

{-
Computes the fitness of a plan.
-}
fitness :: Point -> Time -> Point -> Time -> Int -> Int -> Plan -> Fitness
fitness p t ep et bd bl pl
fitness :: Point -> Time -> Point -> Time -> Int -> Fitness
fitness p t ep et bd
= fTIME * (et - t)
+ fDIST * manhattan p ep
+ fBLOCKS * bd
+ fBLOCKS * bl
+ fLOOPS * (loops . V.toList $ pl)
+ fLINE * (s - fst p)
+ fCOL * (s - snd p)
+ fBDIST * bd
+ fAREA * fst p * snd p
where
s = snd ep

{-
Get the number of loops contained in a plan.
-}
loops :: [Cardinal] -> Int
loops pl = getLoopsAux . map cartez $ pl
where
cartez E = (1, 0)
cartez W = (-1, 0)
cartez S = (0, 1)
cartez N = (0, -1)
getLoopsAux (c:cs) = getLoops c cs + getLoopsAux cs
getLoopsAux [] = 0
getLoops c (c':cs) = if c == (0, 0) then 1 else getLoops (c |+| c') cs
getLoops _ [] = 0
(x, y) |+| (x', y') = (x + x', y + y')

{-
Gets the manhattan distance between two points.
-}
Expand Down

0 comments on commit 3cb81b7

Please sign in to comment.