Commit 6f7429e1 authored by Miroslav Shaltev's avatar Miroslav Shaltev
Browse files

initial commit using version 3.6.1

parents
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta http-equiv="refresh" content="0;url=http://www.gerad.ca/nomad/doxygen/html/index.html" /></head><body></body></html>
\ No newline at end of file
/*-------------------------------------------------------------------*/
/* Example of a problem with categorical variables */
/*-------------------------------------------------------------------*/
/* */
/* . portfolio problem with 2 assets */
/* */
/* . NOMAD is used in batch mode */
/* */
/* . the number of variables is fixed to 4 (2 assets) */
/* */
/* . variables are of the form (t0 v0 t1 v1) where ti is the type */
/* of an asset and vi is the money invested in this asset */
/* */
/* . categorical variables are t0 and t1 */
/* */
/* . with a $10,000 budget, the problem consists in minimizing */
/* some measure of the risk and of the revenue */
/* */
/*-------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main ( int argc , char ** argv ) {
double g1 = 1e20 , g2 = 1e20 , f = 1e20;
if ( argc == 2 ) {
ifstream in ( argv[1] );
if ( !in.fail() ) {
double t0 , v0 , t1 , v1;
in >> t0 >> v0 >> t1 >> v1;
if ( !in.fail() ) {
double v[3] , vmin = 10000;
v[0] = v[1] = v[2] = 0.0;
v[static_cast<int>(t0)] = v0;
v[static_cast<int>(t1)] = v1;
if ( v0 < vmin )
vmin = v0;
if ( v1 < vmin )
vmin = v1;
// constraints (budget and each asset is considered with at least 1$):
double vt = v[0] + v[1] + v[2];
double h = vt - 10000;
g1 = h;
g2 = 1-vmin;
if ( h <= 0 && vmin >= 1 ) {
// compute the risk and revenue:
double vt2 = vt*vt;
double rev = v[0] * 0.0891 + v[1] * 0.2137 + v[2] * 0.2346;
double risk = 0.01 * (v[0]/vt)*(v[0]/vt) +
0.05 * (v[1]/vt)*(v[1]/vt) +
0.09 * (v[2]/vt)*(v[2]/vt) +
0.02 * (v[0]*v[1]/vt2) +
0.02 * (v[0]*v[2]/vt2) +
0.10 * (v[1]*v[2]/vt2);
// the objective is taken as a scaled distance
// between (risk,revenue) and (risk_best,rev_best):
double a = ( risk - 0.01 ) * 100 / 0.08;
double b = ( rev - 891 ) * 100 / 1455;
f = sqrt ( a*a + (100-b)*(100-b) );
}
else
f = 145;
}
in.close();
}
}
cout << g1 << " " << g2 << " " << f << endl;
return 0;
}
/*--------------------------------------*/
/* construct the extended poll points */
/* (categorical neighborhoods) */
/*--------------------------------------*/
#include <iostream>
#include <fstream>
using namespace std;
int main ( int argc , char ** argv ) {
if ( argc != 2 )
return 1;
ifstream in ( argv[1] );
double t0 , v0 , t1 , v1;
in >> t0 >> v0 >> t1 >> v1;
in.close();
if ( in.fail() )
return 1;
int t2 = static_cast<int> ( 3 - t0 - t1 );
// neighbor #1:
cout << t2 << " " << v0 << " " << t1 << " " << v1 << endl;
// neighbor #2:
cout << t0 << " " << v0 << " " << t2 << " " << v1 << endl;
return 0;
}
#!/usr/bin/env python
# Python implementation of Sebastien's example.
if __name__ == '__main__':
import sys
from string import atof
try:
with open(sys.argv[1]) as f:
line = f.readline().split()
t0, v0, t1, v1 = [atof(line[i]) for i in range(4)]
except:
sys.exit(1)
t2 = int(3 - t0 - t1)
print '%d %d %d %d' % (t2, v0, t1, v1) # Neighbor 1.
print '%d %d %d %d' % (t0, v0, t2, v1) # Neighbor 2.
DIMENSION 4
DISPLAY_DEGREE 2
BB_OUTPUT_TYPE EB EB OBJ
BB_INPUT_TYPE ( C R C R )
BB_EXE bb.exe
#NEIGHBORS_EXE "$python neighbors.py"
NEIGHBORS_EXE neighbors.exe
x0 ( 0 100 1 100 )
# display_all_eval yes
display_stats bbe ( sol ) obj
lower_bound ( - 0.0 - 0.0 )
upper_bound ( - 10000 - 10000 )
# TMP_DIR /tmp
F_TARGET 0.0
MAX_BB_EVAL 500
This example illustrates how to run the batch mode of NOMAD
on a problem with categorical variables.
In addition to the usual blackbox executable, the user must provide
an executable for the neighborhoods.
This executable takes a point as input and displays a list of neighbors.
The number of variables must remain constant.
For example:
neighbors.exe x.txt
with x.txt containing '0 100 1 100'
displays:
'2 100 1 100
0 100 2 100'
(two neighbors).
To run the example:
1. compile blackbox executable : g++ -o bb.exe bb.cpp -O3
2. compile neighbors executable: g++ -o neighbors.exe neighbors.cpp -O3
3. run nomad: nomad param.txt
See more details in the NOMAD user guide.
NOTE: another neighbor executable is given as a python script.
Replace NEIGHBORS_EXE neighbors.exe
with NEIGHBORS_EXE "$python neighbors.py"
in the parameters file to use it.
Warning: {
Model use is disabled for problem with categorical variables.
}
NOMAD - version 3.6.1 - www.gerad.ca/nomad
Copyright (C) 2001-2013 {
Mark A. Abramson - The Boeing Company
Charles Audet - Ecole Polytechnique de Montreal
Gilles Couture - Ecole Polytechnique de Montreal
John E. Dennis, Jr. - Rice University
Sebastien Le Digabel - Ecole Polytechnique de Montreal
Christophe Tribes - Ecole Polytechnique de Montreal
}
Funded in part by AFOSR and Exxon Mobil.
License : '$NOMAD_HOME/src/lgpl.txt'
User guide: '$NOMAD_HOME/doc/user_guide.pdf'
Examples : '$NOMAD_HOME/examples'
Tools : '$NOMAD_HOME/tools'
Please report bugs to nomad@gerad.ca
MADS run {
BBE ( SOL ) OBJ
1 ( 0 100.0000000000 1 100.0000000000 ) 159.6460000000
3 ( 0 100.0000000000 1 1100.0000000000 ) 150.4540000000
4 ( 0 100.0000000000 1 4100.0000000000 ) 111.1390000000
6 ( 0 100.0000000000 1 6100.0000000000 ) 85.9544000000
14 ( 0 100.0000000000 1 8100.0000000000 ) 64.1532000000
24 ( 0 100.0000000000 1 9100.0000000000 ) 55.8612000000
32 ( 0 1100.0000000000 1 8100.0000000000 ) 52.5820000000
47 ( 0 1600.0000000000 1 8100.0000000000 ) 47.6450000000
55 ( 0 1850.0000000000 1 8100.0000000000 ) 45.3357000000
72 ( 0 1853.4179687500 1 8111.7187500000 ) 45.2005000000
75 ( 0 1833.8867187500 1 8132.2265625000 ) 45.1958000000
76 ( 0 1775.2929687500 1 8193.7500000000 ) 45.1911000000
80 ( 0 1900.2929687500 1 8068.7500000000 ) 45.1620000000
89 ( 0 1923.7304687500 1 8058.9843750000 ) 45.0398000000
98 ( 0 1899.3164062500 1 8083.3984375000 ) 45.0394000000
105 ( 0 1892.4804687500 1 8106.8359375000 ) 44.8919000000
114 ( 0 1923.7304687500 1 8075.5859375000 ) 44.8905000000
137 ( 0 1922.9360580444 1 8076.6168594360 ) 44.8884000000
146 ( 0 1922.1416473389 1 8077.6477813721 ) 44.8863000000
162 ( 0 1922.1003413200 1 8077.7624607086 ) 44.8856000000
169 ( 0 1922.0590353012 1 8077.8771400452 ) 44.8850000000
171 ( 0 1921.9208717346 1 8078.0774116516 ) 44.8844000000
294 ( 0 1921.9208717346 1 8078.0774116516 ) 44.8844000000
} end of run (mesh index limits (+/- 50))
blackbox evaluations : 294
best feasible solution : ( 0 1921.920872 1 8078.077412 ) h=0 f=44.8844
/*-------------------------------------------------------------------*/
/* Example of a problem with categorical variables */
/*-------------------------------------------------------------------*/
/* */
/* . portfolio problem with 3 assets */
/* */
/* . NOMAD is used in library mode */
/* */
/* . the number of variables can be 3,5, or 7, depending on the */
/* number of assets considered in the portfolio */
/* */
/* . variables are of the form (n t0 v0 t1 v1 t2 v2) where n is the */
/* number of assets, ti is the type of an asset, and vi is the */
/* money invested in this asset */
/* */
/* . categorical variables are n and the ti's */
/* */
/* . with a $10,000 budget, the problem consists in minimizing */
/* some measure of the risk and of the revenue */
/* */
/* . two classes are defined: */
/* */
/* 1. My_Evaluator, wich inherits from the NOMAD class Evaluator, */
/* in order to define the problem via the virtual function */
/* eval_x() */
/* */
/* 2. My_Extended_Poll, which inherits from the NOMAD class */
/* Extended_Poll, in order to define the categorical */
/* variables neighborhoods, via the virtual function */
/* construct_extended_points() */
/* */
/* . My_Extended_Poll also defines 3 signatures, for the solutions */
/* with 3, 5, and 7 variables */
/*-------------------------------------------------------------------*/
#include "nomad.hpp"
using namespace std;
using namespace NOMAD;
#define USE_SURROGATE true
/*----------------------------------------*/
/* the problem */
/*----------------------------------------*/
class My_Evaluator : public Multi_Obj_Evaluator {
public:
My_Evaluator ( const Parameters & p ) :
Multi_Obj_Evaluator ( p ) {
}
~My_Evaluator ( void ) {}
bool eval_x ( Eval_Point & x ,
const Double & h_max ,
bool & count_eval ) const;
};
/*--------------------------------------------------*/
/* user class to define categorical neighborhoods */
/*--------------------------------------------------*/
class My_Extended_Poll : public Extended_Poll {
private:
// signatures for 1, 2, and 3 assets:
Signature * _s1 , * _s2 , * _s3;
public:
// constructor:
My_Extended_Poll ( Parameters & );
// destructor:
virtual ~My_Extended_Poll ( void ) { delete _s1; delete _s2; delete _s3; }
// construct the extended poll points:
virtual void construct_extended_points ( const Eval_Point & );
};
/*------------------------------------------*/
/* NOMAD main function */
/*------------------------------------------*/
int main ( int argc , char ** argv ) {
// NOMAD initializations:
begin ( argc , argv );
// display:
Display out ( cout );
out.precision ( DISPLAY_PRECISION_STD );
// check the number of processess:
#ifdef USE_MPI
if ( Slave::get_nb_processes() < 2 ) {
if ( Slave::is_master() )
cerr << "usage: \'mpirun -np p ./categorical\' with p>1"
<< endl;
end();
return EXIT_FAILURE;
}
#endif
try {
// parameters creation:
Parameters p ( out );
if ( USE_SURROGATE )
p.set_HAS_SGTE ( true );
// p.set_ASYNCHRONOUS(false);
p.set_DISPLAY_DEGREE ( NORMAL_DISPLAY );
// p.set_MAX_ITERATIONS (11);
#ifdef USE_MPI
p.set_MULTI_OVERALL_BB_EVAL ( 500 );
#else
p.set_MAX_BB_EVAL(500);
#endif
p.set_DIMENSION (3);
vector<bb_output_type> bbot (4);
bbot[0] = EB; // budget constraint
bbot[1] = EB; // total value >= 1$
bbot[2] = OBJ; // objective
bbot[3] = OBJ; // objective
p.set_BB_OUTPUT_TYPE ( bbot );
// categorical variables:
p.set_BB_INPUT_TYPE ( 0 , CATEGORICAL );
p.set_BB_INPUT_TYPE ( 1 , CATEGORICAL );
Point x0 ( 3 , 0 );
x0[0] = 1; // 1 asset
x0[1] = 0; // of type 0
x0[2] = 1000; // 100$
p.set_X0 ( x0 );
Point lb ( 3 );
Point ub ( 3 );
// Categorical variables 0 and 1 don't need bounds
lb[2] = 0; ub[2] = 10000;
p.set_LOWER_BOUND ( lb );
p.set_UPPER_BOUND ( ub );
// extended poll trigger:
// p.set_EXTENDED_POLL_TRIGGER ( 0.1 , true );
// parameters validation:
p.check();
// custom evaluator creation:
My_Evaluator ev ( p );
// extended poll:
My_Extended_Poll ep ( p );
// algorithm creation and execution:
Mads mads ( p , &ev , &ep , NULL , NULL );
mads.multi_run();
}
catch ( exception & e ) {
if ( Slave::is_master() )
cerr << "\nNOMAD has been interrupted (" << e.what() << ")\n\n";
}
Slave::stop_slaves ( out );
end();
return EXIT_SUCCESS;
}
/*----------------------------------------------------*/
/* eval_x */
/*----------------------------------------------------*/
bool My_Evaluator::eval_x ( Eval_Point & x ,
const Double & h_max ,
bool & count_eval ) const {
// number of assets:
int n = static_cast<int> ( x[0].value() );
// get the asset types and values:
Point v ( 3 , 0.0 );
Double vmin = 10000 , tmp;
for ( int i = 0 ; i < n ; ++i ) {
tmp = v [ static_cast<int> ( x[2*i+1].value() ) ] = x[2*i+2];
if ( tmp < vmin )
vmin = tmp;
}
// constraints (budget and each asset is considered with at least 1$):
Double vt = v[0] + v[1] + v[2];
Double h = vt - 10000;
x.set_bb_output ( 0 , h );
x.set_bb_output ( 1 , 1-vmin );
if ( h <= 0 && vmin >= 1 ) {
// compute the risk and revenue:
Double vt2 = vt.pow2();
Double rev = v[0] * 0.0891 + v[1] * 0.2137 + v[2] * 0.2346;
Double risk = 0.01 * (v[0]/vt).pow2() +
0.05 * (v[1]/vt).pow2() +
0.09 * (v[2]/vt).pow2() +
0.02 * (v[0]*v[1]/vt2) +
0.02 * (v[0]*v[2]/vt2) +
0.10 * (v[1]*v[2]/vt2);
// the objective is taken as a scaled distance
// between (risk,revenue) and (risk_best,rev_best):
Double a = ( risk - 0.01 ) * 100 / 0.08;
Double b = ( rev - 891 ) * 100 / 1455;
x.set_bb_output ( 2 , ( a.pow2() + (100-b).pow2() ).sqrt() );
count_eval = true;
}
else
x.set_bb_output ( 2 , 145 );
Double f = x.get_bb_outputs()[2];
f.round();
f += ( (rand()%2) ? -1.0 : 1.0 ) * ( (rand()%1000) / 1000.0 );
x.set_bb_output ( 3 , f );
// simulation of a surrogate:
if ( USE_SURROGATE && x.get_eval_type() == SGTE ) {
Double f = x.get_bb_outputs()[2];
f.round();
f += ( (rand()%2) ? -1.0 : 1.0 ) * ( (rand()%1000) / 1000.0 );
x.set_bb_output ( 2 , f );
f += ( (rand()%2) ? -1.0 : 1.0 ) * ( (rand()%1000) / 1000.0 );
x.set_bb_output ( 3 , f );
}
return true;
}
/*-----------------------------------------*/
/* constructor: creates the 3 signatures */
/*-----------------------------------------*/
My_Extended_Poll::My_Extended_Poll ( Parameters & p )
: Extended_Poll ( p ) ,
_s1 ( NULL ) ,
_s2 ( NULL ) ,
_s3 ( NULL ) {
// signature for 1 asset:
// ----------------------
vector<bb_input_type> bbit_1 (3);
bbit_1[0] = bbit_1[1] = CATEGORICAL;
bbit_1[2] = CONTINUOUS;
const Point & d0_1 = p.get_initial_mesh_size();
const Point & lb_1 = p.get_lb();
const Point & ub_1 = p.get_ub();
int halton_seed = p.get_halton_seed();
_s1 = new Signature ( 3 ,
bbit_1 ,
d0_1 ,
lb_1 ,
ub_1 ,
p.get_direction_types () ,
p.get_sec_poll_dir_types() ,
halton_seed++ ,
_p.out() );
// signature for 2 assets:
// -----------------------
{
vector<bb_input_type> bbit_2 (5);
Point d0_2 (5);
Point lb_2 (5);
Point ub_2 (5);
// Categorical variables don't need bounds
for ( int i = 0 ; i < 5 ; ++i ) {
bbit_2[i] = bbit_1[1+(i-1)%2];
d0_2 [i] = d0_1 [1+(i-1)%2];
lb_2 [i] = lb_1 [1+(i-1)%2];
ub_2 [i] = ub_1 [1+(i-1)%2];
}
_s2 = new Signature ( 5 ,
bbit_2 ,
d0_2 ,
lb_2 ,
ub_2 ,
p.get_direction_types () ,
p.get_sec_poll_dir_types() ,
halton_seed++ ,
_p.out() );
}
// signature for 3 assets:
// -----------------------
{
vector<bb_input_type> bbit_3 (7);
Point d0_3 (7);
Point lb_3 (7);
Point ub_3 (7);
// Categorical variables don't need bounds