Added gravity.
[universe.git] / main.cc
1
2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
4
5 #include <iostream>
6 #include <fstream>
7 #include <cmath>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <errno.h>
12 #include <string.h>
13
14 using namespace std;
15
16 #include "misc.h"
17 #include "task.h"
18 #include "simple_window.h"
19 #include "universe.h"
20 #include "plotter.h"
21 #include "retina.h"
22 #include "manipulator.h"
23 #include "intelligence.h"
24 #include "canvas_cairo.h"
25
26 #include "xfig_tracer.h"
27
28 void generate_png(Universe *universe, scalar_t scale, FILE *file) {
29   CanvasCairo canvas(scale, universe->width(), universe->height());
30   universe->draw(&canvas);
31   canvas.write_png(file);
32 }
33
34 // To train
35 // ./main --task hit_shape.task 0 --action-mode=random --nb-ticks=5000 --proportion-for-training=0.5 --save-file=dump.mem --no-window
36
37 // To test
38 // ./main --task hit_shape.task 0 --action-mode=intelligent --load-file=dump.mem
39
40 //////////////////////////////////////////////////////////////////////
41
42 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
43   if(n_opt + n >= argc) {
44     cerr << "Missing argument for " << argv[n_opt] << "." << endl;
45     cerr << "Expecting " << help << "." << endl;
46     exit(1);
47   }
48 }
49
50 void print_help_and_exit(int e) {
51   cout << "Arguments:" << endl;
52   cout << "  --no-window" << endl;
53   cout << "  --nb-ticks=<int: number of ticks>" << endl;
54   cout << "  --nb-training-iterations=<int: number of training iterations>" << endl;
55   cout << "  --load-file=<filename: dump file>" << endl;
56   cout << "  --save-file=<filename: dump file>" << endl;
57   cout << "  --proportion-for-training=<float: proportion of samples for training>" << endl;
58   cout << "  --action-mode=<idle|random|intelligent>" << endl;
59   cout << "  --task <filename: task to load> <int: degree>" << endl;
60   exit(e);
61 }
62
63 //////////////////////////////////////////////////////////////////////
64
65 int main(int argc, char **argv) {
66
67   const int buffer_size = 256;
68   char intelligence_load_file[buffer_size] = "", intelligence_save_file[buffer_size] = "";
69
70   Task *task = 0;
71   int task_degree = 0;
72
73   int nb_ticks = 10000;
74   int nb_training_iterations = 10;
75   scalar_t proportion_for_training = -1;
76
77   Polygon *grabbed_polygon = 0;
78   scalar_t relative_grab_x = 0, relative_grab_y = 0;
79
80   bool quit = false;
81   bool press_shift = false;
82   enum { IDLE, RANDOM, INTELLIGENT } action_mode = IDLE;
83   bool got_event = true;
84   int current_action = 0;
85
86   scalar_t last_hand_x = 0, last_hand_y = 0;
87   Polygon *last_grabbing = 0;
88   bool no_window = false;
89
90   //////////////////////////////////////////////////////////////////////
91   //                    Parsing the shell arguments
92   //////////////////////////////////////////////////////////////////////
93
94   int i = 1;
95   while(i < argc) {
96     if(argc == 1 || strcmp(argv[i], "--help") == 0) print_help_and_exit(0);
97
98     else if(strcmp(argv[i], "--test") == 0) {
99       test_approximer();
100       exit(0);
101     }
102
103     else if(strcmp(argv[i], "--task") == 0) {
104       check_opt(argc, argv, i, 2, "<filename: task to load> <int: degree>");
105       if(task) {
106         cerr << "Can not load two tasks." << endl;
107         exit(1);
108       }
109       task = load_task(argv[i+1]);
110       task_degree = atoi(argv[i+2]);
111       i += 3;
112
113     } else if(strncmp(argv[i], "--", 2) == 0) {
114       char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
115       char *o = argv[i]+2, *s = variable_name, *u = variable_value;
116       while(*o && *o != '=') *s++ = *o++;
117       if(*o) {
118         o++;
119         while(*o) *u++ = *o++;
120       }
121
122       if(strcmp(variable_name, "nb-ticks") == 0)
123         nb_ticks = atoi(variable_value);
124       else if(strcmp(variable_name, "nb-training-iterations") == 0)
125         nb_training_iterations = atoi(variable_value);
126       else if(strcmp(variable_name, "proportion-for-training") == 0)
127         proportion_for_training = atof(variable_value);
128       else if(strcmp(variable_name, "no-window") == 0)
129         no_window = true;
130       else if(strcmp(variable_name, "save-file") == 0)
131         strcpy(intelligence_save_file, variable_value);
132       else if(strcmp(variable_name, "load-file") == 0)
133         strcpy(intelligence_load_file, variable_value);
134       else if(strcmp(variable_name, "action-mode") == 0) {
135         if(strcmp(variable_value, "idle") == 0) action_mode = IDLE;
136         else if(strcmp(variable_value, "random") == 0) action_mode = RANDOM;
137         else if(strcmp(variable_value, "intelligent") == 0) action_mode = INTELLIGENT;
138         else {
139           cerr << "The only known modes are idle, random and intelligent" << endl;
140           exit(1);
141         }
142       } else {
143         cerr << "Unknown option " << argv[i] << endl;
144         print_help_and_exit(1);
145       }
146       i++;
147     } else {
148       cerr << "Unknown option " << argv[i] << endl;
149       print_help_and_exit(1);
150     }
151   }
152
153   cout << "FlatLand, a toy universe for goal-planning experiments." << endl;
154
155   if(!task) {
156     task = load_task("dummy.so");
157     task_degree = 0;
158   }
159
160   cout << "Loaded task " << task->name()
161        << " with degree " << task_degree << "/" << task->nb_degrees()
162        << endl;
163
164   if(task_degree < 0 || task_degree >= task->nb_degrees()) {
165     cout << "Invalid degree: " << task_degree << "." << endl;
166     exit(1);
167   }
168
169   //////////////////////////////////////////////////////////////////////
170   //                      Various initializations
171   //////////////////////////////////////////////////////////////////////
172
173   Universe universe(100, task->width(), task->height());
174   task->init(&universe, task_degree);
175   Retina retina(&universe);
176   Manipulator manipulator(task);
177   manipulator.force_move(task->width()/2, task->height()/2);
178   retina.set_location(manipulator.hand_x(), manipulator.hand_y());
179
180   SimpleWindow *window_main = 0, *window_brain = 0;
181   int window_main_fd = -1;
182
183 #ifdef CAIRO_SUPPORT
184   // cairo_t *window_main_cairo_cr = 0;
185 #endif
186
187   MapConcatener sensory_map(2);
188   sensory_map.add_map(&retina);
189   sensory_map.add_map(&manipulator);
190   sensory_map.init();
191
192   MapExpander expanded_map(1000);
193   expanded_map.set_input(&sensory_map);
194   expanded_map.init();
195
196   Intelligence intelligence(&expanded_map, &manipulator, nb_ticks + 1, nb_training_iterations);
197   intelligence.update(0, 0.0);
198
199   if(intelligence_load_file[0]) {
200     cout << "Loading from " << intelligence_load_file << " ... " ;
201     cout.flush();
202     ifstream in(intelligence_load_file);
203     if(in.fail()) {
204       cerr << "error reading " << intelligence_load_file << "." << endl;
205       exit(1);
206     }
207     intelligence.load(in);
208     cout << "done." << endl ;
209   }
210
211   if(no_window) {
212     cout << "Started without windows." << endl;
213   } else {
214     window_main = new SimpleWindow("Universe (main window)", 4, 4, task->width(), task->height());
215     window_main_fd = window_main->file_descriptor();
216     window_main->map();
217 #ifdef CAIRO_SUPPORT
218     // window_main_cairo_cr = window_main->get_cairo_context_resource();
219 #endif
220     cout << "When the main window has the focus, press `q' to quit and click and drag to move" << endl
221          << "objects." << endl;
222     window_brain = new SimpleWindow("Universe (brain)",
223                                     12 + task->width(), 4,
224                                     retina.width(), retina.height() + manipulator.parameter_height());
225     window_brain->map();
226   }
227
228   int tick = 0;
229   time_t last_t = 0;
230   scalar_t sum_reward = 0;
231
232   //////////////////////////////////////////////////////////////////////
233   //                         The main loop
234   //////////////////////////////////////////////////////////////////////
235
236   while(!quit && tick != nb_ticks) {
237
238     int r;
239     fd_set fds;
240
241
242     if(window_main) {
243       struct timeval tv;
244       FD_ZERO (&fds);
245       FD_SET (window_main_fd, &fds);
246       tv.tv_sec = 0;
247       tv.tv_usec = 5000; // 0.05s
248       r = select(window_main_fd + 1, &fds, 0, 0, &tv);
249     } else r = 0;
250
251     time_t t = time(0);
252
253     if(t > last_t) {
254       last_t = t;
255       cout << tick << " " << sum_reward << "              \r"; cout.flush();
256     }
257
258     if(r == 0) { // No window event, thus it's the clock tick
259
260         int nb_it = 10;
261
262         bool changed = got_event;
263         got_event = false;
264
265         switch(action_mode) {
266         case IDLE:
267           break;
268         case RANDOM:
269           current_action = manipulator.random_action();
270           break;
271         case INTELLIGENT:
272           current_action = intelligence.best_action();
273           //           if(drand48() < 0.5) current_action = intelligence.best_action();
274           //           else                current_action = manipulator.random_action();
275           break;
276         }
277
278         manipulator.do_action(current_action);
279
280         scalar_t dt = 1.0/scalar_t(nb_it);
281         for(int k = 0; k < nb_it; k++) {
282           manipulator.update(dt, &universe);
283           task->update(dt, &universe, &manipulator);
284           universe.apply_gravity(dt, 0.0, 2.0);
285           changed |= universe.update(dt);
286         }
287
288         tick++;
289
290         changed |= manipulator.hand_x() != last_hand_x ||
291           manipulator.hand_y() != last_hand_y ||
292           manipulator.grabbing() != last_grabbing;
293
294         scalar_t reward = task->reward(&universe, &manipulator);
295         sum_reward += abs(reward);
296         intelligence.update(current_action, reward);
297         expanded_map.update_map();
298
299         if(changed) {
300           last_hand_x = manipulator.hand_x();
301           last_hand_y = manipulator.hand_y();
302           last_grabbing = manipulator.grabbing();
303
304           retina.set_location(manipulator.hand_x(),
305                               manipulator.hand_y());
306
307           if(window_main) {
308             window_main->color(0.0, 0.0, 0.0);
309             window_main->color(1.0, 1.0, 1.0);
310             window_main->fill();
311
312 // #ifdef CAIRO_SUPPORT
313             // CanvasCairo cc;
314             // cc._context_resource = window_main_cairo_cr;
315             // universe.draw(&cc);
316 // #else
317             universe.draw(window_main);
318 // #endif
319
320             task->draw(window_main);
321             manipulator.draw_on_universe(window_main);
322             retina.draw_on_universe(window_main);
323
324             if(grabbed_polygon) {
325               int x, y, delta = 3;
326               x = int(grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y));
327               y = int(grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y));
328               window_main->color(0.0, 0.0, 0.0);
329               window_main->draw_line(x - delta, y, x + delta, y);
330               window_main->draw_line(x, y - delta, x, y + delta);
331             }
332
333             window_main->show();
334
335             if(window_brain) {
336               retina.draw_parameters(0, 0, window_brain);
337               manipulator.draw_parameters(0, retina.height() + 1, window_brain);
338               window_brain->show();
339             }
340           }
341         }
342
343     } else if(r > 0) { // We got window events, let's process them
344
345       got_event = true;
346
347       if(FD_ISSET(window_main_fd, &fds)) {
348
349         SimpleEvent se;
350
351         do {
352           se = window_main->event();
353
354           switch(se.type) {
355
356           case SimpleEvent::MOUSE_CLICK_PRESS:
357             {
358               switch(se.button) {
359
360               case 1:
361                 if(press_shift) {
362                   manipulator.force_move(se.x, se.y);
363                   manipulator.do_action(Manipulator::ACTION_GRAB);
364                 } else {
365                   grabbed_polygon = universe.pick_polygon(se.x, se.y);
366                   if(grabbed_polygon) {
367                     relative_grab_x = grabbed_polygon->relative_x(se.x, se.y);
368                     relative_grab_y = grabbed_polygon->relative_y(se.x, se.y);
369                   }
370                 }
371                 break;
372               case 4:
373                 {
374                   Polygon *g = universe.pick_polygon(se.x, se.y);
375                   if(g) g->_theta += M_PI/32;
376                 }
377                 break;
378               case 5:
379                 {
380                   Polygon *g = universe.pick_polygon(se.x, se.y);
381                   if(g) g->_theta -= M_PI/32;
382                 }
383                 break;
384               }
385             }
386             break;
387
388           case SimpleEvent::MOUSE_CLICK_RELEASE:
389             switch(se.button) {
390             case 1:
391               if(press_shift) manipulator.do_action(Manipulator::ACTION_RELEASE);
392               else            grabbed_polygon = 0;
393               break;
394             default:
395               break;
396             }
397
398           case SimpleEvent::MOUSE_MOTION:
399             {
400               if(press_shift) {
401                 manipulator.force_move(se.x, se.y);
402               } else if(grabbed_polygon) {
403                 scalar_t xf, yf, force_x, force_y, f, fmax = 100;
404                 xf = grabbed_polygon->absolute_x(relative_grab_x, relative_grab_y);
405                 yf = grabbed_polygon->absolute_y(relative_grab_x, relative_grab_y);
406                 force_x = se.x - xf;
407                 force_y = se.y - yf;
408                 f = sqrt(sq(force_x) + sq(force_y));
409                 if(f > fmax) { force_x = (force_x * fmax)/f; force_y = (force_y * fmax)/f; }
410                 grabbed_polygon->apply_force(0.1, xf, yf, force_x, force_y);
411               }
412               break;
413             }
414             break;
415
416           case SimpleEvent::KEY_PRESS:
417             {
418               if(strcmp(se.key, "q") == 0) {
419                 quit = true;
420               }
421
422               else if(strcmp(se.key, "s") == 0) {
423
424                 retina.save_as_ppm("/tmp/retina.ppm");
425                 cout << "Retina screen shot saved in /tmp/retina.ppm" << endl;
426
427                 {
428                   Plotter plotter(int(universe.width()), int(universe.height()), 4);
429                   plotter.save_as_ppm(&universe, "/tmp/plotter.ppm", 16);
430                 }
431
432 #ifdef XFIG_SUPPORT
433                 {
434                   XFigTracer tracer("/tmp/universe.fig");
435                   universe.print_xfig(&tracer);
436                 }
437 #endif
438
439 #ifdef CAIRO_SUPPORT
440                 {
441                   FILE *file = fopen("/tmp/universe.png", "w");
442                   generate_png(&universe, 0.25, file);
443                   // generate_png(task->width(), task->height(), &universe, "/tmp/universe.png");
444                   cout << "Universe image saved in /tmp/universe.png" << endl;
445                 }
446 #endif
447
448               }
449
450               else if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) {
451                 press_shift = true;
452               }
453
454               else if(strcmp(se.key, "Up") == 0) {
455                 manipulator.do_action(Manipulator::ACTION_MOVE_UP);
456               }
457
458               else if(strcmp(se.key, "Right") == 0) {
459                 manipulator.do_action(Manipulator::ACTION_MOVE_RIGHT);
460               }
461
462               else if(strcmp(se.key, "Down") == 0) {
463                 manipulator.do_action(Manipulator::ACTION_MOVE_DOWN);
464               }
465
466               else if(strcmp(se.key, "Left") == 0) {
467                 manipulator.do_action(Manipulator::ACTION_MOVE_LEFT);
468               }
469
470               else if(strcmp(se.key, "g") == 0) {
471                 manipulator.do_action(Manipulator::ACTION_GRAB);
472               }
473
474               else if(strcmp(se.key, "r") == 0) {
475                 manipulator.do_action(Manipulator::ACTION_RELEASE);
476               }
477
478               else if(strcmp(se.key, "space") == 0) {
479                 switch(action_mode) {
480                 case IDLE:
481                   action_mode = RANDOM;
482                   cout << "Switched to random mode" << endl;
483                   break;
484                 case RANDOM:
485                   action_mode = INTELLIGENT;
486                   cout << "Switched to intelligent mode" << endl;
487                   break;
488                 case INTELLIGENT:
489                   cout << "Switched to idle mode" << endl;
490                   action_mode = IDLE;
491                   break;
492                 }
493               }
494
495               else cout << "Undefined key " << se.key << endl;
496             }
497             break;
498
499           case SimpleEvent::KEY_RELEASE:
500             {
501               if(strcmp(se.key, "Shift_L") == 0 || strcmp(se.key, "Shift_R") == 0) press_shift = false;
502             }
503             break;
504
505           default:
506             break;
507
508           }
509         } while(se.type != SimpleEvent::NO_EVENT);
510       } else {
511         cerr << "Error on select: " << strerror(errno) << endl;
512         exit(1);
513       }
514     }
515   }
516
517   if(proportion_for_training > 0) {
518     cout << "Learning ... "; cout.flush();
519     intelligence.learn(proportion_for_training);
520     cout << "done." << endl;
521   }
522
523   if(intelligence_save_file[0]) {
524     cout << "Saving to " << intelligence_save_file << endl; cout.flush();
525     ofstream os(intelligence_save_file);
526     if(os.fail()) {
527       cerr << "error writing " << intelligence_save_file << "." << endl;
528       exit(1);
529     }
530     cout << "done." << endl;
531     intelligence.save(os);
532   }
533
534   delete window_brain;
535   delete window_main;
536
537 }