2008-05-15 18:04:42 +04:00
|
|
|
/**
|
2011-12-12 17:48:42 +04:00
|
|
|
* \description This is the main for the new version of the mert algorithm developed during the 2nd MT marathon
|
2008-05-15 18:04:42 +04:00
|
|
|
*/
|
2008-05-15 15:06:32 +04:00
|
|
|
|
|
|
|
#include <limits>
|
2009-02-12 15:58:27 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
2008-05-15 15:06:32 +04:00
|
|
|
#include "Data.h"
|
|
|
|
#include "Point.h"
|
|
|
|
#include "Scorer.h"
|
2008-05-27 20:50:52 +04:00
|
|
|
#include "ScorerFactory.h"
|
2008-05-15 15:06:32 +04:00
|
|
|
#include "ScoreData.h"
|
|
|
|
#include "FeatureData.h"
|
|
|
|
#include "Optimizer.h"
|
2008-05-15 23:09:01 +04:00
|
|
|
#include "Types.h"
|
2008-05-16 23:57:01 +04:00
|
|
|
#include "Timer.h"
|
2008-05-16 11:09:15 +04:00
|
|
|
#include "Util.h"
|
2008-05-15 15:06:32 +04:00
|
|
|
|
2011-09-07 12:08:35 +04:00
|
|
|
#include "../moses/src/ThreadPool.h"
|
|
|
|
|
2012-02-01 07:35:50 +04:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace {
|
2008-05-15 15:06:32 +04:00
|
|
|
|
2012-02-01 07:35:50 +04:00
|
|
|
/**
|
|
|
|
* Runs an optimisation, or a random restart.
|
|
|
|
*/
|
|
|
|
class OptimizationTask : public Moses::Task {
|
|
|
|
public:
|
|
|
|
OptimizationTask(Optimizer* optimizer, const Point& point)
|
|
|
|
: m_optimizer(optimizer), m_point(point) {}
|
|
|
|
|
|
|
|
~OptimizationTask() {}
|
|
|
|
|
|
|
|
void resetOptimizer() {
|
|
|
|
if (m_optimizer) {
|
|
|
|
delete m_optimizer;
|
|
|
|
m_optimizer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteAfterExecution() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
m_score = m_optimizer->Run(m_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
statscore_t getScore() const {
|
|
|
|
return m_score;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Point& getPoint() const {
|
|
|
|
return m_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Do not allow the user to instanciate without arguments.
|
|
|
|
OptimizationTask() {}
|
|
|
|
|
|
|
|
Optimizer* m_optimizer;
|
|
|
|
Point m_point;
|
|
|
|
statscore_t m_score;
|
|
|
|
};
|
2008-05-15 18:04:42 +04:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
void usage(int ret)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2008-05-16 00:49:49 +04:00
|
|
|
cerr<<"usage: mert -d <dimensions> (mandatory )"<<endl;
|
2011-08-03 21:00:17 +04:00
|
|
|
cerr<<"[-n] retry ntimes (default 1)"<<endl;
|
|
|
|
cerr<<"[-m] number of random directions in powell (default 0)"<<endl;
|
|
|
|
cerr<<"[-o] the indexes to optimize(default all)"<<endl;
|
|
|
|
cerr<<"[-t] the optimizer(default powell)"<<endl;
|
|
|
|
cerr<<"[-r] the random seed (defaults to system clock)"<<endl;
|
2008-05-16 23:57:01 +04:00
|
|
|
cerr<<"[--sctype|-s] the scorer type (default BLEU)"<<endl;
|
2008-06-24 23:27:18 +04:00
|
|
|
cerr<<"[--scconfig|-c] configuration string passed to scorer"<<endl;
|
2008-11-18 21:51:02 +03:00
|
|
|
cerr<<"[--scfile|-S] comma separated list of scorer data files (default score.data)"<<endl;
|
|
|
|
cerr<<"[--ffile|-F] comma separated list of feature data files (default feature.data)"<<endl;
|
2010-01-08 18:12:28 +03:00
|
|
|
cerr<<"[--ifile|-i] the starting point data file (default init.opt)"<<endl;
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
2011-09-15 21:45:35 +04:00
|
|
|
cerr<<"[--threads|-T] use multiple threads (default 1)"<<endl;
|
2011-09-07 12:08:35 +04:00
|
|
|
#endif
|
2011-09-15 21:45:35 +04:00
|
|
|
cerr<<"[--shard-count] Split data into shards, optimize for each shard and average"<<endl;
|
|
|
|
cerr<<"[--shard-size] Shard size as proportion of data. If 0, use non-overlapping shards"<<endl;
|
2010-01-08 18:12:28 +03:00
|
|
|
cerr<<"[-v] verbose level"<<endl;
|
2008-06-24 23:27:18 +04:00
|
|
|
cerr<<"[--help|-h] print this message and exit"<<endl;
|
2011-09-15 21:45:35 +04:00
|
|
|
exit(ret);
|
2008-05-15 15:06:32 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"pdim", 1, 0, 'd'},
|
|
|
|
{"ntry",1,0,'n'},
|
2011-07-23 04:24:45 +04:00
|
|
|
{"nrandom",1,0,'m'},
|
2011-02-24 15:42:19 +03:00
|
|
|
{"rseed",required_argument,0,'r'},
|
|
|
|
{"optimize",1,0,'o'},
|
2011-11-12 04:24:19 +04:00
|
|
|
{"pro",required_argument,0,'p'},
|
2011-02-24 15:42:19 +03:00
|
|
|
{"type",1,0,'t'},
|
|
|
|
{"sctype",1,0,'s'},
|
|
|
|
{"scconfig",required_argument,0,'c'},
|
|
|
|
{"scfile",1,0,'S'},
|
|
|
|
{"ffile",1,0,'F'},
|
|
|
|
{"ifile",1,0,'i'},
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
{"threads", required_argument,0,'T'},
|
|
|
|
#endif
|
2011-09-15 21:45:35 +04:00
|
|
|
{"shard-count", required_argument, 0, 'a'},
|
|
|
|
{"shard-size", required_argument, 0, 'b'},
|
2011-02-24 15:42:19 +03:00
|
|
|
{"verbose",1,0,'v'},
|
|
|
|
{"help",no_argument,0,'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2008-05-15 18:04:42 +04:00
|
|
|
int option_index;
|
2008-05-15 15:06:32 +04:00
|
|
|
|
2012-02-01 07:35:50 +04:00
|
|
|
} // anonymous namespace
|
2011-09-07 12:08:35 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
ResetUserTime();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Timer timer;
|
|
|
|
timer.start("Starting...");
|
|
|
|
*/
|
|
|
|
|
2012-02-01 07:37:41 +04:00
|
|
|
int c,pdim;
|
2008-05-16 14:57:24 +04:00
|
|
|
pdim=-1;
|
2008-05-15 18:04:42 +04:00
|
|
|
int ntry=1;
|
2011-07-23 04:24:45 +04:00
|
|
|
int nrandom=0;
|
2009-02-12 15:58:27 +03:00
|
|
|
int seed=0;
|
|
|
|
bool hasSeed = false;
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
size_t threads=1;
|
|
|
|
#endif
|
2011-09-15 21:45:35 +04:00
|
|
|
float shard_size = 0;
|
|
|
|
size_t shard_count = 0;
|
2008-05-15 18:04:42 +04:00
|
|
|
string type("powell");
|
|
|
|
string scorertype("BLEU");
|
2008-06-24 23:27:18 +04:00
|
|
|
string scorerconfig("");
|
2008-05-16 00:49:49 +04:00
|
|
|
string scorerfile("statscore.data");
|
|
|
|
string featurefile("features.data");
|
2008-05-23 15:48:16 +04:00
|
|
|
string initfile("init.opt");
|
2010-01-08 18:12:28 +03:00
|
|
|
|
|
|
|
string tooptimizestr("");
|
|
|
|
vector<unsigned> tooptimize;
|
2011-07-23 04:24:45 +04:00
|
|
|
vector<vector<parameter_t> > start_list;
|
2011-07-04 01:01:16 +04:00
|
|
|
vector<parameter_t> min;
|
|
|
|
vector<parameter_t> max;
|
2011-11-12 03:58:23 +04:00
|
|
|
// NOTE: those mins and max are the bound for the starting points of the algorithm, not strict bound on the result!
|
2011-07-04 01:01:16 +04:00
|
|
|
|
2011-08-03 21:00:17 +04:00
|
|
|
while ((c=getopt_long (argc, argv, "o:r:d:n:m:t:s:S:F:v:p:", long_options, &option_index)) != -1) {
|
2008-05-15 15:06:32 +04:00
|
|
|
switch (c) {
|
2011-11-12 04:24:19 +04:00
|
|
|
case 'o':
|
|
|
|
tooptimizestr = string(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
pdim = strtol(optarg, NULL, 10);
|
|
|
|
break;
|
|
|
|
case 'n':
|
2011-11-17 19:58:56 +04:00
|
|
|
ntry=strtol(optarg, NULL, 10);
|
|
|
|
break;
|
2011-11-12 04:24:19 +04:00
|
|
|
case 'm':
|
|
|
|
nrandom=strtol(optarg, NULL, 10);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
seed=strtol(optarg, NULL, 10);
|
|
|
|
hasSeed = true;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
type=string(optarg);
|
|
|
|
break;
|
|
|
|
case's':
|
|
|
|
scorertype=string(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
scorerconfig = string(optarg);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
scorerfile=string(optarg);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
featurefile=string(optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
initfile=string(optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
setverboselevel(strtol(optarg,NULL,10));
|
|
|
|
break;
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
2011-11-12 04:24:19 +04:00
|
|
|
case 'T':
|
|
|
|
threads = strtol(optarg, NULL, 10);
|
|
|
|
if (threads < 1) threads = 1;
|
|
|
|
break;
|
2011-09-07 12:08:35 +04:00
|
|
|
#endif
|
2011-11-12 04:24:19 +04:00
|
|
|
case 'a':
|
|
|
|
shard_count = strtof(optarg,NULL);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
shard_size = strtof(optarg,NULL);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(1);
|
2008-05-15 15:06:32 +04:00
|
|
|
}
|
|
|
|
}
|
2008-05-16 14:57:24 +04:00
|
|
|
if (pdim < 0)
|
2011-09-15 21:45:35 +04:00
|
|
|
usage(1);
|
|
|
|
|
|
|
|
cerr << "shard_size = " << shard_size << " shard_count = " << shard_count << endl;
|
|
|
|
if (shard_size && !shard_count) {
|
|
|
|
cerr << "Error: shard-size provided without shard-count" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (shard_size > 1 || shard_size < 0) {
|
|
|
|
cerr << "Error: shard-size should be between 0 and 1" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-12 15:58:27 +03:00
|
|
|
|
|
|
|
if (hasSeed) {
|
2011-02-24 15:42:19 +03:00
|
|
|
cerr << "Seeding random numbers with " << seed << endl;
|
|
|
|
srandom(seed);
|
2009-02-12 15:58:27 +03:00
|
|
|
} else {
|
2011-02-24 15:42:19 +03:00
|
|
|
cerr << "Seeding random numbers with system clock " << endl;
|
|
|
|
srandom(time(NULL));
|
2009-02-12 15:58:27 +03:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-07-23 04:24:45 +04:00
|
|
|
// read in starting points
|
|
|
|
std::string onefile;
|
|
|
|
while (!initfile.empty()) {
|
|
|
|
getNextPound(initfile, onefile, ",");
|
|
|
|
vector<parameter_t> start;
|
|
|
|
ifstream opt(onefile.c_str());
|
|
|
|
if(opt.fail()) {
|
|
|
|
cerr<<"could not open initfile: " << initfile << endl;
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
start.resize(pdim);//to do:read from file
|
|
|
|
int j;
|
|
|
|
for( j=0; j<pdim&&!opt.fail(); j++)
|
|
|
|
opt>>start[j];
|
|
|
|
if(j<pdim) {
|
|
|
|
cerr<<initfile<<":Too few starting weights." << endl;
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
start_list.push_back(start);
|
|
|
|
// for the first time, also read in the min/max values for scores
|
|
|
|
if (start_list.size() == 1) {
|
|
|
|
min.resize(pdim);
|
|
|
|
for( j=0; j<pdim&&!opt.fail(); j++)
|
|
|
|
opt>>min[j];
|
|
|
|
if(j<pdim) {
|
|
|
|
cerr<<initfile<<":Too few minimum weights." << endl;
|
|
|
|
cerr<<"error could not initialize start point with " << initfile << endl;
|
2011-12-12 17:48:42 +04:00
|
|
|
std::cerr << "j: " << j << ", pdim: " << pdim << std::endl;
|
2011-07-23 04:24:45 +04:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
max.resize(pdim);
|
|
|
|
for( j=0; j<pdim&&!opt.fail(); j++)
|
|
|
|
opt>>max[j];
|
|
|
|
if(j<pdim) {
|
|
|
|
cerr<<initfile<<":Too few maximum weights." << endl;
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opt.close();
|
2011-07-04 01:01:16 +04:00
|
|
|
}
|
2008-05-16 14:57:24 +04:00
|
|
|
|
2008-11-18 21:51:02 +03:00
|
|
|
vector<string> ScoreDataFiles;
|
2011-02-24 15:42:19 +03:00
|
|
|
if (scorerfile.length() > 0) {
|
2011-11-11 17:00:30 +04:00
|
|
|
Tokenize(scorerfile.c_str(), ',', &ScoreDataFiles);
|
2008-11-18 21:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> FeatureDataFiles;
|
2011-02-24 15:42:19 +03:00
|
|
|
if (featurefile.length() > 0) {
|
2011-11-11 17:00:30 +04:00
|
|
|
Tokenize(featurefile.c_str(), ',', &FeatureDataFiles);
|
2008-11-18 21:51:02 +03:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
if (ScoreDataFiles.size() != FeatureDataFiles.size()) {
|
2008-11-18 21:51:02 +03:00
|
|
|
throw runtime_error("Error: there is a different number of previous score and feature files");
|
|
|
|
}
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// it make sense to know what parameter set were used to generate the nbest
|
2011-11-11 15:40:59 +04:00
|
|
|
Scorer *TheScorer = ScorerFactory::getScorer(scorertype,scorerconfig);
|
2008-05-21 14:03:48 +04:00
|
|
|
|
2008-11-18 21:51:02 +03:00
|
|
|
//load data
|
2008-05-27 20:50:52 +04:00
|
|
|
Data D(*TheScorer);
|
2011-02-24 15:42:19 +03:00
|
|
|
for (size_t i=0; i < ScoreDataFiles.size(); i++) {
|
2008-11-18 21:51:02 +03:00
|
|
|
cerr<<"Loading Data from: "<< ScoreDataFiles.at(i) << " and " << FeatureDataFiles.at(i) << endl;
|
|
|
|
D.load(FeatureDataFiles.at(i), ScoreDataFiles.at(i));
|
|
|
|
}
|
2008-05-21 14:03:48 +04:00
|
|
|
|
2011-12-12 17:48:42 +04:00
|
|
|
//ADDED_BY_TS
|
|
|
|
D.remove_duplicates();
|
|
|
|
//END_ADDED
|
|
|
|
|
2010-01-08 18:12:28 +03:00
|
|
|
PrintUserTime("Data loaded");
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-09-07 20:37:33 +04:00
|
|
|
// starting point score over latest n-best, accumulative n-best
|
|
|
|
//vector<unsigned> bests;
|
|
|
|
//compute bests with sparse features needs to be implemented
|
|
|
|
//currently sparse weights are not even loaded
|
|
|
|
//statscore_t score = TheScorer->score(bests);
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
if (tooptimizestr.length() > 0) {
|
2010-01-08 18:12:28 +03:00
|
|
|
cerr << "Weights to optimize: " << tooptimizestr << endl;
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// Parse string to get weights to optimize, and set them as active
|
2010-01-08 18:12:28 +03:00
|
|
|
std::string substring;
|
|
|
|
int index;
|
2011-02-24 15:42:19 +03:00
|
|
|
while (!tooptimizestr.empty()) {
|
2010-01-08 18:12:28 +03:00
|
|
|
getNextPound(tooptimizestr, substring, ",");
|
|
|
|
index = D.getFeatureIndex(substring);
|
|
|
|
cerr << "FeatNameIndex:" << index << " to insert" << endl;
|
|
|
|
//index = strtol(substring.c_str(), NULL, 10);
|
2011-02-24 15:42:19 +03:00
|
|
|
if (index >= 0 && index < pdim) {
|
|
|
|
tooptimize.push_back(index);
|
|
|
|
} else {
|
|
|
|
cerr << "Index " << index << " is out of bounds. Allowed indexes are [0," << (pdim-1) << "]." << endl;
|
|
|
|
}
|
2010-01-08 18:12:28 +03:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2010-01-08 18:12:28 +03:00
|
|
|
//set all weights as active
|
|
|
|
tooptimize.resize(pdim);//We'll optimize on everything
|
2011-02-24 15:42:19 +03:00
|
|
|
for(int i=0; i<pdim; i++) {
|
|
|
|
tooptimize[i]=1;
|
|
|
|
}
|
2010-01-08 18:12:28 +03:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:37:33 +04:00
|
|
|
// treat sparse features just like regular features
|
|
|
|
if (D.hasSparseFeatures()) {
|
|
|
|
D.mergeSparseFeatures();
|
|
|
|
}
|
|
|
|
|
2009-01-07 16:30:06 +03:00
|
|
|
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
cerr << "Creating a pool of " << threads << " threads" << endl;
|
|
|
|
Moses::ThreadPool pool(threads);
|
|
|
|
#endif
|
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
Point::setpdim(pdim);
|
|
|
|
Point::setdim(tooptimize.size());
|
2011-09-07 12:08:35 +04:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
//starting points consist of specified points and random restarts
|
|
|
|
vector<Point> startingPoints;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
for (size_t i = 0; i < start_list.size(); ++i) {
|
|
|
|
startingPoints.push_back(Point(start_list[i],min,max));
|
|
|
|
}
|
2011-09-07 12:08:35 +04:00
|
|
|
for (int i = 0; i < ntry; ++i) {
|
2011-09-15 21:45:35 +04:00
|
|
|
startingPoints.push_back(Point(start_list[0],min,max));
|
|
|
|
startingPoints.back().Randomize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vector<vector<OptimizationTask*> > allTasks(1);
|
|
|
|
|
|
|
|
//optional sharding
|
|
|
|
vector<Data> shards;
|
|
|
|
if (shard_count) {
|
|
|
|
D.createShards(shard_count, shard_size, scorerconfig, shards);
|
|
|
|
allTasks.resize(shard_count);
|
|
|
|
}
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// launch tasks
|
2011-09-15 21:45:35 +04:00
|
|
|
for (size_t i = 0 ; i < allTasks.size(); ++i) {
|
|
|
|
Data& data = D;
|
|
|
|
if (shard_count) data = shards[i]; //use the sharded data if it exists
|
|
|
|
vector<OptimizationTask*>& tasks = allTasks[i];
|
2011-11-11 15:40:59 +04:00
|
|
|
Optimizer *O = OptimizerFactory::BuildOptimizer(pdim,tooptimize,start_list[0],type,nrandom);
|
2011-09-15 21:45:35 +04:00
|
|
|
O->SetScorer(data.getScorer());
|
|
|
|
O->SetFData(data.getFeatureData());
|
|
|
|
//A task for each start point
|
|
|
|
for (size_t j = 0; j < startingPoints.size(); ++j) {
|
2011-11-11 15:40:59 +04:00
|
|
|
OptimizationTask* task = new OptimizationTask(O, startingPoints[j]);
|
2011-09-15 21:45:35 +04:00
|
|
|
tasks.push_back(task);
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
2011-09-15 21:45:35 +04:00
|
|
|
pool.Submit(task);
|
2011-09-07 12:08:35 +04:00
|
|
|
#else
|
2011-09-15 21:45:35 +04:00
|
|
|
task->Run();
|
2011-09-07 12:08:35 +04:00
|
|
|
#endif
|
2011-09-15 21:45:35 +04:00
|
|
|
}
|
2011-09-07 12:08:35 +04:00
|
|
|
}
|
2011-09-15 21:45:35 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// wait for all threads to finish
|
2011-09-07 12:08:35 +04:00
|
|
|
#ifdef WITH_THREADS
|
|
|
|
pool.Stop(true);
|
|
|
|
#endif
|
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
statscore_t total = 0;
|
|
|
|
Point totalP;
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// collect results
|
2011-09-15 21:45:35 +04:00
|
|
|
for (size_t i = 0; i < allTasks.size(); ++i) {
|
|
|
|
statscore_t best=0, mean=0, var=0;
|
|
|
|
Point bestP;
|
|
|
|
for (size_t j = 0; j < allTasks[i].size(); ++j) {
|
|
|
|
statscore_t score = allTasks[i][j]->getScore();
|
|
|
|
mean += score;
|
|
|
|
var += score*score;
|
|
|
|
if (score > best) {
|
|
|
|
bestP = allTasks[i][j]->getPoint();
|
|
|
|
best = score;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2011-09-15 21:45:35 +04:00
|
|
|
|
|
|
|
mean/=(float)ntry;
|
|
|
|
var/=(float)ntry;
|
|
|
|
var=sqrt(abs(var-mean*mean));
|
|
|
|
if (verboselevel()>1)
|
|
|
|
cerr<<"shard " << i << " best score: "<< best << " variance of the score (for "<<ntry<<" try): "<<var<<endl;
|
|
|
|
|
|
|
|
totalP += bestP;
|
|
|
|
total += best;
|
|
|
|
if (verboselevel()>1)
|
|
|
|
cerr << "bestP " << bestP << endl;
|
2008-05-15 18:04:42 +04:00
|
|
|
}
|
2011-09-07 12:08:35 +04:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
//cerr << "totalP: " << totalP << endl;
|
|
|
|
Point finalP = totalP * (1.0 / allTasks.size());
|
|
|
|
statscore_t final = total / allTasks.size();
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
if (verboselevel()>1)
|
2011-09-15 21:45:35 +04:00
|
|
|
cerr << "bestP: " << finalP << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-07-23 04:24:45 +04:00
|
|
|
// L1-Normalization of the best Point
|
|
|
|
if ((int)tooptimize.size() == pdim)
|
2011-09-15 21:45:35 +04:00
|
|
|
finalP.NormalizeL1();
|
2011-01-25 19:10:47 +03:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
cerr << "Best point: " << finalP << " => " << final << endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
ofstream res("weights.txt");
|
2011-09-15 21:45:35 +04:00
|
|
|
res<<finalP<<endl;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-11 15:40:59 +04:00
|
|
|
for (size_t i = 0; i < allTasks.size(); ++i) {
|
|
|
|
allTasks[i][0]->resetOptimizer();
|
|
|
|
for (size_t j = 0; j < allTasks[i].size(); ++j) {
|
|
|
|
delete allTasks[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete TheScorer;
|
2011-02-24 15:42:19 +03:00
|
|
|
PrintUserTime("Stopping...");
|
2008-05-15 15:06:32 +04:00
|
|
|
}
|