Skip to content

Commit

Permalink
fixed the ordering of constraints weights in RankSVM, ref #57, ref #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Benazera committed Jan 30, 2015
1 parent b0eca41 commit 60d329f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
15 changes: 7 additions & 8 deletions src/surrogates/rankingsvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class RankingSVM
int nalphas = x.cols()-1;
_C = dMat::Constant(nalphas,1,_Cval);
for (int i=0;i<nalphas;i++)
_C(i) = _Cval*pow(nalphas-i,3);
_dKij = dMat(nalphas,nalphas);
_C(nalphas-1-i) = _Cval*pow(nalphas-i,2);
_dKij = dMat::Zero(nalphas,nalphas);
_alpha = dVec::Zero(nalphas);

if (_encode)
Expand Down Expand Up @@ -183,7 +183,7 @@ class RankingSVM
{
if (_alpha.size() == 0)
return; // model is not yet trained.
fit = dVec(x_test.cols());
fit = dVec::Zero(x_test.cols());
if (_encode)
{
encode(x_train,covinv,xmean);
Expand Down Expand Up @@ -254,8 +254,8 @@ class RankingSVM
const int &niter)
{
// initialization of temporary variables
dVec sum_alphas(_dKij.cols());
dMat div_dKij(_dKij.rows(),_dKij.cols());
dVec sum_alphas = dVec::Zero(_dKij.cols());
dMat div_dKij = dMat::Zero(_dKij.rows(),_dKij.cols());
#pragma omp parallel
{
#pragma omp for
Expand All @@ -277,7 +277,6 @@ class RankingSVM
div_dKij(i,j) = _dKij(i,j) / _dKij(j,j);
}
sum_alphas(i) = (_epsilon - sum_alpha) / _dKij(i,i);

}
}

Expand All @@ -292,7 +291,7 @@ class RankingSVM
new_alpha = old_alpha + sum_alphas(i1);
new_alpha = std::max(std::min(new_alpha,_C(i1)),0.0);
delta_alpha = new_alpha - old_alpha;
double dL = delta_alpha * _dKij(i1,i1) * (sum_alphas(i1) - 0.5*delta_alpha);
double dL = delta_alpha * _dKij(i1,i1) * (sum_alphas(i1) - 0.5*delta_alpha + _epsilon);
if (dL > 0)
{
sum_alphas -= delta_alpha * div_dKij.row(i1);
Expand Down Expand Up @@ -347,7 +346,7 @@ class RankingSVM
dVec _alpha; /**< vector of Ranking SVM parameters over ranking constraints. */
dMat _dKij;
dMat _C; /**< constraint violation weights. */
double _Cval = 10e6; /**< constraing violation base weight value. */
double _Cval = 1e6; /**< constraing violation base weight value. */
double _epsilon = 1.0;

TKernel _kernel; /**< kernel class. */
Expand Down
16 changes: 9 additions & 7 deletions src/surrogates/rsvm_surr_strategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ namespace libcmaes

// Interfacing candidates with an Eigen matrix of double x and a vector of objective function value.
void to_mat_vec(std::vector<Candidate> &cp,
dMat &x, dVec &fvalues)
dMat &x, dVec &fvalues,
const bool &train)
{
std::sort(cp.begin(),cp.end(),
[](Candidate const &c1, Candidate const &c2){return c1.get_fvalue() > c2.get_fvalue();}); // descending sort
if (train)
std::sort(cp.begin(),cp.end(),
[](Candidate const &c1, Candidate const &c2){return c1.get_fvalue() > c2.get_fvalue();}); // descending sort
x = dMat(cp.at(0).get_x_size(),cp.size());
fvalues = dVec(cp.size());
for (int i=0;i<(int)cp.size();i++)
Expand All @@ -62,7 +64,7 @@ namespace libcmaes
dMat x;
dVec fvalues;
std::vector<Candidate> cp = c;
to_mat_vec(cp,x,fvalues);
to_mat_vec(cp,x,fvalues,true);
dVec xmean = eostrat<TGenoPheno>::get_solutions().xmean();
_rsvm = RankingSVM<RBFKernel>();
_rsvm._encode = true;
Expand All @@ -78,7 +80,7 @@ namespace libcmaes
dMat x_train;
dVec fvalues;
std::vector<Candidate> tset = this->_tset;
to_mat_vec(tset,x_train,fvalues);
to_mat_vec(tset,x_train,fvalues,true);

dVec fit;
dVec xmean = eostrat<TGenoPheno>::get_solutions().xmean();
Expand All @@ -90,10 +92,10 @@ namespace libcmaes
};
}

~RSVMSurrogateStrategy() {};
~RSVMSurrogateStrategy() {}

RankingSVM<RBFKernel> _rsvm;
int _rsvm_iter = 5e6; /**< number of iterations for optimizing the ranking SVM */
int _rsvm_iter = 1e6; /**< number of iterations for optimizing the ranking SVM */
};

}
Expand Down
7 changes: 4 additions & 3 deletions tests/surr-tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ template<template <class U, class V> class TStrategy, class TCovarianceUpdate=Co
optim.set_exploit(!FLAGS_no_exploit);
optim.set_prelambda(FLAGS_prelambda);
optim.set_lambdaprime(lambdaprime);
optim._rsvm_iter = FLAGS_rsvm_iter;
//optim._rsvm_iter = FLAGS_rsvm_iter;
optim._rsvm_iter = 50000*std::sqrt(dim);
if (FLAGS_fname == "elli"
|| FLAGS_fname == "rosenbrock")
optim.set_l(std::floor(40.0*std::sqrt(dim)));
else if (FLAGS_fname == "rastrigin")
optim.set_l(std::floor(70.0*std::sqrt(dim)));
else if (FLAGS_fname == "rastrigin")
{
optim.set_theta_sel0(0.6);
optim.set_theta_sel1(0.6);
Expand Down

0 comments on commit 60d329f

Please sign in to comment.