-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.hs
More file actions
163 lines (128 loc) · 8.08 KB
/
Simulation.hs
File metadata and controls
163 lines (128 loc) · 8.08 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
module Simulation
(
createEnvironment
)
where
import System.Random
import Data.List
import Environment
import Functions
import Objects
import UtilsRobots
import Robots
-- crea el estado inicial del ambiente, ubicando en posiciones aleatorias los distintos objetos
createEnvironment :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int -> [Int] -> [Int] -> String -> IO()
createEnvironment n m agNumber childNumber dirtyNumber obstNumber t robotType sim totalSim results allTurns debugMode
| sim == totalSim = let averageResults = div ((sum results)*100) (length results)
averageTurn = div (sum allTurns) (length allTurns)
averageIter = div ((sum allTurns)*agNumber) (length allTurns)
in do
putStrLn ("Victory Percent = " ++ (show averageResults) ++ "%")
putStrLn ("Turns average = " ++ (show averageTurn))
putStrLn ("Robots Iterations average = " ++ (show averageIter) ++ "\n")
| otherwise = do
let freePos = createPos n m
-- ubicando corral
gen <- newStdGen
gen' <- newStdGen
let playpenPos = createPlaypen childNumber n m gen gen'
let playpen = locateObjects "Playpen" childNumber playpenPos gen
let aux = updatePos playpenPos freePos
let freePos = aux
-- ubicando agentes
gen <- newStdGen
let robotList = locateObjects "Robot free" agNumber freePos gen
let aux = updatePos (getPosObjects robotList) freePos
let freePos = aux
-- ubicando obstaculos
gen <- newStdGen
let obstList = locateObjects "Obstacle" obstNumber freePos gen
let aux = updatePos (getPosObjects obstList) freePos
let freePos = aux
-- ubicando ninnos
gen <- newStdGen
let childList = locateObjects "Child" childNumber freePos gen
let aux = updatePos (getPosObjects childList) freePos
let freePos = aux
-- ubicando suciedad
gen <- newStdGen
let dirtyList = locateObjects "Dirty" dirtyNumber freePos gen
let aux = updatePos (getPosObjects dirtyList) freePos
let freePos = aux
let actionRobot = getRobotType
gen <- newStdGen
mainPrintEnvironment playpen robotList childList dirtyList obstList freePos n m
simulation childList robotList dirtyList freePos obstList playpen t 1 0 gen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode
where
getRobotType
| robotType == 1 = randomRobot
| robotType == 2 = nannyRobot
| robotType == 3 = cleanerRobot
| robotType == 4 = balancedRobot
| robotType == 5 = teamworkRobot
-- ejecuta la simulacion
simulation :: [Object] -> [Object] -> [Object] -> [(Int,Int)] -> [Object] -> [Object] -> Int -> Int -> Int -> StdGen -> Int -> Int ->
(Object -> [Object] -> [(Int,Int)] -> [Object] -> [Object] -> [Object] -> StdGen -> Int -> Int -> ([Object],[Object],[Object],[(Int,Int)],StdGen)) -> Int -> Int -> Int -> Int -> [Int] -> [Int] -> String -> IO()
simulation childList robotList dirtyList freePos obstList playpen t turn iRobot gen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode
-- derrota
| (cleanPercent dirtyList freePos) < 60 && turn > 5 = do
putStrLn("Fail")
putStrLn("Robots couldnt keep environment clean")
putStrLn("Turns: " ++ (show turn))
putStrLn("Robots iterations: " ++ (show (turn * (length robotList))))
printAt
createEnvironment n m (length robotList) (length childList) dirtyNumber (length obstList) t robotType (sim+1) totalSim (results ++ [0]) (allTurns ++ [turn]) debugMode
-- victoria
| turn > 150 = do
putStrLn("Success")
putStrLn("Number of turns exceeded")
printAt
createEnvironment n m (length robotList) (length childList) dirtyNumber (length obstList) t robotType (sim+1) totalSim (results ++ [1]) (allTurns ++ [turn]) debugMode
-- victoria
| ( let inPlaypen = [child | child <- (getPosObjects childList), elem child (getPosObjects playpen)]
inRobot = [child | child <- (getPosObjects childList), elem child (getPosObjects robotList)]
inBoth = [child | child <- (getPosObjects childList), elem child (getPosObjects playpen),
elem child (getPosObjects robotList)]
in ( (length inPlaypen) + (length inRobot) - (length inBoth) ) == (length childList)) = do
putStrLn ("Success")
putStrLn ("All childs in playplen or in robots")
putStrLn("Turns: " ++ (show turn))
putStrLn("Robots iterations: " ++ (show (turn * (length robotList))))
printAt
createEnvironment n m (length robotList) (length childList) dirtyNumber (length obstList) t robotType (sim+1) totalSim (results ++ [1]) (allTurns ++ [turn]) debugMode
-- cambio de turno (puede haber cambio de ambiente)
| iRobot == (length robotList) =
if (mod turn t) == 0
then let (newChildList, newDirtyList, newObstList, newFreePos) =
changeEnvironment childList dirtyList obstList freePos robotList playpen n m 0 gen
in do
printNumber
putStrLn ("\nEnvironment changing\n")
mainPrintEnvironment playpen robotList newChildList newDirtyList newObstList newFreePos n m
printNumber
simulation newChildList robotList newDirtyList newFreePos newObstList playpen t (turn + 1) 0 gen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode
else simulation childList robotList dirtyList freePos obstList playpen t (turn + 1) 0 gen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode
-- ejecucion de una accion de agente
| otherwise =
let robot = robotList !! iRobot
(newChildList, newDirtyList, newRobotList, newFreePos, newGen) =
actionRobot robot childList freePos dirtyList playpen robotList gen n m
newRobot = newRobotList !! iRobot
in do
printMinus
putStrLn("\n\nMove: " ++ (show robot) ++ "\nTo: " ++ (show newRobot))
mainPrintEnvironment playpen newRobotList newChildList newDirtyList obstList newFreePos n m
printMinus
callSim newChildList newRobotList newDirtyList newFreePos newGen
where
printAt = putStrLn("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
printNumber = putStrLn("##################################################################################################")
printMinus = putStrLn("--------------------------------------------------------------------------------------------------")
callSim newChildList newRobotList newDirtyList newFreePos newGen =
if debugMode == "y"
then do
putStrLn ("Press Enter to continue")
a <- getLine
simulation newChildList newRobotList newDirtyList newFreePos obstList playpen t turn (iRobot+1) newGen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode
else
simulation newChildList newRobotList newDirtyList newFreePos obstList playpen t turn (iRobot+1) newGen n m actionRobot robotType dirtyNumber sim totalSim results allTurns debugMode