comparison src/MY_sequential_example.py @ 0:f895e266b37a draft

planemo upload for repository https://forgemia.inra.fr/nathalie.rousse/use/-/tree/gama/GAMA/galaxy-tools commit 67d85c013c62c16392b4796af86836b1334f2eef
author siwaa
date Tue, 04 Jun 2024 15:18:01 +0000
parents
children 168edc2db729
comparison
equal deleted inserted replaced
-1:000000000000 0:f895e266b37a
1 import asyncio
2 from asyncio import Future
3 from typing import Dict
4
5 from gama_client.base_client import GamaBaseClient
6 from gama_client.command_types import CommandTypes
7 from gama_client.message_types import MessageTypes
8
9 import pprint # _PRINT_
10 import argparse
11
12 nb_preys_init = 100 # default
13 nb_predators_init = 10 # default
14 nb_more_steps = 5 # default
15
16 experiment_future: Future
17 play_future: Future
18 pause_future: Future
19 expression_future: Future
20 step_future: Future
21 stop_future: Future
22
23
24 async def message_handler(message: Dict):
25 print("received", message)
26 if "command" in message:
27 if message["command"]["type"] == CommandTypes.Load.value:
28 experiment_future.set_result(message)
29 elif message["command"]["type"] == CommandTypes.Play.value:
30 play_future.set_result(message)
31 elif message["command"]["type"] == CommandTypes.Pause.value:
32 pause_future.set_result(message)
33 elif message["command"]["type"] == CommandTypes.Expression.value:
34 expression_future.set_result(message)
35 elif message["command"]["type"] == CommandTypes.Step.value:
36 step_future.set_result(message)
37 elif message["command"]["type"] == CommandTypes.Stop.value:
38 stop_future.set_result(message)
39
40
41 async def main():
42
43 global experiment_future
44 global play_future
45 global pause_future
46 global expression_future
47 global step_future
48 global stop_future
49
50 # Experiment and Gama-server constants
51
52 MY_SERVER_URL = "localhost"
53
54 MY_SERVER_PORT = 8000
55
56 GAML_FILE_PATH_ON_SERVER = "gama-platform/headless/samples/predatorPrey/MY_predatorPrey.gaml"
57
58 EXPERIMENT_NAME = "prey_predator"
59
60 MY_EXP_INIT_PARAMETERS = [{"type": "int", "name": "nb_preys_init",
61 "value": nb_preys_init},
62 {"type": "int", "name": "nb_predators_init",
63 "value": nb_predators_init}]
64
65 print("[MY_sequential_example.py Setting]",
66 " MY_SERVER_URL:", MY_SERVER_URL,
67 ", MY_SERVER_PORT:", MY_SERVER_PORT,
68 ", GAML_FILE_PATH_ON_SERVER: ", GAML_FILE_PATH_ON_SERVER,
69 ", EXPERIMENT_NAME: ", EXPERIMENT_NAME,
70 ", MY_EXP_INIT_PARAMETERS: ", MY_EXP_INIT_PARAMETERS)
71
72 client = GamaBaseClient(MY_SERVER_URL, MY_SERVER_PORT, message_handler)
73
74 print("connecting to Gama server")
75 await client.connect()
76
77 print("initialize a gaml model")
78 experiment_future = asyncio.get_running_loop().create_future()
79 #await client.load(GAML_FILE_PATH_ON_SERVER, EXPERIMENT_NAME, True, True, True, MY_EXP_INIT_PARAMETERS) # error
80 await client.load(file_path=GAML_FILE_PATH_ON_SERVER,
81 experiment_name=EXPERIMENT_NAME,
82 console=True, status=True, dialog=True, runtime=True,
83 parameters=MY_EXP_INIT_PARAMETERS)
84
85 gama_response = await experiment_future
86 print("*********************") # _PRINT_
87 print("gama_response.keys():", gama_response.keys())
88 print("gama_response:")
89 pprint.pprint(gama_response)
90 print("*********************")
91
92 try:
93 experiment_id = gama_response["content"]
94 except Exception as e:
95 print("error while initializing", gama_response, e)
96 return
97
98 print("initialization successful, running the model")
99 play_future = asyncio.get_running_loop().create_future()
100 await client.play(experiment_id)
101 gama_response = await play_future
102 if gama_response["type"] != MessageTypes.CommandExecutedSuccessfully.value:
103 print("error while trying to run the experiment", gama_response)
104 return
105 print("*********************") # _PRINT_
106 print("gama_response.keys():", gama_response.keys())
107 print("gama_response:")
108 pprint.pprint(gama_response)
109 print("*********************")
110
111 print("model running, waiting a bit")
112 await asyncio.sleep(2)
113
114 print("pausing the model")
115 pause_future = asyncio.get_running_loop().create_future()
116 await client.pause(experiment_id)
117 gama_response = await pause_future
118 if gama_response["type"] != MessageTypes.CommandExecutedSuccessfully.value:
119 print("Unable to pause the experiment", gama_response)
120 return
121 print("*********************") # _PRINT_
122 print("gama_response.keys():", gama_response.keys())
123 print("gama_response:")
124 pprint.pprint(gama_response)
125 print("*********************")
126
127 expression_future = asyncio.get_running_loop().create_future()
128 await client.expression(experiment_id, r"cycle")
129 gama_response = await expression_future
130 print("asking simulation the value of: cycle=", gama_response["content"])
131 print("*********************") # _PRINT_
132 print("gama_response.keys():", gama_response.keys())
133 print("gama_response:")
134 pprint.pprint(gama_response)
135 print("*********************")
136
137 expression_future = asyncio.get_running_loop().create_future()
138 await client.expression(experiment_id, r"nb_preys/nb_preys_init")
139 gama_response = await expression_future
140 print("asking simulation the value of: nb_preys/nb_preys_init=", gama_response["content"])
141 print("*********************") # _PRINT_
142 print("gama_response.keys():", gama_response.keys())
143 print("gama_response:")
144 pprint.pprint(gama_response)
145 print("*********************")
146
147 print("asking gama to run ",nb_more_steps," more steps of the experiment")
148 step_future = asyncio.get_running_loop().create_future()
149 await client.step(experiment_id, nb_more_steps, True)
150 #await client.step(experiment_id, 10, True)
151 gama_response = await step_future
152 if gama_response["type"] != MessageTypes.CommandExecutedSuccessfully.value:
153 print("Unable to execute 10 new steps in the experiment", gama_response)
154 return
155 print("*********************") # _PRINT_
156 print("gama_response.keys():", gama_response.keys())
157 print("gama_response:")
158 pprint.pprint(gama_response)
159 print("*********************")
160
161 expression_future = asyncio.get_running_loop().create_future()
162 await client.expression(experiment_id, r"cycle")
163 gama_response = await expression_future
164 print("asking simulation the value of: cycle=", gama_response["content"])
165 print("*********************") # _PRINT_
166 print("gama_response.keys():", gama_response.keys())
167 print("gama_response:")
168 pprint.pprint(gama_response)
169 print("*********************")
170
171 print("killing the simulation")
172 stop_future = asyncio.get_running_loop().create_future()
173 await client.stop(experiment_id)
174 gama_response = await stop_future
175 if gama_response["type"] != MessageTypes.CommandExecutedSuccessfully.value:
176 print("Unable to stop the experiment", gama_response)
177 return
178 print("*********************") # _PRINT_
179 print("gama_response.keys():", gama_response.keys())
180 print("gama_response:")
181 pprint.pprint(gama_response)
182 print("*********************")
183
184 # Added
185 print("killing gama-server")
186 exit_future = asyncio.get_running_loop().create_future()
187 await client.exit()
188 # no response
189 print("*********************") # _PRINT_
190 print("Command exit() done")
191 print("*********************")
192
193
194 if __name__ == "__main__":
195
196 parser = argparse.ArgumentParser()
197 parser.add_argument("-nb_preys_init", type=int, help="nb_preys_init")
198 parser.add_argument("-nb_predators_init", type=int, help="nb_predators_init")
199 parser.add_argument("-nb_more_steps", type=int, help="nb_more_steps")
200 args = parser.parse_args()
201 if (args.nb_preys_init is not None) and (args.nb_preys_init != 'None') :
202 nb_preys_init = args.nb_preys_init
203 if (args.nb_predators_init is not None) and (args.nb_predators_init != 'None') :
204 nb_predators_init = args.nb_predators_init
205 if (args.nb_more_steps is not None) and (args.nb_more_steps != 'None') :
206 nb_more_steps = args.nb_more_steps
207
208 print("[MY_sequential_example.py Parameters]",
209 " nb_preys_init (for MY_predatorPrey.gaml) :", nb_preys_init,
210 " nb_predators_init (for MY_predatorPrey.gaml) :", nb_predators_init,
211 ", nb_more_steps (for MY_sequential_example.py) :", nb_more_steps)
212
213 asyncio.run(main())