BayesOpt
displaygp.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 
23 #include "displaygp.hpp"
24 #include "dataset.hpp"
25 #include "prob_distribution.hpp"
26 
27 namespace bayesopt
28 {
29  namespace utils
30  {
31  DisplayProblem1D::DisplayProblem1D(): MatPlot()
32  {
33  status = NOT_READY;
34  }
35 
36  void DisplayProblem1D::init(BayesOptBase* bopt, size_t dim)
37  {
38  if (dim != 1)
39  {
40  throw std::invalid_argument("Display only works for 1D problems");
41  }
42 
43  bopt_model = bopt;
44  bopt->initializeOptimization();
45  size_t n_points = bopt->getData()->getNSamples();
46  for (size_t i = 0; i<n_points;++i)
47  {
48  const double res = bopt->getData()->getSampleY(i);
49  const vectord last = bopt->getData()->getSampleX(i);
50  ly.push_back(res);
51  lx.push_back(last(0));
52  }
53  state_ii = 0;
54  status = STOP;
55  };
56 
57  void DisplayProblem1D::setSTEP()
58  {
59  if (status != NOT_READY)
60  {
61  status = STEP;
62  }
63  };
64 
65  void DisplayProblem1D::toogleRUN()
66  {
67  if (status != NOT_READY)
68  {
69  if(status != RUN)
70  {
71  status = RUN;
72  }
73  else
74  {
75  status = STOP;
76  }
77  }
78  }
79 
80  void DisplayProblem1D::DISPLAY()
81  {
82  if (status != NOT_READY)
83  {
84  size_t nruns = bopt_model->getParameters()->n_iterations;
85  if ((status != STOP) && (state_ii < nruns))
86  {
87  // We are moving. Next iteration
88  ++state_ii;
89  bopt_model->stepOptimization();
90  const double res = bopt_model->getData()->getLastSampleY();
91  const vectord last = bopt_model->getData()->getLastSampleX();
92  ly.push_back(res);
93  lx.push_back(last(0));
94 
95  if (status == STEP) { status = STOP; }
96  }
97 
98  // We compute the prediction, true value and criteria at 1000 points
99  int n=1000;
100  std::vector<double> x,y,z,su,sl,c;
101  x = linspace(0,1,n);
102  y = x; z = x; su = x; sl = x; c = x;
103 
104  // Query functions at the 1000 points
105  vectord q(1);
106  for(size_t i=0; i<n; ++i)
107  {
108  q(0) = x[i]; // Query
109  ProbabilityDistribution* pd = bopt_model->getPrediction(q);
110  y[i] = pd->getMean(); //Expected value
111  su[i] = y[i] + 2*pd->getStd(); //Upper bound (95 %)
112  sl[i] = y[i] - 2*pd->getStd(); //Lower bound (95 %)
113  c[i] = -bopt_model->evaluateCriteria(q); //Criteria value
114  z[i] = bopt_model->evaluateSample(q); //Target function true value
115  }
116 
117  //GP subplot
118  subplot(2,1,1);
119  title("Press r to run and stop, s to run a step and q to quit.");
120  plot(x,y); set(3); // Expected value in default color (blue)
121  plot(lx,ly);set("k");set("o");set(4); // Data points as black star
122  plot(x,su);set("g"); set(2); // Uncertainty as green lines
123  plot(x,sl);set("g"); set(2);
124  plot(x,z);set("r"); set(3); // True function as red line
125 
126  //Criterion subplot
127  subplot(2,1,2);
128  plot(x,c); set(3);
129  }
130  };
131 
132  DisplayProblem2D::DisplayProblem2D():
133  MatPlot(), cx(1), cy(1), c_points(100), cX(c_points),
134  cY(c_points), cZ(c_points,std::vector<double>(c_points))
135  {
136  status = NOT_READY;
137  }
138 
139  void DisplayProblem2D::setSolution(vectord sol)
140  {
141  solx.push_back(sol(0));
142  soly.push_back(sol(1));
143  }
144 
145  void DisplayProblem2D::prepareContourPlot()
146  {
147  cX=linspace(0,1,c_points);
148  cY=linspace(0,1,c_points);
149 
150  for(int i=0;i<c_points;++i)
151  {
152  for(int j=0;j<c_points;++j)
153  {
154  vectord q(2);
155  q(0) = cX[j]; q(1) = cY[i];
156  cZ[i][j]= bopt_model->evaluateSample(q);
157  }
158  }
159  }
160 
161  void DisplayProblem2D::init(BayesOptBase* bopt, size_t dim)
162  {
163  if (dim != 2)
164  {
165  throw std::invalid_argument("This display only works "
166  "for 2D problems");
167  }
168 
169  bopt_model = bopt;
170  prepareContourPlot();
171 
172  bopt->initializeOptimization();
173  size_t n_points = bopt->getData()->getNSamples();
174  for (size_t i = 0; i<n_points;++i)
175  {
176  const vectord last = bopt->getData()->getSampleX(i);
177  lx.push_back(last(0));
178  ly.push_back(last(1));
179  }
180  state_ii = 0;
181  status = STOP;
182  };
183 
184  void DisplayProblem2D::setSTEP()
185  {
186  if (status != NOT_READY)
187  {
188  status = STEP;
189  }
190  };
191 
192  void DisplayProblem2D::toogleRUN()
193  {
194  if (status != NOT_READY)
195  {
196  if(status != RUN)
197  {
198  status = RUN;
199  }
200  else
201  {
202  status = STOP;
203  }
204  }
205  }
206 
207  void DisplayProblem2D::DISPLAY()
208  {
209  if (status != NOT_READY)
210  {
211  size_t nruns = bopt_model->getParameters()->n_iterations;
212  title("Press r to run and stop, s to run a step and q to quit.");
213  contour(cX,cY,cZ,50); // Contour plot (50 lines)
214  plot(cx,cy);set("g");set("o");set(4); // Data points as black star
215  plot(solx,soly);set("r"); set("o");set(4); // Solutions as red points
216 
217  if ((status != STOP) && (state_ii < nruns))
218  {
219  // We are moving. Next iteration
220  ++state_ii;
221  bopt_model->stepOptimization();
222  const vectord last = bopt_model->getData()->getLastSampleX();
223  //GP subplot
224  cx[0] = last(0);
225  cy[0] = last(1);
226 
227  if (!lx.empty())
228  {
229  plot(lx,ly);set("k");set("o");set(4); // Data points as black star
230  }
231 
232  lx.push_back(last(0));
233  ly.push_back(last(1));
234 
235  if (status == STEP) { status = STOP; }
236  }
237  else
238  {
239  plot(lx,ly);set("k");set("o");set(4); // Data points as black star
240  }
241 
242  }
243  };
244 
245  } //namespace utils
246 
247 } //namespace bayesopt
Dataset model.
Namespace of the library interface.
Definition: using.dox:1
STL namespace.
Plots the evolution (nonparametric process, criteria or contour plots) of 1D and 2D problems...
Interface for probability models.