Cairo support works, both to draw in the window and to write as a png image in a...
[universe.git] / map.cc
1
2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
4
5 #include "map.h"
6
7 Map::Map() { parameters = 0; }
8
9 Map::~Map() { delete[] parameters; }
10
11 void Map::init(int np) {
12   nb_parameters = np;
13   parameters = new scalar_t[nb_parameters];
14   for(int n = 0; n < nb_parameters; n++) parameters[n] = 0;
15 }
16
17 MapConcatener::MapConcatener(int nb_max_maps) : _nb_max_maps(nb_max_maps), _nb_maps(0),
18                                                 _maps(new Map *[_nb_max_maps]) { }
19
20 MapConcatener::~MapConcatener() {
21   delete[] _maps;
22 }
23
24 void MapConcatener::add_map(Map *map) {
25   if(_nb_maps >= _nb_max_maps) abort();
26   _maps[_nb_maps++] = map;
27 }
28
29 void MapConcatener::init() {
30   int s = 0;
31   for(int m = 0; m < _nb_maps; m++) s += _maps[m]->nb_parameters;
32   Map::init(s);
33 }
34
35 void MapConcatener::update_map() {
36   for(int m = 0; m < _nb_maps; m++) _maps[m]->update_map();
37   int k = 0;
38   for(int m = 0; m < _nb_maps; m++) for(int l = 0; l < _maps[m]->nb_parameters; l++)
39     parameters[k++] = _maps[m]->parameters[l];
40 }
41
42 //////////////////////////////////////////////////////////////////////
43
44 MapExpander::MapExpander(int nb_units) : _nb_units(nb_units),
45                                          _unit_weights(0),
46                                          _input(0) { }
47
48 MapExpander::~MapExpander() {
49   delete[] _unit_weights;
50   delete[] _state_switch;
51 }
52
53 void MapExpander::load(istream &is) {
54   is.read((char *) &_nb_units, sizeof(_nb_units));
55   int input_size;
56   is.read((char *) &input_size, sizeof(input_size));
57
58   if(input_size != _input->nb_parameters) {
59     cerr << "Loaded map expander size missmatch." << endl;
60     exit(1);
61   }
62
63   _unit_weights = new scalar_t[_nb_units * 2 * (_input->nb_parameters + 1)];
64   parameters = new scalar_t[_nb_units];
65   _state_switch = new scalar_t[_nb_units];
66
67   is.read((char *) _unit_weights, sizeof(scalar_t) * _nb_units * 2 * (_input->nb_parameters + 1));
68
69   for(int u = 0; u < _nb_units; u++) {
70     _state_switch[u] = 0.0;
71     parameters[u] = 0.0;
72   }
73 }
74
75 void MapExpander::save(ostream &os) {
76   os.write((char *) &_nb_units, sizeof(_nb_units));
77   os.write((char *) &_input->nb_parameters, sizeof(_input->nb_parameters));
78   os.write((char *) _unit_weights, sizeof(scalar_t) * _nb_units * 2 * (_input->nb_parameters + 1));
79 }
80
81 void MapExpander::set_input(Map *input) {
82   ASSERT(!_input, "You can not set the input of an expanding map twice.");
83   _input = input;
84 }
85
86 void MapExpander::init() {
87   ASSERT(!_unit_weights, "You can not initialize a MapExpander twice.");
88   _unit_weights = new scalar_t[_nb_units * 2 * (_input->nb_parameters + 1)];
89   _state_switch = new scalar_t[_nb_units];
90   for(int k = 0; k < _nb_units * 2 * (_input->nb_parameters + 1); k++)
91     _unit_weights[k] = 2 * (drand48() - 0.5);
92   Map::init(_nb_units);
93   update_map();
94 }
95
96 void MapExpander::update_map() {
97   ASSERT(_unit_weights, "You have to call MapExpander::init() before using it.");
98   _input->update_map();
99   scalar_t s1, s2;
100   int k = 0;
101   for(int u = 0; u < _nb_units; u++) {
102     s1 = _unit_weights[k++];
103     for(int p = 0; p < _input->nb_parameters; p++)
104       s1 += _unit_weights[k++] * _input->parameters[p];
105     s2 = _unit_weights[k++];
106     for(int p = 0; p < _input->nb_parameters; p++)
107       s2 += _unit_weights[k++] * _input->parameters[p];
108     parameters[u] = s1;
109     _state_switch[u] = s2;
110   }
111 }
112
113 //////////////////////////////////////////////////////////////////////