public
Created

  • Download Gist
mclsvg.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/*
Monte Carlo localization simulator by Daniel L. Lu
Outputs SVG images pertaining to a one-dimensional robot attempting to localize
in a circular corridor with three doors.
 
Copyright (c) 2013, Daniel L. Lu
All rights reserved.
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <fstream>
 
using namespace std;
 
static const long double
DITHER = 10,
MOVEMENT_D_NOISE = 0.05, // pixels noise proportional to distance moved
MOVEMENT_P_NOISE = 5, // pixels noise independent of distance moved
SENSITIVITY = 500, // pixels width of gaussian for door
KIDNAP_PROBABILITY = 0.1;
static const int
L = 1000, // map size
M = 500, // number of particles
doors[] = {300, 450, 700}, // position of doors
D = 3; // number of doors
 
struct particle {
long double z;
long double w;
};
 
class robot {
public:
robot() {
srand(3);
z = 100;
for(int m=0; m<M; m++) {
particle x;
x.z = L*(rand()/(long double)RAND_MAX);
x.w = 1;
belief.push_back(x);
}
prints = 0;
t = 0;
}
void mcl(bool sensor, long double command) {
vector <particle> belief_;
svg(false, belief);
long double cumsum = 0, *integral = new long double[belief.size()], trial;
int m = 0, i, di;
z += command;
for(m=0; m<M; m++) {
motion_update(command, &(belief[m]));
}
svg(false, belief);
m = 0;
for(m=0; m<M; m++) {
/* this loop can be merged with the previous loop in normal implementations but here they are separate
because we need to output the SVG in between. */
particle x = belief.back();
sensor_update(sensor, &x);
cumsum += x.w;
integral[m] = cumsum;
belief.pop_back();
belief_.push_back(x);
}
svg(true, belief_);
for(m=0; m<M; m++) {
trial = cumsum*(rand()/(long double)RAND_MAX);
/* this part is much better done using binary search but I'm too lazy to write the 2 extra lines for that*/
for(i=0; i<M; i++) {
if(integral[i]>trial) break;
}
belief.push_back(belief_[i]);
belief[m].z = wrap(belief[m].z + noise()*DITHER);
}
svg(false, belief);
belief_.clear();
t++;
prints = 0;
delete [] integral;
}
void svg(bool showweights, vector<particle>bel) {
char filename[200];
sprintf(filename, "mcl_t_%d_%d.svg", t, prints++);
fstream fout(filename, fstream::out);
fout << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"" << L << "px\" height=\"160px\">" << endl;
long double maxweight = 0;
fout << "<rect x=\"0\" y=\"0\" width=\"" << L << "\" height=\"100\" style=\"fill:#eeeeee;stroke-width:0\"/>" << endl;
fout << "<rect x=\"0\" y=\"100\" width=\"" << L << "\" height=\"60\" style=\"fill:#ffffff;stroke-width:0\"/>" << endl;
for(int m=0; m<M; m++) {
if(bel[m].w > maxweight) maxweight = bel[m].w;
fout << "<line x1=\"" << bel[m].z << "\" y1=\"100\" x2=\""
<< bel[m].z << "\" y2=\"160\" style=\"stroke:#000000;opacity:0.2;stroke-width:1\"/>" << endl;
}
if(showweights) {
for(int m=0; m<M; m++) {
fout << "<line x1=\"" << bel[m].z << "\" y1=\"" << 160 - 60*bel[m].w/maxweight << "\" x2=\""
<< bel[m].z << "\" y2=\"160\" style=\"stroke:#ff0000;stroke-width:1\"/>" << endl;
}
}
fout << "<line x1=\"0\" y1=\"100\" x2=\"" << L << "\" y2=\"100\" style=\"stroke:#cccccc;stroke-width:1\"/>" << endl;
 
/* draw the doors */
for(int d=0; d<D; d++) {
fout << "<g transform=\"translate(" << doors[d] << ",0)\">" << endl;
fout << " <rect x=\"" << -34 << "\" y=\"10\" width=\"68\" height=\"90\" style=\"fill:#aa7060;stroke:#995544;stroke-width:2\"/>" << endl;
fout << " <circle cx=\"" << 16 << "\" cy=\"55\" r=\"6\" style=\"fill:#333333;stroke:#111111;stroke-width:2\"/>" << endl;
fout << "</g>" << endl;
}
 
/* draw the robot itself */
fout << "<g transform=\"translate(" << z << ",0)\">" << endl;
fout << " <rect x=\"" << -31 << "\" y=\"20\" width=\"62\" height=\"70\" rx=\"16\" style=\"fill:#5588ff;stroke:#3355cc;stroke-width:2\"/>" << endl;
fout << " <circle cx=\"" << -16 << "\" cy=\"90\" r=\"10\" style=\"fill:#555555;stroke:#333333;stroke-width:2\"/>" << endl; // left wheel
fout << " <circle cx=\"" << 16 << "\" cy=\"90\" r=\"10\" style=\"fill:#555555;stroke:#333333;stroke-width:2\"/>" << endl; // right wheel
fout << " <circle cx=\"" << -12 << "\" cy=\"32\" r=\"5\" style=\"fill:#333333;stroke:#111111;stroke-width:2\"/>" << endl; // left eye
fout << " <circle cx=\"" << 12 << "\" cy=\"32\" r=\"5\" style=\"fill:#333333;stroke:#111111;stroke-width:2\"/>" << endl; // right eye
fout << " <path d=\"M-7,43 h14 a7,-7 0 0,1 -14,0 z\" style=\"fill:#ee0000;stroke:#aa0000;stroke-width:2\"/>" << endl; // mouth
fout << "</g>" << endl;
 
fout << "</svg>" << endl;
}
private:
void motion_update(long double command, particle *x) {
x->z = wrap(x->z + command + command*noise()*MOVEMENT_D_NOISE + noise()*MOVEMENT_P_NOISE);
}
void sensor_update(bool sensor, particle *x) {
if(sensor) {
x->w = KIDNAP_PROBABILITY;
for(int d=0; d<D; d++) {
x->w += exp(-pow(x->z - doors[d], 2)/SENSITIVITY);
}
} else {
x->w = 1 + KIDNAP_PROBABILITY;
for(int d=0; d<D; d++) {
x->w -= exp(-pow(x->z - doors[d], 2)/SENSITIVITY);
}
}
}
inline long double noise() {
return (rand()/(long double)RAND_MAX)-(rand()/(long double)RAND_MAX);
}
inline long double wrap(long double z) {
return z - L*floor(z/L);
}
long double z;
int prints, t;
vector <particle> belief;
};
 
int main() {
robot A;
A.mcl(true, 200);
A.mcl(false, 250);
A.mcl(true, -100);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.