2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
18 #include "simple_window.h"
21 #include "manipulator.h"
22 #include "intelligence.h"
25 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
28 // ./main --task hit_shape.task 0 --action-mode=intelligent --load-file=dump.mem
30 //////////////////////////////////////////////////////////////////////
32 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
33 if(n_opt + n >= argc) {
34 cerr << "Missing argument for " << argv[n_opt] << "." << endl;
35 cerr << "Expecting " << help << "." << endl;
40 void print_help_and_exit(int e) {
41 cout << "Arguments:" << endl;
42 cout << " --no-window" << endl;
43 cout << " --nb-ticks=<int: number of ticks>" << endl;
44 cout << " --nb-training-iterations=<int: number of training iterations>" << endl;
45 cout << " --load-file=<filename: dump file>" << endl;
46 cout << " --save-file=<filename: dump file>" << endl;
47 cout << " --proportion-for-training=<float: proportion of samples for training>" << endl;
48 cout << " --action-mode=<idle|random|intelligent>" << endl;
49 cout << " --task <filename: task to load> <int: degree>" << endl;
53 //////////////////////////////////////////////////////////////////////
55 int main(int argc, char **argv) {
57 const int buffer_size = 256;
58 char intelligence_load_file[buffer_size] = "", intelligence_save_file[buffer_size] = "";
64 int nb_training_iterations = 10;
65 scalar_t proportion_for_training = -1;
67 Polygon *grabbed_polygon = 0;
68 scalar_t relative_grab_x = 0, relative_grab_y = 0;
71 bool press_shift = false;
72 enum { IDLE, RANDOM, INTELLIGENT } action_mode = IDLE;
73 bool got_event = true;
74 int current_action = 0;
76 scalar_t last_hand_x = 0, last_hand_y = 0;
77 Polygon *last_grabbing = 0;
78 bool no_window = false;
80 //////////////////////////////////////////////////////////////////////
81 // Parsing the shell arguments
82 //////////////////////////////////////////////////////////////////////
86 if(argc == 1 || strcmp(argv[i], "--help") == 0) print_help_and_exit(0);
88 else if(strcmp(argv[i], "--test") == 0) {
93 else if(strcmp(argv[i], "--task") == 0) {
94 check_opt(argc, argv, i, 2, "<filename: task to load> <int: degree>");
96 cerr << "Can not load two tasks." << endl;
99 task = load_task(argv[i+1]);
100 task_degree = atoi(argv[i+2]);
103 } else if(strncmp(argv[i], "--", 2) == 0) {
104 char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
105 char *o = argv[i]+2, *s = variable_name, *u = variable_value;
106 while(*o && *o != '=') *s++ = *o++;
109 while(*o) *u++ = *o++;
112 if(strcmp(variable_name, "nb-ticks") == 0)
113 nb_ticks = atoi(variable_value);
114 else if(strcmp(variable_name, "nb-training-iterations") == 0)
115 nb_training_iterations = atoi(variable_value);
116 else if(strcmp(variable_name, "proportion-for-training") == 0)
117 proportion_for_training = atof(variable_value);
118 else if(strcmp(variable_name, "no-window") == 0)
120 else if(strcmp(variable_name, "save-file") == 0)
121 strcpy(intelligence_save_file, variable_value);
122 else if(strcmp(variable_name, "load-file") == 0)
123 strcpy(intelligence_load_file, variable_value);
124 else if(strcmp(variable_name, "action-mode") == 0) {
125 if(strcmp(variable_value, "idle") == 0) action_mode = IDLE;
126 else if(strcmp(variable_value, "random") == 0) action_mode = RANDOM;
127 else if(strcmp(variable_value, "intelligent") == 0) action_mode = INTELLIGENT;
129 cerr << "The only known modes are idle, random and intelligent" << endl;
133 cerr << "Unknown option " << argv[i] << endl;
134 print_help_and_exit(1);
138 cerr << "Unknown option " << argv[i] << endl;
139 print_help_and_exit(1);
143 cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
144 cout << "$Id: main.cc,v 1.89 2007-06-16 13:51:53 fleuret Exp $" << endl;
147 task = load_task("dummy.task");
151 cout << "Loaded task " << task->name()
152 << " with degree " << task_degree << "/" << task->nb_degrees()
155 if(task_degree < 0 || task_degree >= task->nb_degrees()) {
156 cout << "Invalid degree: " << task_degree << "." << endl;
160 //////////////////////////////////////////////////////////////////////
161 // Various initializations
162 //////////////////////////////////////////////////////////////////////
164 Universe universe(100, task->width(), task->height());
165 task->init(&universe, task_degree);
166 Retina retina(&universe);
167 Manipulator manipulator(task);
168 manipulator.force_move(task->width()/2, task->height()/2);
169 retina.set_location(manipulator.hand_x(), manipulator.hand_y());
171 SimpleWindow *window_main = 0, *window_brain = 0;
175 MapConcatener sensory_map(2);
176 sensory_map.add_map(&retina);
177 sensory_map.add_map(&manipulator);
180 MapExpander expanded_map(1000);
181 expanded_map.set_input(&sensory_map);
184 Intelligence intelligence(&expanded_map, &manipulator, nb_ticks + 1, nb_training_iterations);
185 intelligence.update(0, 0.0);
187 if(intelligence_load_file[0]) {
188 cout << "Loading from " << intelligence_load_file << " ... " ;
190 ifstream in(intelligence_load_file);
192 cerr << "error reading " << intelligence_load_file << "." << endl;
195 intelligence.load(in);
196 cout << "done." << endl ;
200 window_main = new SimpleWindow("Universe (main window)", 4, 4, task->width(), task->height());
201 winfd = window_main->file_descriptor();
203 cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
204 << "objects." << endl;
205 window_brain = new SimpleWindow("Universe (brain)",
206 12 + task->width(), 4,
207 retina.width(), retina.height() + manipulator.parameter_height());
210 cout << "Started without windows." << endl;
215 scalar_t sum_reward = 0;
217 //////////////////////////////////////////////////////////////////////
219 //////////////////////////////////////////////////////////////////////
221 while(!quit && tick != nb_ticks) {
230 FD_SET (winfd, &fds);
232 tv.tv_usec = 5000; // 0.05s
233 r = select(winfd + 1, &fds, 0, 0, &tv);
240 cout << tick << " " << sum_reward << " \r"; cout.flush();
243 if(r == 0) { // No window event, thus it's the clock tick
247 bool changed = got_event;
250 switch(action_mode) {
254 current_action = manipulator.random_action();
257 current_action = intelligence.best_action();
258 // if(drand48() < 0.5) current_action = intelligence.best_action();
259 // else current_action = manipulator.random_action();
263 manipulator.do_action(current_action);
265 scalar_t dt = 1.0/scalar_t(nb_it);
266 for(int k = 0; k < nb_it; k++) {
267 manipulator.update(dt, &universe);
268 task->update(dt, &universe, &manipulator);
269 changed |= universe.update(dt);
274 changed |= manipulator.hand_x() != last_hand_x ||
275 manipulator.hand_y() != last_hand_y ||
276 manipulator.grabbing() != last_grabbing;
278 scalar_t reward = task->reward(&universe, &manipulator);
279 sum_reward += abs(reward);
280 intelligence.update(current_action, reward);
281 expanded_map.update_map();
284 last_hand_x = manipulator.hand_x();
285 last_hand_y = manipulator.hand_y();
286 last_grabbing = manipulator.grabbing();
287 retina.set_location(manipulator.hand_x(), manipulator.hand_y());
290 window_main->color(0.0, 0.0, 0.0);
292 task->draw(window_main);
293 universe.draw(window_main);
294 manipulator.draw_on_universe(window_main);
295 retina.draw_on_universe(window_main);
297 if(grabbed_polygon) {
299 x = int(grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y));
300 y = int(grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y));
301 window_main->color(0.0, 0.0, 0.0);
302 window_main->draw_line(x - delta, y, x + delta, y);
303 window_main->draw_line(x, y - delta, x, y + delta);
309 retina.draw_parameters(0, 0, window_brain);
310 manipulator.draw_parameters(0, retina.height() + 1, window_brain);
311 window_brain->show();
316 } else if(r > 0) { // We got window events, let's process them
320 if(FD_ISSET(winfd, &fds)) {
325 se = window_main->event();
329 case SimpleEvent::MOUSE_CLICK_PRESS:
335 manipulator.force_move(se.x, se.y);
336 manipulator.do_action(Manipulator::ACTION_GRAB);
338 grabbed_polygon = universe.pick_polygon(se.x, se.y);
339 if(grabbed_polygon) {
340 relative_grab_x = grabbed_polygon->relative_x(se.x, se.y);
341 relative_grab_y = grabbed_polygon->relative_y(se.x, se.y);
347 Polygon *g = universe.pick_polygon(se.x, se.y);
348 if(g) g->_theta += M_PI/32;
353 Polygon *g = universe.pick_polygon(se.x, se.y);
354 if(g) g->_theta -= M_PI/32;
361 case SimpleEvent::MOUSE_CLICK_RELEASE:
364 if(press_shift) manipulator.do_action(Manipulator::ACTION_RELEASE);
365 else grabbed_polygon = 0;
371 case SimpleEvent::MOUSE_MOTION:
373 if(press_shift) manipulator.force_move(se.x, se.y);
375 if(grabbed_polygon) {
376 scalar_t xf, yf, force_x, force_y, f, fmax = 100;
377 xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
378 yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
381 f = sqrt(sq(force_x) + sq(force_y));
382 if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
383 grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
390 case SimpleEvent::KEY_PRESS:
392 if(strcmp(se.key, "q") == 0) quit = true;
393 else if(strcmp(se.key, "s") == 0) {
394 retina.save_as_ppm("/tmp/retina.ppm");
395 cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
397 ofstream out("/tmp/universe.fig");
398 universe.print_fig(out);
401 else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = true;
403 else if(strcmp(se.key, "Up") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_UP);
404 else if(strcmp(se.key, "Right") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
405 else if(strcmp(se.key, "Down") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
406 else if(strcmp(se.key, "Left") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
407 else if(strcmp(se.key, "g") == 0) manipulator.do_action(Manipulator::ACTION_GRAB);
408 else if(strcmp(se.key, "r") == 0) manipulator.do_action(Manipulator::ACTION_RELEASE);
410 else if(strcmp(se.key, "space") == 0) {
411 switch(action_mode) {
413 action_mode = RANDOM;
414 cout << "Switched to random mode" << endl;
417 action_mode = INTELLIGENT;
418 cout << "Switched to intelligent mode" << endl;
421 cout << "Switched to idle mode" << endl;
427 else cout << "Undefined key " << se.key << endl;
431 case SimpleEvent::KEY_RELEASE:
433 if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = false;
441 } while(se.type != SimpleEvent::NO_EVENT);
443 cerr << "Error on select: " << strerror(errno) << endl;
449 if(proportion_for_training > 0) {
450 cout << "Learning ... "; cout.flush();
451 intelligence.learn(proportion_for_training);
452 cout << "done." << endl;
455 if(intelligence_save_file[0]) {
456 cout << "Saving to " << intelligence_save_file << endl; cout.flush();
457 ofstream os(intelligence_save_file);
459 cerr << "error writing " << intelligence_save_file << "." << endl;
462 cout << "done." << endl;
463 intelligence.save(os);