PLearn 0.1
|
#include <OptimizeOptionOracle.h>
Public Member Functions | |
OptimizeOptionOracle () | |
virtual TVec< string > | getOptionNames () const |
Overridden. | |
virtual TVec< string > | generateNextTrial (const TVec< string > &older_trial, real obtained_objective) |
virtual void | forget () |
SUBCLASS WRITING: Should reset the generator's internal state (as having no info about previous trials). | |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual OptimizeOptionOracle * | deepCopy (CopiesMap &copies) const |
Static Public Member Functions | |
static string | _classname_ () |
Declares name and deepCopy methods. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Public Attributes | |
real | factor |
int | max_steps |
real | max_value |
real | min_value |
real | relative_precision |
string | option |
string | start_direction |
real | start_value |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Protected Attributes | |
string | current_direction |
real | best |
real | best_objective |
real | lower_bound |
int | n_steps |
real | upper_bound |
Private Types | |
typedef OptionsOracle | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 51 of file OptimizeOptionOracle.h.
typedef OptionsOracle PLearn::OptimizeOptionOracle::inherited [private] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 56 of file OptimizeOptionOracle.h.
PLearn::OptimizeOptionOracle::OptimizeOptionOracle | ( | ) |
Definition at line 60 of file OptimizeOptionOracle.cc.
: current_direction("not_started"), lower_bound(1), n_steps(0), upper_bound(-1), factor(2), max_steps(10), max_value(REAL_MAX), min_value(-REAL_MAX), relative_precision(0.5), option(""), start_direction("random"), start_value(0) {}
string PLearn::OptimizeOptionOracle::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
OptionList & PLearn::OptimizeOptionOracle::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
RemoteMethodMap & PLearn::OptimizeOptionOracle::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
Object * PLearn::OptimizeOptionOracle::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 55 of file OptimizeOptionOracle.cc.
StaticInitializer OptimizeOptionOracle::_static_initializer_ & PLearn::OptimizeOptionOracle::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
void PLearn::OptimizeOptionOracle::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::OptionsOracle.
Definition at line 134 of file OptimizeOptionOracle.cc.
References PLearn::OptionsOracle::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::OptimizeOptionOracle::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::OptionsOracle.
Definition at line 142 of file OptimizeOptionOracle.cc.
References lower_bound, max_value, min_value, PLERROR, and upper_bound.
Referenced by build().
{ if (min_value > max_value) { PLERROR("In OptimizeOptionOracle::build_ - You specified a min_value higher than max_value"); } if (lower_bound > upper_bound) { // Should only happen when this object is built for the first time. lower_bound = min_value; upper_bound = max_value; } }
string PLearn::OptimizeOptionOracle::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file OptimizeOptionOracle.cc.
void PLearn::OptimizeOptionOracle::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::OptionsOracle.
Definition at line 78 of file OptimizeOptionOracle.cc.
References best, best_objective, PLearn::OptionBase::buildoption, current_direction, PLearn::declareOption(), PLearn::OptionsOracle::declareOptions(), factor, PLearn::OptionBase::learntoption, lower_bound, max_steps, max_value, min_value, n_steps, option, relative_precision, start_direction, start_value, and upper_bound.
{ declareOption(ol, "option", &OptimizeOptionOracle::option, OptionBase::buildoption, "The name of the option to optimize."); declareOption(ol, "max_steps", &OptimizeOptionOracle::max_steps, OptionBase::buildoption, "The maximum number of steps performed."); declareOption(ol, "start_value", &OptimizeOptionOracle::start_value, OptionBase::buildoption, "The value we start from for this option."); declareOption(ol, "min_value", &OptimizeOptionOracle::min_value, OptionBase::buildoption, "The minimum value that should be tried."); declareOption(ol, "max_value", &OptimizeOptionOracle::max_value, OptionBase::buildoption, "The maximum value that should be tried."); declareOption(ol, "relative_precision", &OptimizeOptionOracle::relative_precision, OptionBase::buildoption, "The precision to which we want to find the optimal option. For instance\n" "0.1 would indicate we want the 'best' value to be found in an interval\n" "of the kind [best - 10% * best, best + 10% * best]."); declareOption(ol, "factor", &OptimizeOptionOracle::factor, OptionBase::buildoption, "The factor by which we multiply / divide the current value after\n" "we started from 'start_value'."); declareOption(ol, "start_direction", &OptimizeOptionOracle::start_direction, OptionBase::buildoption, "The direction we start going to ('up', 'down' or 'random')."); //*************** declareOption(ol, "best", &OptimizeOptionOracle::best, OptionBase::learntoption, "The best value found so far."); declareOption(ol, "best_objective", &OptimizeOptionOracle::best_objective, OptionBase::learntoption, "The objective obtained with option = 'best'."); declareOption(ol, "current_direction", &OptimizeOptionOracle::current_direction, OptionBase::learntoption, "The current direction we are going ('up', 'down' or 'not_started')."); declareOption(ol, "lower_bound", &OptimizeOptionOracle::lower_bound, OptionBase::learntoption, "The lower bound of the interval in which we working."); declareOption(ol, "n_steps", &OptimizeOptionOracle::n_steps, OptionBase::learntoption, "The number of steps performed so far."); declareOption(ol, "upper_bound", &OptimizeOptionOracle::upper_bound, OptionBase::learntoption, "The upper bound of the interval in which we working."); // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::OptimizeOptionOracle::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 123 of file OptimizeOptionOracle.h.
OptimizeOptionOracle * PLearn::OptimizeOptionOracle::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::OptionsOracle.
Definition at line 55 of file OptimizeOptionOracle.cc.
void PLearn::OptimizeOptionOracle::forget | ( | ) | [virtual] |
SUBCLASS WRITING: Should reset the generator's internal state (as having no info about previous trials).
Implements PLearn::OptionsOracle.
Definition at line 157 of file OptimizeOptionOracle.cc.
References current_direction, lower_bound, max_value, min_value, n_steps, and upper_bound.
{ current_direction = "not_started"; lower_bound = min_value; upper_bound = max_value; n_steps = 0; }
TVec< string > PLearn::OptimizeOptionOracle::generateNextTrial | ( | const TVec< string > & | older_trial, |
real | obtained_objective | ||
) | [virtual] |
SUBCLASS WRITING: This notifies the oracle, of the result obtained for a previous trial it suggested. The oracle is expected to return the next suggestion, or an empty vector if stopping conditions were met, or it run out of suggestions. An empty vector may be passed for older_trial if there is no result to report.
Implements PLearn::OptionsOracle.
Definition at line 175 of file OptimizeOptionOracle.cc.
References best, best_objective, current_direction, factor, PLearn::TVec< T >::length(), lower_bound, max_steps, n_steps, PLERROR, relative_precision, start_direction, start_value, PLearn::tostring(), PLearn::uniform_sample(), and upper_bound.
{ if (n_steps >= max_steps) { // Time to stop. TVec<string> empty(0); return empty; } else { n_steps++; } if (older_trial.length() == 0) { // This is the first try: we just start with the given start value. best = start_value; best_objective = REAL_MAX; return TVec<string>(1, tostring(start_value)); } // We can stop if the interval is restrained enough. if (lower_bound >= best - (best * relative_precision) && upper_bound <= best + (best * relative_precision)) { TVec<string> empty(0); return empty; } real last = toreal(older_trial[0]); // The last value tried bool improved = false; if (obtained_objective < best_objective) { improved = true; best_objective = obtained_objective; } if (current_direction == "not_started") { // This is the second try. if (start_direction == "random") { // Need to start with a random direction. real t = uniform_sample(); if (t < 0.5) { current_direction = "up"; } else { current_direction = "down"; } } else { current_direction = start_direction; } } else { // Find out in which direction we should go now. if (improved) { // Going in the 'current_direction' helped, let's keep going. if (current_direction == "up") { lower_bound = best; } else { upper_bound = best; } } else { // Going in the 'current_direction' didn't help. We go the other way. if (current_direction == "up") { current_direction = "down"; upper_bound = last; } else { current_direction = "up"; lower_bound = last; } } } if (improved) { best = last; } real next = 0; // The next value we are going to try. if (current_direction == "up") { if (best * factor < upper_bound*0.99) { // We can try much higher (the 0.99 is there to ensure we do not indefinitely // try the same 'upper_bound' value). next = best * factor; } else { // We take the middle point between 'best' and 'upper_bound'. next = (best + upper_bound) / 2; } } else if (current_direction == "down") { if (best / factor > lower_bound*1.01) { // We can try much lower. next = best / factor; } else { // We take the middle point between 'best' and 'lower_bound'. next = (best + lower_bound) / 2; } } else { PLERROR("In OptimizeOptionOracle::generateNextTrial - Wrong value for 'current_direction'"); } return TVec<string>(1, tostring(next)); }
OptionList & PLearn::OptimizeOptionOracle::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file OptimizeOptionOracle.cc.
OptionMap & PLearn::OptimizeOptionOracle::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file OptimizeOptionOracle.cc.
TVec< string > PLearn::OptimizeOptionOracle::getOptionNames | ( | ) | const [virtual] |
Overridden.
Implements PLearn::OptionsOracle.
Definition at line 168 of file OptimizeOptionOracle.cc.
References option.
{ return TVec<string>(1,option); }
RemoteMethodMap & PLearn::OptimizeOptionOracle::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file OptimizeOptionOracle.cc.
void PLearn::OptimizeOptionOracle::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::OptionsOracle.
Definition at line 271 of file OptimizeOptionOracle.cc.
References PLearn::OptionsOracle::makeDeepCopyFromShallowCopy(), and PLERROR.
{ inherited::makeDeepCopyFromShallowCopy(copies); // ### Call deepCopyField on all "pointer-like" fields // ### that you wish to be deepCopied rather than // ### shallow-copied. // ### ex: // deepCopyField(trainvec, copies); // ### Remove this line when you have fully implemented this method. PLERROR("OptimizeOptionOracle::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
Reimplemented from PLearn::OptionsOracle.
Definition at line 123 of file OptimizeOptionOracle.h.
real PLearn::OptimizeOptionOracle::best [protected] |
Definition at line 65 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
real PLearn::OptimizeOptionOracle::best_objective [protected] |
Definition at line 66 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
string PLearn::OptimizeOptionOracle::current_direction [protected] |
Definition at line 64 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), forget(), and generateNextTrial().
Definition at line 77 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
real PLearn::OptimizeOptionOracle::lower_bound [protected] |
Definition at line 67 of file OptimizeOptionOracle.h.
Referenced by build_(), declareOptions(), forget(), and generateNextTrial().
Definition at line 78 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
Definition at line 79 of file OptimizeOptionOracle.h.
Referenced by build_(), declareOptions(), and forget().
Definition at line 80 of file OptimizeOptionOracle.h.
Referenced by build_(), declareOptions(), and forget().
int PLearn::OptimizeOptionOracle::n_steps [protected] |
Definition at line 68 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), forget(), and generateNextTrial().
Definition at line 82 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and getOptionNames().
Definition at line 81 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
Definition at line 83 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
Definition at line 84 of file OptimizeOptionOracle.h.
Referenced by declareOptions(), and generateNextTrial().
real PLearn::OptimizeOptionOracle::upper_bound [protected] |
Definition at line 69 of file OptimizeOptionOracle.h.
Referenced by build_(), declareOptions(), forget(), and generateNextTrial().