TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
Distribution.cpp
Go to the documentation of this file.
1/* -*- Mode: C++; -*- */
2// VER: $Id$
3// copyright (c) 2004 by Christos Dimitrakakis <dimitrak@idiap.ch>
4/***************************************************************************
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 ***************************************************************************/
12#include <cstdio>
13#include <cstdlib>
14#include <cmath>
15#include <learning/Distribution.h>
16
17static const double PI = 3.14159265358979323846;
18
19void setRandomSeed(unsigned int seed)
20{
21 srand(seed);
22}
24{
25 real x;
26 do {
27 x = ((real) rand())/((real) (RAND_MAX));
28 } while (x==1.0);
29 return x;
30}
31
33{
34 return min + ((max-min)*urandom());
35}
36
38{
39 return m + (urandom()-0.5)*s;
40}
41
43{
44 real dx=x-m;
45 real sh=.5*s;
46 if ((dx > -sh)&&(dx <= sh))
47 return 1.0/s;
48 return 0.0;
49}
50
51
52
53// Taken from numerical recipes in C
55{
56 if(!cache) {
57 normal_x = urandom();
58 normal_y = urandom();
59 normal_rho = sqrt(-2.0 * log(1.0 - normal_y));
60 cache = true;
61 } else {
62 cache = false;
63 }
64
65 if (cache) {
66 return normal_rho * cos(2.0 * PI * normal_x) * s + m;
67 } else {
68 return normal_rho * sin(2.0 * PI * normal_x) * s + m;
69 }
70}
71
73{
74 real d = (m-x)/s;
75 return exp(-0.5 * d*d)/(sqrt(2.0 * PI) * s);
76}
77
79{
80 real x = urandom(-1.0, 1.0);
81 real absx = fabs (x);
82 real sgnx;
83 if (x>0.0) {
84 sgnx = 1.0;
85 } else {
86 sgnx = -1.0;
87 }
88
89 return m + sgnx * log(1.0 - absx) / l;
90
91}
92
94{
95 return 0.5*l * exp (-l*fabs(x-m));
96}
97
99{
100 real x = urandom();
101 return - log (1.0 - x) / l;
102}
103
105{
106 real d = x - m;
107 if (d>0.0) {
108 return l * exp (-l*d);
109 }
110 return 0.0;
111}
112
113
117
119 p = NULL;
120 n_outcomes = 0;
121 p = (real*) malloc (sizeof(real) * N);
122 n_outcomes = N;
123 real invN = 1.0/((real) N);
124 for (int i=0; i<N; i++) {
125 p[i] = invN;
126 }
127}
128
132
134{
135 real d=urandom();
136 real sum = 0.0;
137 for (int i=0; i<n_outcomes; i++) {
138 sum += p[i];
139 if (d < sum) {
140 return (real) i;
141 }
142 }
143 return 0.0;
144}
145
147{
148 int i=(int) floor(x);
149 if ((i>=0)&&(i<n_outcomes)) {
150 return p[i];
151 }
152 return 0.0;
153}
Scalar min(Scalar x, Scalar y)
Definition Basic.h:49
Scalar max(Scalar x, Scalar y)
Definition Basic.h:50
real urandom()
void setRandomSeed(unsigned int seed)
static const double PI
real urandom()
return a number drawn uniformly from [0,1]
virtual real pdf(real x)
int n_outcomes
number of possible outcomes
real * p
probabilities of outcomes
virtual ~DiscreteDistribution()
virtual real generate()
virtual real pdf(real x)
virtual real generate()
virtual real pdf(real x)
virtual real generate()
virtual real pdf(real x)
real s
standard deviation
virtual real generate()
real s
standard deviation
virtual real pdf(real x)
float real
Definition real.h:13