Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted model selection range builder out
- Loading branch information
Showing
6 changed files
with
182 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/shogun/modelselection/ModelSelectionParameterRangeBuilder.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
95
src/shogun/modelselection/ModelSelectionParameterRangeBuilder.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters