Recoded to UTF-8.
[cmim.git] / create_samples.cc
1
2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify         //
4 // it under the terms of the version 3 of the GNU General Public License        //
5 // as published by the Free Software Foundation.                                //
6 //                                                                              //
7 // This program is distributed in the hope that it will be useful, but          //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of                   //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU             //
10 // General Public License for more details.                                     //
11 //                                                                              //
12 // You should have received a copy of the GNU General Public License            //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.         //
14 //                                                                              //
15 // Written by Francois Fleuret                                                  //
16 // Copyright (C) Ecole Polytechnique Federale de Lausanne                       //
17 // Contact <francois.fleuret@epfl.ch> for comments & bug reports                //
18 //////////////////////////////////////////////////////////////////////////////////
19
20 // $Id: create_samples.cc,v 1.3 2007-08-23 08:36:50 fleuret Exp $
21
22 using namespace std;
23
24 #include <cmath>
25 #include <iostream>
26 #include <fstream>
27 #include <stdlib.h>
28
29 // This defines the positive population
30
31 bool in_region(double x, double y) {
32   return x*x + y*y <= 0.25;
33 }
34
35 int main(int argc, char **argv) {
36
37   const int nb_samples = 1000;
38
39   const int nb_features = 1000;
40   double vxf[nb_features], vyf[nb_features], kf[nb_features];
41   for(int j = 0; j < nb_features; j++) {
42     double alpha = drand48() * 2 * M_PI;
43     vxf[j] = sin(alpha);
44     vyf[j] = cos(alpha);
45     kf[j]  = - drand48() * vxf[j] - drand48() * vyf[j];
46   }
47
48   cout << "Saving the training set.\n";
49   ofstream training("train.dat");
50   training << nb_samples << " " << nb_features << "\n";
51   for(int k = 0; k < nb_samples; k++) {
52     double x = drand48(), y = drand48();
53     for(int j = 0; j < nb_features; j++)
54       training << ((x*vxf[j] + y*vyf[j] + kf[j] >= 0) ? 1 : 0) << ((j < nb_features-1) ? " " : "\n");
55     training << (in_region(x, y) ? 1 : 0) << "\n";
56   }
57
58   int delta = 50;
59   cout << "Saving the test set.\n";
60   ofstream test("test.dat");
61   test << delta*delta << " " << nb_features << "\n";
62   for(int xx = 0; xx < delta; xx++) for(int yy = 0; yy < delta; yy++) {
63     double x = double(xx)/double(delta-1), y = double(yy)/double(delta-1);
64     for(int j = 0; j < nb_features; j++)
65       test << ((x*vxf[j] + y*vyf[j] + kf[j] >= 0) ? 1 : 0) << ((j < nb_features-1) ? " " : "\n");
66     test << (in_region(x, y) ? 1 : 0) << "\n";
67   }
68 }