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"
23 #include "xfig_tracer.h"
26 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
29 // ./main --task hit_shape.task 0 --action-mode=intelligent --load-file=dump.mem
31 //////////////////////////////////////////////////////////////////////
33 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
34 if(n_opt + n >= argc) {
35 cerr << "Missing argument for " << argv[n_opt] << "." << endl;
36 cerr << "Expecting " << help << "." << endl;
41 void print_help_and_exit(int e) {
42 cout << "Arguments:" << endl;
43 cout << " --no-window" << endl;
44 cout << " --nb-ticks=<int: number of ticks>" << endl;
45 cout << " --nb-training-iterations=<int: number of training iterations>" << endl;
46 cout << " --load-file=<filename: dump file>" << endl;
47 cout << " --save-file=<filename: dump file>" << endl;
48 cout << " --proportion-for-training=<float: proportion of samples for training>" << endl;
49 cout << " --action-mode=<idle|random|intelligent>" << endl;
50 cout << " --task <filename: task to load> <int: degree>" << endl;
54 //////////////////////////////////////////////////////////////////////
56 int main(int argc, char **argv) {
58 const int buffer_size = 256;
59 char intelligence_load_file[buffer_size] = "", intelligence_save_file[buffer_size] = "";
65 int nb_training_iterations = 10;
66 scalar_t proportion_for_training = -1;
68 Polygon *grabbed_polygon = 0;
69 scalar_t relative_grab_x = 0, relative_grab_y = 0;
72 bool press_shift = false;
73 enum { IDLE, RANDOM, INTELLIGENT } action_mode = IDLE;
74 bool got_event = true;
75 int current_action = 0;
77 scalar_t last_hand_x = 0, last_hand_y = 0;
78 Polygon *last_grabbing = 0;
79 bool no_window = false;
81 //////////////////////////////////////////////////////////////////////
82 // Parsing the shell arguments
83 //////////////////////////////////////////////////////////////////////
87 if(argc == 1 || strcmp(argv[i], "--help") == 0) print_help_and_exit(0);
89 else if(strcmp(argv[i], "--test") == 0) {
94 else if(strcmp(argv[i], "--task") == 0) {
95 check_opt(argc, argv, i, 2, "<filename: task to load> <int: degree>");
97 cerr << "Can not load two tasks." << endl;
100 task = load_task(argv[i+1]);
101 task_degree = atoi(argv[i+2]);
104 } else if(strncmp(argv[i], "--", 2) == 0) {
105 char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
106 char *o = argv[i]+2, *s = variable_name, *u = variable_value;
107 while(*o && *o != '=') *s++ = *o++;
110 while(*o) *u++ = *o++;
113 if(strcmp(variable_name, "nb-ticks") == 0)
114 nb_ticks = atoi(variable_value);
115 else if(strcmp(variable_name, "nb-training-iterations") == 0)
116 nb_training_iterations = atoi(variable_value);
117 else if(strcmp(variable_name, "proportion-for-training") == 0)
118 proportion_for_training = atof(variable_value);
119 else if(strcmp(variable_name, "no-window") == 0)
121 else if(strcmp(variable_name, "save-file") == 0)
122 strcpy(intelligence_save_file, variable_value);
123 else if(strcmp(variable_name, "load-file") == 0)
124 strcpy(intelligence_load_file, variable_value);
125 else if(strcmp(variable_name, "action-mode") == 0) {
126 if(strcmp(variable_value, "idle") == 0) action_mode = IDLE;
127 else if(strcmp(variable_value, "random") == 0) action_mode = RANDOM;
128 else if(strcmp(variable_value, "intelligent") == 0) action_mode = INTELLIGENT;
130 cerr << "The only known modes are idle, random and intelligent" << endl;
134 cerr << "Unknown option " << argv[i] << endl;
135 print_help_and_exit(1);
139 cerr << "Unknown option " << argv[i] << endl;
140 print_help_and_exit(1);
144 cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
145 cout << "$Id: main.cc,v 1.89 2007-06-16 13:51:53 fleuret Exp $" << endl;
148 task = load_task("dummy.task");
152 cout << "Loaded task " << task->name()
153 << " with degree " << task_degree << "/" << task->nb_degrees()
156 if(task_degree < 0 || task_degree >= task->nb_degrees()) {
157 cout << "Invalid degree: " << task_degree << "." << endl;
161 //////////////////////////////////////////////////////////////////////
162 // Various initializations
163 //////////////////////////////////////////////////////////////////////
165 Universe universe(100, task->width(), task->height());
166 task->init(&universe, task_degree);
167 Retina retina(&universe);
168 Manipulator manipulator(task);
169 manipulator.force_move(task->width()/2, task->height()/2);
170 retina.set_location(manipulator.hand_x(), manipulator.hand_y());
172 SimpleWindow *window_main = 0, *window_brain = 0;
176 MapConcatener sensory_map(2);
177 sensory_map.add_map(&retina);
178 sensory_map.add_map(&manipulator);
181 MapExpander expanded_map(1000);
182 expanded_map.set_input(&sensory_map);
185 Intelligence intelligence(&expanded_map, &manipulator, nb_ticks + 1, nb_training_iterations);
186 intelligence.update(0, 0.0);
188 if(intelligence_load_file[0]) {
189 cout << "Loading from " << intelligence_load_file << " ... " ;
191 ifstream in(intelligence_load_file);
193 cerr << "error reading " << intelligence_load_file << "." << endl;
196 intelligence.load(in);
197 cout << "done." << endl ;
201 window_main = new SimpleWindow("Universe (main window)", 4, 4, task->width(), task->height());
202 winfd = window_main->file_descriptor();
204 cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
205 << "objects." << endl;
206 window_brain = new SimpleWindow("Universe (brain)",
207 12 + task->width(), 4,
208 retina.width(), retina.height() + manipulator.parameter_height());
211 cout << "Started without windows." << endl;
216 scalar_t sum_reward = 0;
218 //////////////////////////////////////////////////////////////////////
220 //////////////////////////////////////////////////////////////////////
222 while(!quit && tick != nb_ticks) {
231 FD_SET (winfd, &fds);
233 tv.tv_usec = 5000; // 0.05s
234 r = select(winfd + 1, &fds, 0, 0, &tv);
241 cout << tick << " " << sum_reward << " \r"; cout.flush();
244 if(r == 0) { // No window event, thus it's the clock tick
248 bool changed = got_event;
251 switch(action_mode) {
255 current_action = manipulator.random_action();
258 current_action = intelligence.best_action();
259 // if(drand48() < 0.5) current_action = intelligence.best_action();
260 // else current_action = manipulator.random_action();
264 manipulator.do_action(current_action);
266 scalar_t dt = 1.0/scalar_t(nb_it);
267 for(int k = 0; k < nb_it; k++) {
268 manipulator.update(dt, &universe);
269 task->update(dt, &universe, &manipulator);
270 changed |= universe.update(dt);
275 changed |= manipulator.hand_x() != last_hand_x ||
276 manipulator.hand_y() != last_hand_y ||
277 manipulator.grabbing() != last_grabbing;
279 scalar_t reward = task->reward(&universe, &manipulator);
280 sum_reward += abs(reward);
281 intelligence.update(current_action, reward);
282 expanded_map.update_map();
285 last_hand_x = manipulator.hand_x();
286 last_hand_y = manipulator.hand_y();
287 last_grabbing = manipulator.grabbing();
288 retina.set_location(manipulator.hand_x(), manipulator.hand_y());
291 window_main->color(0.0, 0.0, 0.0);
293 task->draw(window_main);
294 universe.draw(window_main);
295 manipulator.draw_on_universe(window_main);
296 retina.draw_on_universe(window_main);
298 if(grabbed_polygon) {
300 x = int(grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y));
301 y = int(grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y));
302 window_main->color(0.0, 0.0, 0.0);
303 window_main->draw_line(x - delta, y, x + delta, y);
304 window_main->draw_line(x, y - delta, x, y + delta);
310 retina.draw_parameters(0, 0, window_brain);
311 manipulator.draw_parameters(0, retina.height() + 1, window_brain);
312 window_brain->show();
317 } else if(r > 0) { // We got window events, let's process them
321 if(FD_ISSET(winfd, &fds)) {
326 se = window_main->event();
330 case SimpleEvent::MOUSE_CLICK_PRESS:
336 manipulator.force_move(se.x, se.y);
337 manipulator.do_action(Manipulator::ACTION_GRAB);
339 grabbed_polygon = universe.pick_polygon(se.x, se.y);
340 if(grabbed_polygon) {
341 relative_grab_x = grabbed_polygon->relative_x(se.x, se.y);
342 relative_grab_y = grabbed_polygon->relative_y(se.x, se.y);
348 Polygon *g = universe.pick_polygon(se.x, se.y);
349 if(g) g->_theta += M_PI/32;
354 Polygon *g = universe.pick_polygon(se.x, se.y);
355 if(g) g->_theta -= M_PI/32;
362 case SimpleEvent::MOUSE_CLICK_RELEASE:
365 if(press_shift) manipulator.do_action(Manipulator::ACTION_RELEASE);
366 else grabbed_polygon = 0;
372 case SimpleEvent::MOUSE_MOTION:
374 if(press_shift) manipulator.force_move(se.x, se.y);
376 if(grabbed_polygon) {
377 scalar_t xf, yf, force_x, force_y, f, fmax = 100;
378 xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
379 yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
382 f = sqrt(sq(force_x) + sq(force_y));
383 if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
384 grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
391 case SimpleEvent::KEY_PRESS:
393 if(strcmp(se.key, "q") == 0) quit = true;
394 else if(strcmp(se.key, "s") == 0) {
395 retina.save_as_ppm("/tmp/retina.ppm");
396 cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
398 XFigTracer tracer("/tmp/universe.fig");
399 universe.print_fig(&tracer);
402 else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = true;
404 else if(strcmp(se.key, "Up") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_UP);
405 else if(strcmp(se.key, "Right") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
406 else if(strcmp(se.key, "Down") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
407 else if(strcmp(se.key, "Left") == 0) manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
408 else if(strcmp(se.key, "g") == 0) manipulator.do_action(Manipulator::ACTION_GRAB);
409 else if(strcmp(se.key, "r") == 0) manipulator.do_action(Manipulator::ACTION_RELEASE);
411 else if(strcmp(se.key, "space") == 0) {
412 switch(action_mode) {
414 action_mode = RANDOM;
415 cout << "Switched to random mode" << endl;
418 action_mode = INTELLIGENT;
419 cout << "Switched to intelligent mode" << endl;
422 cout << "Switched to idle mode" << endl;
428 else cout << "Undefined key " << se.key << endl;
432 case SimpleEvent::KEY_RELEASE:
434 if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = false;
442 } while(se.type != SimpleEvent::NO_EVENT);
444 cerr << "Error on select: " << strerror(errno) << endl;
450 if(proportion_for_training > 0) {
451 cout << "Learning ... "; cout.flush();
452 intelligence.learn(proportion_for_training);
453 cout << "done." << endl;
456 if(intelligence_save_file[0]) {
457 cout << "Saving to " << intelligence_save_file << endl; cout.flush();
458 ofstream os(intelligence_save_file);
460 cerr << "error writing " << intelligence_save_file << "." << endl;
463 cout << "done." << endl;
464 intelligence.save(os);