diff options
author | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-03-18 08:08:38 +0400 |
---|---|---|
committer | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2012-03-18 08:08:38 +0400 |
commit | 54233d327052793807fc44583334dd3b396176e5 (patch) | |
tree | db01a4c85371b5d9669d2e82e366af42504b42fd /mert | |
parent | 38c662d4e036558ce07cc4c469d11631b8186be8 (diff) |
Create files for OptimizerFactory class; add the unit test.
Diffstat (limited to 'mert')
-rw-r--r-- | mert/Jamfile | 2 | ||||
-rw-r--r-- | mert/Optimizer.cpp | 60 | ||||
-rw-r--r-- | mert/Optimizer.h | 27 | ||||
-rw-r--r-- | mert/OptimizerFactory.cpp | 66 | ||||
-rw-r--r-- | mert/OptimizerFactory.h | 41 | ||||
-rw-r--r-- | mert/OptimizerFactoryTest.cpp | 46 | ||||
-rw-r--r-- | mert/mert.cpp | 1 |
7 files changed, 156 insertions, 87 deletions
diff --git a/mert/Jamfile b/mert/Jamfile index 8879253d5..b8a76396a 100644 --- a/mert/Jamfile +++ b/mert/Jamfile @@ -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 ; diff --git a/mert/Optimizer.cpp b/mert/Optimizer.cpp index 6d35a3567..8331342a0 100644 --- a/mert/Optimizer.cpp +++ b/mert/Optimizer.cpp @@ -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; - } -} diff --git a/mert/Optimizer.h b/mert/Optimizer.h index 78e07f819..34dce01a7 100644 --- a/mert/Optimizer.h +++ b/mert/Optimizer.h @@ -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 diff --git a/mert/OptimizerFactory.cpp b/mert/OptimizerFactory.cpp new file mode 100644 index 000000000..06b50d20b --- /dev/null +++ b/mert/OptimizerFactory.cpp @@ -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; + } +} diff --git a/mert/OptimizerFactory.h b/mert/OptimizerFactory.h new file mode 100644 index 000000000..4c90c962f --- /dev/null +++ b/mert/OptimizerFactory.h @@ -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_ diff --git a/mert/OptimizerFactoryTest.cpp b/mert/OptimizerFactoryTest.cpp new file mode 100644 index 000000000..30734ab67 --- /dev/null +++ b/mert/OptimizerFactoryTest.cpp @@ -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); +} diff --git a/mert/mert.cpp b/mert/mert.cpp index 2455aa39f..5acec897c 100644 --- a/mert/mert.cpp +++ b/mert/mert.cpp @@ -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" |