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"
28 static cairo_status_t write_cairo_to_file(void *closure,
29 const unsigned char *data,
30 unsigned int length) {
31 fwrite(data, 1, length, (FILE *) closure);
32 return CAIRO_STATUS_SUCCESS;
35 void generate_png(int width, int height, Universe *universe, FILE *file) {
37 cairo_surface_t *image;
38 cairo_t* context_resource;
40 data = new unsigned char [width * height * depth];
42 image = cairo_image_surface_create_for_data(data,
48 context_resource = cairo_create(image);
50 cairo_set_source_rgb(context_resource, 1.0, 1.0, 1.0);
51 cairo_rectangle(context_resource, 0, 0, width, height);
52 cairo_fill(context_resource);
54 universe->draw(context_resource);
56 cairo_surface_write_to_png_stream(image, write_cairo_to_file, file);
58 cairo_destroy(context_resource);
59 cairo_surface_destroy(image);
66 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
69 // ./main --task hit_shape.task 0 --action-mode=intelligent --load-file=dump.mem
71 //////////////////////////////////////////////////////////////////////
73 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
74 if(n_opt + n >= argc) {
75 cerr << "Missing argument for " << argv[n_opt] << "." << endl;
76 cerr << "Expecting " << help << "." << endl;
81 void print_help_and_exit(int e) {
82 cout << "Arguments:" << endl;
83 cout << " --no-window" << endl;
84 cout << " --nb-ticks=<int: number of ticks>" << endl;
85 cout << " --nb-training-iterations=<int: number of training iterations>" << endl;
86 cout << " --load-file=<filename: dump file>" << endl;
87 cout << " --save-file=<filename: dump file>" << endl;
88 cout << " --proportion-for-training=<float: proportion of samples for training>" << endl;
89 cout << " --action-mode=<idle|random|intelligent>" << endl;
90 cout << " --task <filename: task to load> <int: degree>" << endl;
94 //////////////////////////////////////////////////////////////////////
96 int main(int argc, char **argv) {
98 const int buffer_size = 256;
99 char intelligence_load_file[buffer_size] = "", intelligence_save_file[buffer_size] = "";
104 int nb_ticks = 10000;
105 int nb_training_iterations = 10;
106 scalar_t proportion_for_training = -1;
108 Polygon *grabbed_polygon = 0;
109 scalar_t relative_grab_x = 0, relative_grab_y = 0;
112 bool press_shift = false;
113 enum { IDLE, RANDOM, INTELLIGENT } action_mode = IDLE;
114 bool got_event = true;
115 int current_action = 0;
117 scalar_t last_hand_x = 0, last_hand_y = 0;
118 Polygon *last_grabbing = 0;
119 bool no_window = false;
121 //////////////////////////////////////////////////////////////////////
122 // Parsing the shell arguments
123 //////////////////////////////////////////////////////////////////////
127 if(argc == 1 || strcmp(argv[i], "--help") == 0) print_help_and_exit(0);
129 else if(strcmp(argv[i], "--test") == 0) {
134 else if(strcmp(argv[i], "--task") == 0) {
135 check_opt(argc, argv, i, 2, "<filename: task to load> <int: degree>");
137 cerr << "Can not load two tasks." << endl;
140 task = load_task(argv[i+1]);
141 task_degree = atoi(argv[i+2]);
144 } else if(strncmp(argv[i], "--", 2) == 0) {
145 char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
146 char *o = argv[i]+2, *s = variable_name, *u = variable_value;
147 while(*o && *o != '=') *s++ = *o++;
150 while(*o) *u++ = *o++;
153 if(strcmp(variable_name, "nb-ticks") == 0)
154 nb_ticks = atoi(variable_value);
155 else if(strcmp(variable_name, "nb-training-iterations") == 0)
156 nb_training_iterations = atoi(variable_value);
157 else if(strcmp(variable_name, "proportion-for-training") == 0)
158 proportion_for_training = atof(variable_value);
159 else if(strcmp(variable_name, "no-window") == 0)
161 else if(strcmp(variable_name, "save-file") == 0)
162 strcpy(intelligence_save_file, variable_value);
163 else if(strcmp(variable_name, "load-file") == 0)
164 strcpy(intelligence_load_file, variable_value);
165 else if(strcmp(variable_name, "action-mode") == 0) {
166 if(strcmp(variable_value, "idle") == 0) action_mode = IDLE;
167 else if(strcmp(variable_value, "random") == 0) action_mode = RANDOM;
168 else if(strcmp(variable_value, "intelligent") == 0) action_mode = INTELLIGENT;
170 cerr << "The only known modes are idle, random and intelligent" << endl;
174 cerr << "Unknown option " << argv[i] << endl;
175 print_help_and_exit(1);
179 cerr << "Unknown option " << argv[i] << endl;
180 print_help_and_exit(1);
184 cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
187 task = load_task("dummy.task");
191 cout << "Loaded task " << task->name()
192 << " with degree " << task_degree << "/" << task->nb_degrees()
195 if(task_degree < 0 || task_degree >= task->nb_degrees()) {
196 cout << "Invalid degree: " << task_degree << "." << endl;
200 //////////////////////////////////////////////////////////////////////
201 // Various initializations
202 //////////////////////////////////////////////////////////////////////
204 Universe universe(100, task->width(), task->height());
205 task->init(&universe, task_degree);
206 Retina retina(&universe);
207 Manipulator manipulator(task);
208 manipulator.force_move(task->width()/2, task->height()/2);
209 retina.set_location(manipulator.hand_x(), manipulator.hand_y());
211 SimpleWindow *window_main = 0, *window_brain = 0;
213 cairo_t *cairo_cr = 0;
216 int window_main_fd = -1;
218 MapConcatener sensory_map(2);
219 sensory_map.add_map(&retina);
220 sensory_map.add_map(&manipulator);
223 MapExpander expanded_map(1000);
224 expanded_map.set_input(&sensory_map);
227 Intelligence intelligence(&expanded_map, &manipulator, nb_ticks + 1, nb_training_iterations);
228 intelligence.update(0, 0.0);
230 if(intelligence_load_file[0]) {
231 cout << "Loading from " << intelligence_load_file << " ... " ;
233 ifstream in(intelligence_load_file);
235 cerr << "error reading " << intelligence_load_file << "." << endl;
238 intelligence.load(in);
239 cout << "done." << endl ;
243 cout << "Started without windows." << endl;
245 window_main = new SimpleWindow("Universe (main window)", 4, 4, task->width(), task->height());
246 window_main_fd = window_main->file_descriptor();
249 cairo_cr = window_main->get_cairo_context_resource();
251 cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
252 << "objects." << endl;
253 window_brain = new SimpleWindow("Universe (brain)",
254 12 + task->width(), 4,
255 retina.width(), retina.height() + manipulator.parameter_height());
261 scalar_t sum_reward = 0;
263 //////////////////////////////////////////////////////////////////////
265 //////////////////////////////////////////////////////////////////////
267 while(!quit && tick != nb_ticks) {
276 FD_SET (window_main_fd, &fds);
278 tv.tv_usec = 5000; // 0.05s
279 r = select(window_main_fd + 1, &fds, 0, 0, &tv);
286 cout << tick << " " << sum_reward << " \r"; cout.flush();
289 if(r == 0) { // No window event, thus it's the clock tick
293 bool changed = got_event;
296 switch(action_mode) {
300 current_action = manipulator.random_action();
303 current_action = intelligence.best_action();
304 // if(drand48() < 0.5) current_action = intelligence.best_action();
305 // else current_action = manipulator.random_action();
309 manipulator.do_action(current_action);
311 scalar_t dt = 1.0/scalar_t(nb_it);
312 for(int k = 0; k < nb_it; k++) {
313 manipulator.update(dt, &universe);
314 task->update(dt, &universe, &manipulator);
315 changed |= universe.update(dt);
320 changed |= manipulator.hand_x() != last_hand_x ||
321 manipulator.hand_y() != last_hand_y ||
322 manipulator.grabbing() != last_grabbing;
324 scalar_t reward = task->reward(&universe, &manipulator);
325 sum_reward += abs(reward);
326 intelligence.update(current_action, reward);
327 expanded_map.update_map();
330 last_hand_x = manipulator.hand_x();
331 last_hand_y = manipulator.hand_y();
332 last_grabbing = manipulator.grabbing();
334 retina.set_location(manipulator.hand_x(),
335 manipulator.hand_y());
338 window_main->color(0.0, 0.0, 0.0);
339 window_main->color(1.0, 1.0, 1.0);
341 task->draw(window_main);
344 universe.draw(cairo_cr);
346 universe.draw(window_main);
349 manipulator.draw_on_universe(window_main);
350 retina.draw_on_universe(window_main);
352 if(grabbed_polygon) {
354 x = int(grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y));
355 y = int(grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y));
356 window_main->color(0.0, 0.0, 0.0);
357 window_main->draw_line(x - delta, y, x + delta, y);
358 window_main->draw_line(x, y - delta, x, y + delta);
364 retina.draw_parameters(0, 0, window_brain);
365 manipulator.draw_parameters(0, retina.height() + 1, window_brain);
366 window_brain->show();
371 } else if(r > 0) { // We got window events, let's process them
375 if(FD_ISSET(window_main_fd, &fds)) {
380 se = window_main->event();
384 case SimpleEvent::MOUSE_CLICK_PRESS:
390 manipulator.force_move(se.x, se.y);
391 manipulator.do_action(Manipulator::ACTION_GRAB);
393 grabbed_polygon = universe.pick_polygon(se.x, se.y);
394 if(grabbed_polygon) {
395 relative_grab_x = grabbed_polygon->relative_x(se.x, se.y);
396 relative_grab_y = grabbed_polygon->relative_y(se.x, se.y);
402 Polygon *g = universe.pick_polygon(se.x, se.y);
403 if(g) g->_theta += M_PI/32;
408 Polygon *g = universe.pick_polygon(se.x, se.y);
409 if(g) g->_theta -= M_PI/32;
416 case SimpleEvent::MOUSE_CLICK_RELEASE:
419 if(press_shift) manipulator.do_action(Manipulator::ACTION_RELEASE);
420 else grabbed_polygon = 0;
426 case SimpleEvent::MOUSE_MOTION:
429 manipulator.force_move(se.x, se.y);
430 } else if(grabbed_polygon) {
431 scalar_t xf, yf, force_x, force_y, f, fmax = 100;
432 xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
433 yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
436 f = sqrt(sq(force_x) + sq(force_y));
437 if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
438 grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
444 case SimpleEvent::KEY_PRESS:
446 if(strcmp(se.key, "q") == 0) {
450 else if(strcmp(se.key, "s") == 0) {
452 retina.save_as_ppm("/tmp/retina.ppm");
453 cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
456 XFigTracer tracer("/tmp/universe.fig");
457 universe.print_xfig(&tracer);
462 FILE *file = fopen("/tmp/universe.png", "w");
463 generate_png(task->width(), task->height(), &universe, file);
464 // generate_png(task->width(), task->height(), &universe, "/tmp/universe.png");
465 cout << "Universe image saved in /tmp/universe.png" << endl;
471 else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) {
475 else if(strcmp(se.key, "Up") == 0) {
476 manipulator.do_action(Manipulator::ACTION_MOVE_UP);
479 else if(strcmp(se.key, "Right") == 0) {
480 manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
483 else if(strcmp(se.key, "Down") == 0) {
484 manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
487 else if(strcmp(se.key, "Left") == 0) {
488 manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
491 else if(strcmp(se.key, "g") == 0) {
492 manipulator.do_action(Manipulator::ACTION_GRAB);
495 else if(strcmp(se.key, "r") == 0) {
496 manipulator.do_action(Manipulator::ACTION_RELEASE);
499 else if(strcmp(se.key, "space") == 0) {
500 switch(action_mode) {
502 action_mode = RANDOM;
503 cout << "Switched to random mode" << endl;
506 action_mode = INTELLIGENT;
507 cout << "Switched to intelligent mode" << endl;
510 cout << "Switched to idle mode" << endl;
516 else cout << "Undefined key " << se.key << endl;
520 case SimpleEvent::KEY_RELEASE:
522 if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = false;
530 } while(se.type != SimpleEvent::NO_EVENT);
532 cerr << "Error on select: " << strerror(errno) << endl;
538 if(proportion_for_training > 0) {
539 cout << "Learning ... "; cout.flush();
540 intelligence.learn(proportion_for_training);
541 cout << "done." << endl;
544 if(intelligence_save_file[0]) {
545 cout << "Saving to " << intelligence_save_file << endl; cout.flush();
546 ofstream os(intelligence_save_file);
548 cerr << "error writing " << intelligence_save_file << "." << endl;
551 cout << "done." << endl;
552 intelligence.save(os);