Create files for OptimizerFactory class; add the unit test.

This commit is contained in:
Tetsuo Kiso 2012-03-18 13:08:38 +09:00
parent 82c683758f
commit 724b5cbb37
7 changed files with 156 additions and 87 deletions

View File

@ -22,6 +22,7 @@ PerScorer.cpp
Scorer.cpp
ScorerFactory.cpp
Optimizer.cpp
OptimizerFactory.cpp
TER/alignmentStruct.cpp
TER/hashMap.cpp
TER/hashMapStringInfos.cpp
@ -51,6 +52,7 @@ alias programs : mert extractor evaluator pro ;
unit-test feature_data_test : FeatureDataTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test data_test : DataTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test ngram_test : NgramTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test optimizer_factory_test : OptimizerFactoryTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test reference_test : ReferenceTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test timer_test : TimerTest.cpp mert_lib ..//boost_unit_test_framework ;
unit-test util_test : UtilTest.cpp mert_lib ..//boost_unit_test_framework ;

View File

@ -462,63 +462,3 @@ statscore_t RandomOptimizer::TrueRun(Point& P) const
P.SetScore(score);
return score;
}
//--------------------------------------
vector<string> OptimizerFactory::m_type_names;
void OptimizerFactory::SetTypeNames()
{
if (m_type_names.empty()) {
m_type_names.resize(NOPTIMIZER);
m_type_names[POWELL]="powell";
m_type_names[RANDOM_DIRECTION]="random-direction";
m_type_names[RANDOM]="random";
// Add new type there
}
}
vector<string> OptimizerFactory::GetTypeNames()
{
if (m_type_names.empty())
SetTypeNames();
return m_type_names;
}
OptimizerFactory::OptType OptimizerFactory::GetOType(const string& type)
{
unsigned int thetype;
if (m_type_names.empty())
SetTypeNames();
for (thetype = 0; thetype < m_type_names.size(); thetype++)
if (m_type_names[thetype] == type)
break;
return((OptType)thetype);
}
Optimizer* OptimizerFactory::BuildOptimizer(unsigned dim, const vector<unsigned>& i2o, const vector<parameter_t>& start, const string& type, unsigned int nrandom)
{
OptType T = GetOType(type);
if (T == NOPTIMIZER) {
cerr << "Error: unknown Optimizer type " << type << endl;
cerr << "Known Algorithm are:" << endl;
unsigned int thetype;
for (thetype = 0; thetype < m_type_names.size(); thetype++)
cerr << m_type_names[thetype] << endl;
throw ("unknown Optimizer Type");
}
switch ((OptType)T) {
case POWELL:
return new SimpleOptimizer(dim, i2o, start, nrandom);
break;
case RANDOM_DIRECTION:
return new RandomDirectionOptimizer(dim, i2o, start, nrandom);
break;
case RANDOM:
return new RandomOptimizer(dim, i2o, start, nrandom);
break;
default:
cerr << "Error: unknown optimizer" << type << endl;
return NULL;
}
}

View File

@ -104,31 +104,4 @@ public:
virtual statscore_t TrueRun(Point&) const;
};
class OptimizerFactory
{
public:
static vector<string> GetTypeNames();
static Optimizer* BuildOptimizer(unsigned dim, const vector<unsigned>& to_optimize, const vector<parameter_t>& start, const string& type, unsigned int nrandom);
private:
OptimizerFactory() {}
~OptimizerFactory() {}
// Add new optimizer here BEFORE NOPTIMZER
enum OptType {
POWELL = 0,
RANDOM_DIRECTION = 1,
RANDOM,
NOPTIMIZER
};
// Get optimizer type.
static OptType GetOType(const string& type);
// Setup optimization types.
static void SetTypeNames();
static vector<string> m_type_names;
};
#endif // OPTIMIZER_H

66
mert/OptimizerFactory.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "OptimizerFactory.h"
#include "Optimizer.h"
using namespace std;
vector<string> OptimizerFactory::m_type_names;
void OptimizerFactory::SetTypeNames()
{
if (m_type_names.empty()) {
m_type_names.resize(NOPTIMIZER);
m_type_names[POWELL] = "powell";
m_type_names[RANDOM_DIRECTION] = "random-direction";
m_type_names[RANDOM] = "random";
// Add new type there
}
}
vector<string> OptimizerFactory::GetTypeNames()
{
if (m_type_names.empty())
SetTypeNames();
return m_type_names;
}
OptimizerFactory::OptimizerType OptimizerFactory::GetOptimizerType(const string& type)
{
unsigned int t;
if (m_type_names.empty())
SetTypeNames();
for (t = 0; t < m_type_names.size(); t++)
if (m_type_names[t] == type)
break;
return((OptimizerType)t);
}
Optimizer* OptimizerFactory::BuildOptimizer(unsigned dim,
const vector<unsigned>& i2o,
const vector<parameter_t>& start,
const string& type,
unsigned int nrandom)
{
OptimizerType opt_type = GetOptimizerType(type);
if (opt_type == NOPTIMIZER) {
cerr << "Error: unknown Optimizer type " << type << endl;
cerr << "Known Algorithm are:" << endl;
unsigned int t;
for (t = 0; t < m_type_names.size(); t++)
cerr << m_type_names[t] << endl;
throw ("unknown Optimizer Type");
}
switch (opt_type) {
case POWELL:
return new SimpleOptimizer(dim, i2o, start, nrandom);
break;
case RANDOM_DIRECTION:
return new RandomDirectionOptimizer(dim, i2o, start, nrandom);
break;
case RANDOM:
return new RandomOptimizer(dim, i2o, start, nrandom);
break;
default:
cerr << "Error: unknown optimizer" << type << endl;
return NULL;
}
}

41
mert/OptimizerFactory.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef MERT_OPTIMIZER_FACTORY_H_
#define MERT_OPTIMIZER_FACTORY_H_
#include <vector>
#include "Types.h"
class Optimizer;
class OptimizerFactory
{
public:
// NOTE: Add new optimizer here BEFORE NOPTIMZER
enum OptimizerType {
POWELL = 0,
RANDOM_DIRECTION = 1,
RANDOM,
NOPTIMIZER
};
static std::vector<string> GetTypeNames();
// Setup optimization types.
static void SetTypeNames();
// Get optimizer type.
static OptimizerType GetOptimizerType(const std::string& type);
static Optimizer* BuildOptimizer(unsigned dim,
const std::vector<unsigned>& to_optimize,
const std::vector<parameter_t>& start,
const std::string& type,
unsigned int nrandom);
private:
OptimizerFactory() {}
~OptimizerFactory() {}
static vector<string> m_type_names;
};
#endif // MERT_OPTIMIZER_FACTORY_H_

View File

@ -0,0 +1,46 @@
#include "OptimizerFactory.h"
#include "Optimizer.h"
#define BOOST_TEST_MODULE MertOptimizerFactory
#include <boost/test/unit_test.hpp>
#include <boost/scoped_ptr.hpp>
namespace {
inline void CheckBuildOptimizer(unsigned dim,
const vector<unsigned>& to_optimize,
const vector<parameter_t>& start,
const string& type,
unsigned int num_random) {
boost::scoped_ptr<Optimizer> optimizer(
OptimizerFactory::BuildOptimizer(dim, to_optimize, start, type, num_random));
BOOST_CHECK(optimizer.get() != NULL);
}
} // namespace
BOOST_AUTO_TEST_CASE(optimizer_type) {
BOOST_CHECK_EQUAL(OptimizerFactory::GetOptimizerType("powell"),
OptimizerFactory::POWELL);
BOOST_CHECK_EQUAL(OptimizerFactory::GetOptimizerType("random"),
OptimizerFactory::RANDOM);
BOOST_CHECK_EQUAL(OptimizerFactory::GetOptimizerType("random-direction"),
OptimizerFactory::RANDOM_DIRECTION);
}
BOOST_AUTO_TEST_CASE(optimizer_build) {
const unsigned dim = 3;
std::vector<unsigned> to_optimize;
to_optimize.push_back(1);
to_optimize.push_back(2);
to_optimize.push_back(3);
std::vector<parameter_t> start;
start.push_back(0.3);
start.push_back(0.1);
start.push_back(0.2);
const unsigned int num_random = 1;
CheckBuildOptimizer(dim, to_optimize, start, "powell", num_random);
CheckBuildOptimizer(dim, to_optimize, start, "random", num_random);
CheckBuildOptimizer(dim, to_optimize, start, "random-direction", num_random);
}

View File

@ -20,6 +20,7 @@
#include "ScoreData.h"
#include "FeatureData.h"
#include "Optimizer.h"
#include "OptimizerFactory.h"
#include "Types.h"
#include "Timer.h"
#include "Util.h"