Skip to content

Commit a6f5c11

Browse files
committed
Use expected result
1 parent 5cf3596 commit a6f5c11

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

include/bayesopt/bayesoptbase.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace bayesopt {
134134
/** Initialize the optimization process. */
135135
void initializeOptimization();
136136

137-
/** Once the optimization has been perfomed, return the optimal point. */
137+
/** Once the optimization has been performed, return the optimal point. */
138138
vectord getFinalResult();
139139

140140
/** Saves the current state of the optimization process into a state class. */

include/dataset.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,19 @@ namespace bayesopt
5858
vectord getPointAtMinimum() const;
5959
double getValueAtMinimum() const;
6060
size_t getNSamples() const;
61-
void updateMinMax( size_t i );
6261

6362
vecOfvec mX; ///< Data inputs
6463
vectord mY; ///< Data values
65-
66-
private:
67-
size_t mMinIndex, mMaxIndex;
64+
size_t mMinIndex, mMaxIndex; ///< Data optima
6865
};
6966

7067

7168
//// Inline methods
7269

7370
inline void Dataset::addSample(const vectord &x, double y)
7471
{
75-
mX.push_back(x); utils::append(mY,y);
76-
updateMinMax(mY.size()-1);
72+
mX.push_back(x);
73+
utils::append(mY,y);
7774
}
7875

7976
inline double Dataset::getSampleY(size_t index) const
@@ -92,11 +89,6 @@ namespace bayesopt
9289
inline vectord Dataset::getPointAtMinimum() const { return mX[mMinIndex]; };
9390
inline double Dataset::getValueAtMinimum() const { return mY(mMinIndex); };
9491
inline size_t Dataset::getNSamples() const { return mY.size(); };
95-
inline void Dataset::updateMinMax( size_t i )
96-
{
97-
if ( mY(mMinIndex) > mY(i) ) mMinIndex = i;
98-
else if ( mY(mMaxIndex) < mY(i) ) mMaxIndex = i;
99-
};
10092

10193
/**@}*/
10294

include/posteriormodel.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace bayesopt {
7676
void setSamples(const vectord &y);
7777
void setSample(const vectord &x, double y);
7878
void addSample(const vectord &x, double y);
79+
void updateMinMax();
7980
double getValueAtMinimum();
8081
vectord getPointAtMinimum();
8182

src/bayesoptbase.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ namespace bayesopt
149149
mModel->updateSurrogateModel();
150150
}
151151

152+
mModel->updateMinMax();
153+
152154
plotStepData(mCurrentIter,xNext,yNext);
153155
mModel->updateCriteria(xNext);
154156
mCurrentIter++;
@@ -204,8 +206,10 @@ namespace bayesopt
204206

205207
mModel->updateHyperParameters();
206208
mModel->fitSurrogateModel();
207-
mCurrentIter = 0;
208209

210+
mModel->updateMinMax();
211+
212+
mCurrentIter = 0;
209213
mCounterStuck = 0;
210214
mYPrev = 0.0;
211215
}
@@ -215,7 +219,6 @@ namespace bayesopt
215219
return remapPoint(getPointAtMinimum());
216220
}
217221

218-
219222
// SAVE-RESTORE INTERFACE
220223
void BayesOptBase::saveOptimization(BOptState &state)
221224
{
@@ -268,7 +271,9 @@ namespace bayesopt
268271
// Calculate the posterior model
269272
mModel->updateHyperParameters();
270273
mModel->fitSurrogateModel();
271-
274+
275+
mModel->updateMinMax();
276+
272277
mCurrentIter = state.mCurrentIter;
273278
mCounterStuck = state.mCounterStuck;
274279
mYPrev = state.mYPrev;

src/dataset.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,23 @@ namespace bayesopt
4242
// WARNING: It assumes mX is empty
4343
mY = y;
4444
for (size_t i=0; i<x.size1(); ++i)
45-
{
46-
mX.push_back(row(x,i));
47-
updateMinMax(i);
48-
}
45+
{
46+
mX.push_back(row(x,i));
47+
}
4948
};
5049

5150
void Dataset::setSamples(const vectord &y)
5251
{
5352
mY = y;
54-
for (size_t i=0; i<y.size(); ++i)
55-
{
56-
updateMinMax(i);
57-
}
5853
};
5954

6055

6156
void Dataset::setSamples(const matrixd &x)
6257
{
6358
for (size_t i=0; i<x.size1(); ++i)
64-
{
65-
mX.push_back(row(x,i));
66-
}
59+
{
60+
mX.push_back(row(x,i));
61+
}
6762
};
6863

6964
void Dataset::plotData(TLogLevel level)

src/posteriormodel.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ namespace bayesopt
8181
void PosteriorModel::addSample(const vectord &x, double y)
8282
{ mData.addSample(x,y); mMean.addNewPoint(x); };
8383

84+
void PosteriorModel::updateMinMax()
85+
{
86+
double minmean = std::numeric_limits<double>::infinity();
87+
double maxmean = -std::numeric_limits<double>::infinity();
88+
89+
mData.mMinIndex = 0;
90+
mData.mMaxIndex = 0;
91+
92+
for( size_t index = 0; index < mData.mX.size(); ++index )
93+
{
94+
const double mean = getPrediction(mData.mX[index])->getMean();
8495

96+
if( minmean >= mean )
97+
{
98+
minmean = mean;
99+
mData.mMinIndex = index;
100+
}
101+
102+
if( maxmean <= mean )
103+
{
104+
maxmean = mean;
105+
mData.mMaxIndex = index;
106+
}
107+
}
108+
}
85109
} //namespace bayesopt
86110

0 commit comments

Comments
 (0)