2 #include "../../Algos/NelderMead/NMUpdate.hpp"
4 #include "../../Algos/NelderMead/NMMegaIteration.hpp"
5 #include "../../Algos/NelderMead/NMIteration.hpp"
7 #include "../../Algos/CacheInterface.hpp"
9 #include "../../Output/OutputInfo.hpp"
11 void NOMAD::NMUpdate::init()
14 verifyParentNotNull();
18 void NOMAD::NMUpdate::start()
23 bool NOMAD::NMUpdate::run()
26 const NOMAD::NMIteration * iter =
dynamic_cast<const NOMAD::NMIteration*
>(getParentOfType<NOMAD::NMIteration*>());
29 throw NOMAD::Exception(__FILE__,__LINE__,
"Update must have a NMIteration among its ancestors.");
32 if ( iter->getY()->size() == 0 )
34 AddOutputDebug(
"No need to update because NM simplex is empty");
38 const NOMAD::NMMegaIteration * megaIter =
dynamic_cast<const NOMAD::NMMegaIteration*
>(getParentOfType<NOMAD::NMMegaIteration*>());
43 auto barrier = megaIter->getBarrier();
45 if (NOMAD::CacheBase::getInstance()->size() > 1)
48 auto refBestFeas = barrier->getFirstXFeas();
49 auto refBestInf = barrier->getFirstXInf();
52 updateBestInBarrier(
true);
53 updateBestInBarrier(
false);
60 std::shared_ptr<NOMAD::EvalPoint> newBest;
64 NOMAD::ComputeSuccessType computeSuccess;
65 success = computeSuccess(newBestFeas, refBestFeas);
66 if (success >= NOMAD::SuccessType::PARTIAL_SUCCESS)
69 newBest = newBestFeas;
73 AddOutputDebug(
"Update: improving feasible point");
76 AddOutputDebug(
" from " + refBestFeas->display() );
78 AddOutputDebug(
" to " + newBestFeas->display() );
84 if (success2 > success)
88 if (success >= NOMAD::SuccessType::PARTIAL_SUCCESS)
92 AddOutputDebug(
"Update: improving infeasible point");
95 AddOutputDebug(
" from " + refBestInf->display() );
97 AddOutputDebug(
" to " + newBestInf->display());
101 if (success == NOMAD::SuccessType::UNSUCCESSFUL)
103 std::string s =
"Update: no success found";
107 if (success >= NOMAD::SuccessType::PARTIAL_SUCCESS)
112 auto pointFromPtr = newBest->getPointFrom();
114 auto pointNewPtr = newBest->getX();
115 if (
nullptr == pointFromPtr)
117 std::string s =
"Update cannot compute new direction for successful point: pointFromPtr is NULL ";
118 s += newBest->display();
119 throw NOMAD::Exception(__FILE__,__LINE__, s);
123 auto fixedVariable = _parentStep->getSubFixedVariable();
124 auto pointFromSub = std::make_shared<NOMAD::Point>(pointFromPtr->makeSubSpacePointFromFixed(fixedVariable));
126 NOMAD::Direction dir = NOMAD::Point::vectorize(*pointFromSub, *pointNewPtr);
127 std::string dirStr =
"New direction " + dir.display();
128 AddOutputInfo(dirStr);
130 AddOutputInfo(
"Last NM step Successful.");
134 AddOutputInfo(
"Last NM step Unsuccessful.");
142 void NOMAD::NMUpdate::updateBestInBarrier(
bool feasible)
144 std::vector<NOMAD::EvalPoint> evalPointList;
145 auto barrier = getMegaIterationBarrier();
147 NOMAD::Double hMax = 0.0;
150 hMax = barrier->getHMax();
153 NOMAD::CacheInterface cacheInterface(
this);
156 cacheInterface.findBestFeas(evalPointList, getSubFixedVariable());
160 cacheInterface.findBestInf(evalPointList, hMax, getSubFixedVariable());
164 std::string s =
"Found " + std::to_string(evalPointList.size());
165 s += (feasible) ?
" feasible " :
" infeasible ";
167 NOMAD::OutputInfo outputInfo(_name, s, NOMAD::OutputLevel::LEVEL_DEBUG);
168 for (
auto evalPoint : evalPointList)
170 outputInfo.addMsg(evalPoint.display());
172 NOMAD::OutputQueue::Add(std::move(outputInfo));
174 if (_runParams->getAttributeValue<
bool>(
"POLL_CENTER_USE_CACHE"))
181 barrier->clearXFeas();
182 for (
auto evalPoint : evalPointList)
184 barrier->addXFeas(std::make_shared<NOMAD::EvalPoint>(evalPoint));
189 barrier->clearXInf();
190 for (
auto evalPoint : evalPointList)
192 barrier->addXInf(std::make_shared<NOMAD::EvalPoint>(evalPoint));
201 if (evalPointList.size() > 0)
204 NOMAD::EvalPointPtr refBest = (feasible) ? barrier->getFirstXFeas() : barrier->getFirstXInf();
205 NOMAD::ComputeSuccessType computeSuccess;
206 bool doUpdateBarrier =
false;
207 if (
nullptr == refBest)
210 doUpdateBarrier =
true;
212 else if (computeSuccess(newBest, refBest) >= NOMAD::SuccessType::PARTIAL_SUCCESS)
215 doUpdateBarrier =
true;
217 else if (computeSuccess(refBest, newBest) == NOMAD::SuccessType::UNSUCCESSFUL)
221 size_t nbPoints = (feasible) ? barrier->getAllXFeas().size() : barrier->getAllXInf().size();
224 doUpdateBarrier =
true;
231 std::string s =
"Warning: Previous frame center is better than current.";
232 s +=
"\nPrevious frame center: " + refBest->display();
233 s +=
"\nCurrent frame center: " + newBest->display();
241 barrier->clearXFeas();
242 barrier->addXFeas(newBest);
246 barrier->clearXInf();
247 barrier->addXInf(newBest);
255 void NOMAD::NMUpdate::end()
std::shared_ptr< EvalPoint > EvalPointPtr
Definition for eval point pointer.
SuccessType
Success type of an iteration.