Skip to content

Commit

Permalink
Extracted model selection range builder out
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 6, 2013
1 parent f2209a5 commit e46acce
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 126 deletions.
1 change: 1 addition & 0 deletions src/interfaces/modular/ModelSelection.i
Expand Up @@ -33,4 +33,5 @@ SERIALIZABLE_DUMMY(shogun::CCrossValidationResult);
%include <shogun/modelselection/RandomSearchModelSelection.h>
%include <shogun/modelselection/ParameterCombination.h>
%include <shogun/modelselection/ModelSelectionParameters.h>
%include <shogun/modelselection/ModelSelectionParameterRangeBuilder.h>
%include <shogun/modelselection/GradientModelSelection.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/ModelSelection_includes.i
@@ -1,6 +1,7 @@
%{
#include <shogun/modelselection/ModelSelection.h>
#include <shogun/modelselection/ModelSelectionParameters.h>
#include <shogun/modelselection/ModelSelectionParameterRangeBuilder.h>
#include <shogun/modelselection/GridSearchModelSelection.h>
#include <shogun/modelselection/RandomSearchModelSelection.h>
#include <shogun/modelselection/GradientModelSelection.h>
Expand Down
79 changes: 79 additions & 0 deletions src/shogun/modelselection/ModelSelectionParameterRangeBuilder.cpp
@@ -0,0 +1,79 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2013 Sergey Lisitsyn
*/

#include <shogun/modelselection/ModelSelectionParameterRangeBuilder.h>

namespace shogun
{

CModelSelectionParameterRangeBuilder::CModelSelectionParameterRangeBuilder() :
m_parameters(NULL), m_type(R_LINEAR), m_from(), m_to(), m_step(1.0)
{
}

CModelSelectionParameterRangeBuilder::CModelSelectionParameterRangeBuilder(
CModelSelectionParameters* parameters, ERangeType type) :
m_parameters(parameters), m_type(type), m_from(), m_to(), m_step(1.0)
{
}

CModelSelectionParameterRangeBuilder::~CModelSelectionParameterRangeBuilder()
{
}

CModelSelectionParameterRangeBuilder* CModelSelectionParameterRangeBuilder::from_value(float64_t value)
{
m_from.bound = value;
m_from.set = true;
update_range();
return this;
}

CModelSelectionParameterRangeBuilder* CModelSelectionParameterRangeBuilder::to_value(float64_t value)
{
m_to.bound = value;
m_to.set = true;
update_range();
return this;
}

CModelSelectionParameterRangeBuilder* CModelSelectionParameterRangeBuilder::step(float64_t s)
{
if (m_to.set && m_from.set) {
REQUIRE(s < (m_from.bound - m_to.bound), "The step is greater than the width of the range");
}
m_step = s;
return this;
}

void CModelSelectionParameterRangeBuilder::update_range()
{
if (m_parameters)
{
if (m_from.set)
{
if (m_to.set)
{
REQUIRE(m_from.bound < m_to.bound, "The lower bound is greater than the upper bound");
m_parameters->build_values(m_from.bound, m_to.bound, m_type, m_step);
}
else
m_parameters->build_values(m_from.bound, m_from.bound+10.0, m_type, m_step);
}
else
{
if (m_to.set)
m_parameters->build_values(m_to.bound-10.0, m_to.bound, m_type, m_step);
else
m_parameters->build_values(-10.0, 10.0, m_type, m_step);
}
}
}

}
95 changes: 95 additions & 0 deletions src/shogun/modelselection/ModelSelectionParameterRangeBuilder.h
@@ -0,0 +1,95 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2013 Sergey Lisitsyn
*/

#ifndef SHOGUN_MODELSELECTION_MODELSELECTIONPARAMETERRANGEBUILDER_H_
#define SHOGUN_MODELSELECTION_MODELSELECTIONPARAMETERRANGEBUILDER_H_

#include <shogun/modelselection/ModelSelectionParameters.h>

namespace shogun
{

struct RangeBound
{
RangeBound() :
bound(0.0), set(false) {}
RangeBound(float64_t bound_v, bool set_v) :
bound(bound_v), set(set_v) {}

float64_t bound;
bool set;
};

/** @brief a class that represents a range of a model
* selection parameter. Uses builder-like syntax to
* set ranges and step.
*
*/
class CModelSelectionParameterRangeBuilder : public CSGObject
{
public:
/** Constructor */
CModelSelectionParameterRangeBuilder();
/** Constructor
*
* @param parameters the instance of CModelSelectionParameters to set range for
* @param type the type of the range (linear, exponential, ...)
*/
CModelSelectionParameterRangeBuilder(CModelSelectionParameters* parameters, ERangeType type);
/** Destructor */
virtual ~CModelSelectionParameterRangeBuilder();
/** Sets the 'from' value (lower bound) of the range. Updates the range
* afterwards and sets the upper bound to (the lower bound + 10.0)
* in case the upper bound is not set.
*
* @param value the value of the lower bound
*/
CModelSelectionParameterRangeBuilder* from_value(float64_t value);
/** Sets the 'to' value (upper bound) of the range. Updates the range
* afterwards and sets the lower bound to (the upper bound - 10.0)
* in case the lower bound is not set.
*
* @param value the value of the upper bound
*/
CModelSelectionParameterRangeBuilder* to_value(float64_t value);
/** Sets the step of the range. Updates the range afterwards.
*
* @param step the value of the step to be set
*/
CModelSelectionParameterRangeBuilder* step(float64_t s);
/** Returns the name of the object */
virtual const char* get_name() const
{
return "ModelSelectionParametersRangeHelper";
}

protected:

/** Updates the range.
*
* Keeps the bounds consistent all the time
* with setting the width of the range to 10.0 if
* some bound is missed. Normally uses the provided
* bounds.
*
*/
void update_range();

private:

CModelSelectionParameters* m_parameters;
ERangeType m_type;
RangeBound m_from;
RangeBound m_to;
float64_t m_step;
};

}

#endif
7 changes: 3 additions & 4 deletions src/shogun/modelselection/ModelSelectionParameters.cpp
Expand Up @@ -12,6 +12,7 @@

#include <shogun/modelselection/ModelSelectionParameters.h>
#include <shogun/modelselection/ParameterCombination.h>
#include <shogun/modelselection/ModelSelectionParameterRangeBuilder.h>
#include <shogun/lib/DataType.h>
#include <shogun/base/Parameter.h>
#include <shogun/base/DynArray.h>
Expand Down Expand Up @@ -718,9 +719,7 @@ void CModelSelectionParameters::delete_values()
}
}

CModelSelectionParametersRangeHelper* CModelSelectionParameters::as_range(ERangeType type)
CModelSelectionParameterRangeBuilder* CModelSelectionParameters::as_range(ERangeType type)
{
return new CModelSelectionParametersRangeHelper(this, type);
return new CModelSelectionParameterRangeBuilder(this, type);
}


125 changes: 3 additions & 122 deletions src/shogun/modelselection/ModelSelectionParameters.h
Expand Up @@ -19,6 +19,8 @@ namespace shogun

class CParameterCombination;

class CModelSelectionParameterRangeBuilder;

/** type of range */
enum ERangeType
{
Expand Down Expand Up @@ -46,8 +48,6 @@ enum EMSParamType
MSPT_INT32_SGVECTOR,
};

class CModelSelectionParametersRangeHelper;

/**
* @brief Class to select parameters and their ranges for model selection. The
* structure is organized as a tree with different kinds of nodes, depending on
Expand Down Expand Up @@ -109,7 +109,7 @@ class CModelSelectionParameters: public CSGObject
*
* @param type a type of range (linear, exponential, ...)
*/
CModelSelectionParametersRangeHelper* as_range(ERangeType type);
CModelSelectionParameterRangeBuilder* as_range(ERangeType type);

/** SG_PRINT's the tree of which this node is the base
*
Expand Down Expand Up @@ -267,125 +267,6 @@ template <class T> SGVector<T> create_range_array(T min, T max,
return result;
}

struct RangeBound
{
RangeBound() :
bound(0.0), set(false) {}
RangeBound(float64_t bound_v, bool set_v) :
bound(bound_v), set(set_v) {}

float64_t bound;
bool set;
};

/** @brief a class that represents a range of a model
* selection parameter. Uses builder-like syntax to
* set ranges and step.
*
*/
class CModelSelectionParametersRangeHelper : public CSGObject
{
public:
/** Constructor */
CModelSelectionParametersRangeHelper() :
m_parameters(NULL), m_type(R_LINEAR), m_from(), m_to(), m_step(1.0) {};
/** Constructor
*
* @param parameters the instance of CModelSelectionParameters to set range for
* @param type the type of the range (linear, exponential, ...)
*/
CModelSelectionParametersRangeHelper(CModelSelectionParameters* parameters, ERangeType type) :
m_parameters(parameters), m_type(type), m_from(), m_to(), m_step(1.0) {};
/** Destructor */
virtual ~CModelSelectionParametersRangeHelper()
{
}
/** Sets the 'from' value (lower bound) of the range. Updates the range
* afterwards and sets the upper bound to (the lower bound + 10.0)
* in case the upper bound is not set.
*
* @param value the value of the lower bound
*/
CModelSelectionParametersRangeHelper* from_value(float64_t value)
{
m_from.bound = value;
m_from.set = true;
update_range();
return this;
}
/** Sets the 'to' value (upper bound) of the range. Updates the range
* afterwards and sets the lower bound to (the upper bound - 10.0)
* in case the lower bound is not set.
*
* @param value the value of the upper bound
*/
CModelSelectionParametersRangeHelper* to_value(float64_t value)
{
m_to.bound = value;
m_to.set = true;
update_range();
return this;
}
/** Sets the step of the range. Updates the range afterwards.
*
* @param step the value of the step to be set
*/
CModelSelectionParametersRangeHelper* step(float64_t s)
{
if (m_to.set && m_from.set) {
REQUIRE(s < (m_from.bound - m_to.bound), "The step is greater than the width of the range");
}
m_step = s;
return this;
}
/** Returns the name of the object */
virtual const char* get_name() const
{
return "ModelSelectionParametersRangeHelper";
}

protected:

/** Updates the range.
*
* Keeps the bounds consistent all the time
* with setting the width of the range to 10.0 if
* some bound is missed. Normally uses the provided
* bounds.
*
*/
void update_range()
{
if (m_parameters)
{
if (m_from.set)
{
if (m_to.set)
{
REQUIRE(m_from.bound < m_to.bound, "The lower bound is greater than the upper bound");
m_parameters->build_values(m_from.bound, m_to.bound, m_type, m_step);
}
else
m_parameters->build_values(m_from.bound, m_from.bound+10.0, m_type, m_step);
}
else
{
if (m_to.set)
m_parameters->build_values(m_to.bound-10.0, m_to.bound, m_type, m_step);
else
m_parameters->build_values(-10.0, 10.0, m_type, m_step);
}
}
}

private:

CModelSelectionParameters* m_parameters;
ERangeType m_type;
RangeBound m_from;
RangeBound m_to;
float64_t m_step;
};

}
#endif /* __MODELSELECTIONPARAMETERS_H_ */

0 comments on commit e46acce

Please sign in to comment.