Skip to content
Snippets Groups Projects
Commit 6f7429e1 authored by Miroslav Shaltev's avatar Miroslav Shaltev
Browse files

initial commit using version 3.6.1

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 1367 additions and 0 deletions
doc/logos/Nomad_background.jpg

28.7 KiB

doc/logos/nomadIcon.ico

60.6 KiB

File added
doc/logos/nomad_logo.bmp

34.7 KiB

doc/logos/nomad_logo.jpg

122 KiB

<?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
File added
/*-------------------------------------------------------------------*/
/* 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
0 2234.34 1 2332.11
0 100 1 100
/*-------------------------------------------------------------------*/
/* 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
for ( int i = 0 ; i < 7 ; ++i ) {
bbit_3[i] = bbit_1[1+(i-1)%2];
d0_3 [i] = d0_1 [1+(i-1)%2];
lb_3 [i] = lb_1 [1+(i-1)%2];
ub_3 [i] = ub_1 [1+(i-1)%2];
}
_s3 = new Signature ( 7 ,
bbit_3 ,
d0_3 ,
lb_3 ,
ub_3 ,
p.get_direction_types () ,
p.get_sec_poll_dir_types() ,
halton_seed ,
_p.out() );
}
}
/*--------------------------------------*/
/* construct the extended poll points */
/* (categorical neighborhoods) */
/*--------------------------------------*/
void My_Extended_Poll::construct_extended_points ( const Eval_Point & x ) {
// number of assets:
int n = static_cast<int> ( x[0].value() );
// 1 asset:
// --------
if ( n==1 ) {
int cur_type = static_cast<int> ( x[1].value() );
// this vector contains the types of the other assets:
vector<int> other_types;
switch ( cur_type ) {
case 0:
case 2:
other_types.push_back(1);
break;
default:
other_types.push_back(0);
other_types.push_back(2);
}
// add 1 asset (1 or 2 neighbors):
for ( size_t k = 0 ; k < other_types.size() ; ++k ) {
Point y (5);
y[0] = 2;
y[1] = cur_type;
y[3] = other_types[k];
y[2] = y[4] = x[2]/2.0;
add_extended_poll_point ( y , *_s2 );
}
// change the type of the asset to the other types (1 or 2 neighbors):
for ( size_t k = 0 ; k < other_types.size() ; ++k ) {
Point y = x ;
y[1] = other_types[k];
add_extended_poll_point ( y , *_s1 );
}
}
// 2 assets:
// ---------
else if ( n == 2 ) {
int other_type = static_cast<int> ( (3 - x[1] - x[3]).value() );
// change the type of one asset (2 neighbors):
{
Point y1 = x;
Point y2 = x;
y1[1] = y2[3] = other_type;
add_extended_poll_point ( y1 , *_s2 );
add_extended_poll_point ( y2 , *_s2 );
}
// remove 1 asset (2 neighbors):
{
Point y1(3) , y2(3);
y1[0] = y2[0] = 1;
y1[1] = x[1];
y2[1] = x[3];
y1[2] = y2[2] = x[2]+x[4];
add_extended_poll_point ( y1 , *_s1 );
add_extended_poll_point ( y2 , *_s1 );
}
// add one asset (1 neighbor):
{
Point y(7);
y[0] = 3;
y[1] = x[1];
y[3] = x[3];
y[5] = other_type;
y[2] = y[4] = y[6] = (x[2]+x[4])/3.0;
add_extended_poll_point ( y , *_s3 );
}
}
// 3 assets:
// ---------
else {
// remove one asset (3 neighbors):
Point y1(5);
Point y2(5);
Point y3(5);
y1[0] = y2[0] = y3[0] = 2;
y1[1] = x[1];
y1[3] = x[3];
y2[1] = x[1];
y2[3] = x[5];
y3[1] = x[3];
y3[3] = x[5];
y1[2] = y1[4] = y2[2] = y2[4] = y3[2] = y3[4] = (x[2]+x[4]+x[6]) / 2.0;
add_extended_poll_point ( y1 , *_s2 );
add_extended_poll_point ( y2 , *_s2 );
add_extended_poll_point ( y3 , *_s2 );
}
}
EXE = categorical.exe
EXE_MPI = categorical_MPI.exe
COMPILATOR = g++
COMPILATOR_MPI = mpic++
SUNAME = $(shell uname)
OSS=$(findstring MINGW32,$(SUNAME))
ifneq "$(strip $(OSS))" ""
COMPILATOR_MPI = g++
endif
COMPILATOR_OPTIONS = -g -ansi
COMPILATOR_OPTIONS_MPI = $(COMPILATOR_OPTIONS) -DUSE_MPI
L1 = $(NOMAD_HOME)/lib/nomad.a
L1_MPI = $(NOMAD_HOME)/lib/nomad.MPI.a
LIBS = $(L1) -lm
LIBS_MPI = $(L1_MPI) -lm -lmpi
INCLUDE = -I$(NOMAD_HOME)/src -I.
COMPILE = $(COMPILATOR) $(COMPILATOR_OPTIONS) $(INCLUDE) -c
COMPILE_MPI = $(COMPILATOR_MPI) $(COMPILATOR_OPTIONS_MPI) $(INCLUDE) -c
OBJS = categorical.o
OBJS_MPI = categorical_MPI.o
ifndef NOMAD_HOME
define ECHO_NOMAD
@echo Please set NOMAD_HOME environment variable!
@false
endef
endif
$(EXE): $(L1) $(OBJS)
$(ECHO_NOMAD)
@echo " building the scalar version ..."
@echo " exe file : "$(EXE)
@$(COMPILATOR) -o $(EXE) $(OBJS) $(LIBS) $(COMPILATOR_OPTIONS)
@strip $(EXE)
$(EXE_MPI): $(L1_MPI) $(OBJS_MPI)
$(ECHO_NOMAD)
@echo " building the MPI version ..."
@echo " exe file : "$(EXE_MPI)
@$(COMPILATOR_MPI) -o $(EXE_MPI) $(OBJS_MPI) $(LIBS_MPI) $(COMPILATOR_OPTIONS_MPI)
@strip $(EXE_MPI)
categorical.o: categorical.cpp
$(ECHO_NOMAD)
@$(COMPILE) categorical.cpp
categorical_MPI.o: categorical.cpp
$(ECHO_NOMAD)
@$(COMPILE_MPI) categorical.cpp -o categorical_MPI.o
$(L1) $(L1_MPI): ;
$(ECHO_NOMAD)
mpi: $(EXE_MPI)
all: $(EXE) $(EXE_MPI)
clean: ;
@echo " cleaning obj files"
@rm -f $(OBJS) $(OBJS_MPI)
del: ;
@echo " cleaning trash files"
@rm -f core *~
@echo " cleaning obj files"
@rm -f $(OBJS) $(OBJS_MPI)
@echo " cleaning exe file"
@rm -f $(EXE) $(EXE_MPI)
Warning: {
Model use is disabled for problem with categorical variables.
}
multi-MADS run {
MADS run 1/30 ...... OK [bb eval= 60] [overall bb eval= 60] [# dominant pts= 5] [# new pts= 5] [f1=52.02241823 f2=52.95541823]
MADS run 2/30 ...... OK [bb eval= 66] [overall bb eval= 126] [# dominant pts= 5] [# new pts= 0] [f1=52.02241823 f2=51.05441823]
MADS run 3/30 ...... OK [bb eval= 57] [overall bb eval= 183] [# dominant pts= 6] [# new pts= 1] [f1=52.02241823 f2=51.56141823]
MADS run 4/30 ...... OK [bb eval= 49] [overall bb eval= 232] [# dominant pts= 6] [# new pts= 0] [f1=52.02241823 f2=51.70341823]
MADS run 5/30 ...... OK [bb eval=181] [overall bb eval= 413] [# dominant pts= 5] [# new pts= -1] [f1=44.95823059 f2=43.96223059]
MADS run 6/30 ...... OK [bb eval=203] [overall bb eval= 616] [# dominant pts= 5] [# new pts= 0] [f1=44.90686591 f2=43.90786591]
MADS run 7/30 ...... OK [bb eval=138] [overall bb eval= 754] [# dominant pts= 8] [# new pts= 3] [f1=44.90641676 f2=43.90741676]
MADS run 8/30 ...... OK [bb eval=124] [overall bb eval= 878] [# dominant pts= 8] [# new pts= 0] [f1=44.90640745 f2=43.91240745]
MADS run 9/30 ...... OK [bb eval=180] [overall bb eval= 1058] [# dominant pts= 8] [# new pts= 0] [f1=44.90640744 f2=43.91440744]
MADS run 10/30 ...... OK [bb eval= 96] [overall bb eval= 1154] [# dominant pts= 8] [# new pts= 0] [f1=44.90640744 f2=43.91340744]
MADS run 11/30 ...... OK [bb eval=168] [overall bb eval= 1322] [# dominant pts= 11] [# new pts= 3] [f1=44.90640744 f2=43.97340744]
MADS run 12/30 ...... OK [bb eval=201] [overall bb eval= 1523] [# dominant pts= 10] [# new pts= -1] [f1=44.89376891 f2=43.89876891]
MADS run 13/30 ...... OK [bb eval=204] [overall bb eval= 1727] [# dominant pts= 12] [# new pts= 2] [f1=44.88950881 f2=43.90550881]
MADS run 14/30 ...... OK [bb eval=202] [overall bb eval= 1929] [# dominant pts= 10] [# new pts= -2] [f1=44.88548533 f2=43.90048533]
MADS run 15/30 ...... OK [bb eval=212] [overall bb eval= 2141] [# dominant pts= 10] [# new pts= 0] [f1=44.88519731 f2=43.88619731]
MADS run 16/30 ...... OK [bb eval=209] [overall bb eval= 2350] [# dominant pts= 9] [# new pts= -1] [f1=44.88510307 f2=43.91210307]
MADS run 17/30 ...... OK [bb eval=203] [overall bb eval= 2553] [# dominant pts= 9] [# new pts= 0] [f1=44.88508265 f2=43.90208265]
MADS run 18/30 ...... OK [bb eval=239] [overall bb eval= 2792] [# dominant pts= 9] [# new pts= 0] [f1=44.88508168 f2=43.90108168]
MADS run 19/30 ...... OK [bb eval=222] [overall bb eval= 3014] [# dominant pts= 9] [# new pts= 0] [f1=44.8850816 f2=43.9070816]
MADS run 20/30 ...... OK [bb eval=207] [overall bb eval= 3221] [# dominant pts= 8] [# new pts= -1] [f1=44.8850816 f2=43.9610816]
MADS run 21/30 ...... OK [bb eval=180] [overall bb eval= 3401] [# dominant pts= 6] [# new pts= -2] [f1=44.8850816 f2=43.9010816]
MADS run 22/30 ...... OK [bb eval=174] [overall bb eval= 3575] [# dominant pts= 11] [# new pts= 5] [f1=44.8850816 f2=44.0440816]
MADS run 23/30 ...... OK [bb eval=176] [overall bb eval= 3751] [# dominant pts= 9] [# new pts= -2] [f1=44.8850816 f2=43.9800816]
MADS run 24/30 ...... OK [bb eval=187] [overall bb eval= 3938] [# dominant pts= 8] [# new pts= -1] [f1=44.8850816 f2=44.1020816]
MADS run 25/30 ...... OK [bb eval=166] [overall bb eval= 4104] [# dominant pts= 10] [# new pts= 2] [f1=44.8850816 f2=43.9460816]
MADS run 26/30 ...... OK [bb eval=176] [overall bb eval= 4280] [# dominant pts= 11] [# new pts= 1] [f1=44.8850816 f2=43.9870816]
MADS run 27/30 ...... OK [bb eval= 2] [overall bb eval= 4282] [# dominant pts= 11] [# new pts= 0] [f1=44.8850816 f2=43.9460816]
MADS run 28/30 ...... OK [bb eval= 0] [overall bb eval= 4282] [# dominant pts= 11] [# new pts= 0] [f1=44.8850816 f2=43.9870816]
MADS run 29/30 ...... OK [bb eval= 0] [overall bb eval= 4282] [# dominant pts= 11] [# new pts= 0] [f1=44.8850816 f2=43.9460816]
MADS run 30/30 ...... OK [bb eval= 0] [overall bb eval= 4282] [# dominant pts= 11] [# new pts= 0] [f1=44.8850816 f2=43.9870816]
} end of run (max number of MADS runs)
blackbox evaluations : 4282
number of MADS runs : 30
Pareto front {
44.8849906075 45.5549906075
44.8850816013 45.5000816013
44.8850816013 45.2800816013
44.8850816013 44.2430816013
44.8850816013 43.9800816013
44.8850816013 43.9260816013
44.8850816013 43.9020816013
44.8850816013 43.9010816013
44.8850816075 43.8900816075
44.8850816076 43.8890816076
44.8851973070 43.8861973070
}
number of Pareto points: 11
/*-------------------------------------------------------------------*/
/* 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 */
/*-------------------------------------------------------------------*/
/* . compile the scalar version with 'make' */
/* . compile the parallel version with 'make mpi' */
/*-------------------------------------------------------------------*/
/* . execute the scalar version with './categorical' */
/* . execute the parallel version with 'mpirun -np p ./categorical' */
/* with p > 1 */
/*-------------------------------------------------------------------*/
#include "nomad.hpp"
using namespace std;
using namespace NOMAD;
#define USE_SURROGATE false
/*----------------------------------------*/
/* the problem */
/*----------------------------------------*/
class My_Evaluator : public Evaluator {
public:
My_Evaluator ( const Parameters & p ) :
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_DISPLAY_DEGREE ( FULL_DISPLAY );
p.set_MAX_BB_EVAL ( 200 );
p.set_DIMENSION (3);
vector<bb_output_type> bbot (3);
bbot[0] = EB; // budget constraint
bbot[1] = EB; // total value >= 1$
bbot[2] = OBJ; // objective
p.set_BB_OUTPUT_TYPE ( bbot );
// categorical variables:
p.set_BB_INPUT_TYPE ( 0 , CATEGORICAL );
p.set_BB_INPUT_TYPE ( 1 , CATEGORICAL );
// initial point
Point x0 ( 3 , 0 );
x0[0] = 1; // 1 asset
x0[1] = 0; // of type 0
x0[2] = 100; // 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 );
p.set_DISPLAY_STATS ( "bbe ( sol ) obj" );
// extended poll trigger:
p.set_EXTENDED_POLL_TRIGGER ( 10 , false );
// 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.run();
}
catch ( exception & e ) {
string error = string ( "NOMAD has been interrupted: " ) + e.what();
if ( Slave::is_master() )
cerr << endl << error << endl << endl;
}
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 );
// 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 );
}
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
for ( int i = 0 ; i < 7 ; ++i )
{
bbit_3[i] = bbit_1[1+(i-1)%2];
d0_3 [i] = d0_1 [1+(i-1)%2];
lb_3 [i] = lb_1 [1+(i-1)%2];
ub_3 [i] = ub_1 [1+(i-1)%2];
}
_s3 = new Signature ( 7 ,
bbit_3 ,
d0_3 ,
lb_3 ,
ub_3 ,
p.get_direction_types () ,
p.get_sec_poll_dir_types() ,
halton_seed ,
_p.out() );
}
}
/*--------------------------------------*/
/* construct the extended poll points */
/* (categorical neighborhoods) */
/*--------------------------------------*/
void My_Extended_Poll::construct_extended_points ( const Eval_Point & x ) {
// number of assets:
int n = static_cast<int> ( x[0].value() );
// 1 asset:
// --------
if ( n==1 ) {
int cur_type = static_cast<int> ( x[1].value() );
// this vector contains the types of the other assets:
vector<int> other_types;
switch ( cur_type ) {
case 0:
case 2:
other_types.push_back(1);
break;
default:
other_types.push_back(0);
other_types.push_back(2);
}
// add 1 asset (1 or 2 neighbors):
for ( size_t k = 0 ; k < other_types.size() ; ++k ) {
Point y (5);
y[0] = 2;
y[1] = cur_type;
y[3] = other_types[k];
y[2] = y[4] = x[2]/2.0;
add_extended_poll_point ( y , *_s2 );
}
// change the type of the asset to the other types (1 or 2 neighbors):
for ( size_t k = 0 ; k < other_types.size() ; ++k ) {
Point y = x ;
y[1] = other_types[k];
add_extended_poll_point ( y , *_s1 );
}
}
// 2 assets:
// ---------
else if ( n == 2 ) {
int other_type = static_cast<int> ( (3 - x[1] - x[3]).value() );
// change the type of one asset (2 neighbors):
{
Point y1 = x;
Point y2 = x;
y1[1] = y2[3] = other_type;
add_extended_poll_point ( y1 , *_s2 );
add_extended_poll_point ( y2 , *_s2 );
}
// remove 1 asset (2 neighbors):
{
Point y1(3) , y2(3);
y1[0] = y2[0] = 1;
y1[1] = x[1];
y2[1] = x[3];
y1[2] = y2[2] = x[2]+x[4];
add_extended_poll_point ( y1 , *_s1 );
add_extended_poll_point ( y2 , *_s1 );
}
// add one asset (1 neighbor):
{
Point y(7);
y[0] = 3;
y[1] = x[1];
y[3] = x[3];
y[5] = other_type;
y[2] = y[4] = y[6] = (x[2]+x[4])/3.0;
add_extended_poll_point ( y , *_s3 );
}
}
// 3 assets:
// ---------
else {
// remove one asset (3 neighbors):
Point y1(5);
Point y2(5);
Point y3(5);
y1[0] = y2[0] = y3[0] = 2;
y1[1] = x[1];
y1[3] = x[3];
y2[1] = x[1];
y2[3] = x[5];
y3[1] = x[3];
y3[3] = x[5];
y1[2] = y1[4] = y2[2] = y2[4] = y3[2] = y3[4] = (x[2]+x[4]+x[6]) / 2.0;
add_extended_poll_point ( y1 , *_s2 );
add_extended_poll_point ( y2 , *_s2 );
add_extended_poll_point ( y3 , *_s2 );
}
}
EXE = categorical.exe
EXE_MPI = categorical_MPI.exe
COMPILATOR = g++
COMPILATOR_MPI = mpic++
SUNAME = $(shell uname)
OSS=$(findstring MINGW32,$(SUNAME))
ifneq "$(strip $(OSS))" ""
COMPILATOR_MPI = g++
endif
COMPILATOR_OPTIONS = -O2 -ansi
COMPILATOR_OPTIONS_MPI = $(COMPILATOR_OPTIONS) -DUSE_MPI
L1 = $(NOMAD_HOME)/lib/nomad.a
L1_MPI = $(NOMAD_HOME)/lib/nomad.MPI.a
LIBS = $(L1) -lm
LIBS_MPI = $(L1_MPI) -lm -lmpi
INCLUDE = -I$(NOMAD_HOME)/src -I.
COMPILE = $(COMPILATOR) $(COMPILATOR_OPTIONS) $(INCLUDE) -c
COMPILE_MPI = $(COMPILATOR_MPI) $(COMPILATOR_OPTIONS_MPI) $(INCLUDE) -c
OBJS = categorical.o
OBJS_MPI = categorical_MPI.o
ifndef NOMAD_HOME
define ECHO_NOMAD
@echo Please set NOMAD_HOME environment variable!
@false
endef
endif
$(EXE): $(L1) $(OBJS)
$(ECHO_NOMAD)
@echo " building the scalar version ..."
@echo " exe file : "$(EXE)
@$(COMPILATOR) -o $(EXE) $(OBJS) $(LIBS) $(COMPILATOR_OPTIONS)
@strip $(EXE)
$(EXE_MPI): $(L1_MPI) $(OBJS_MPI)
$(ECHO_NOMAD)
@echo " building the MPI version ..."
@echo " exe file : "$(EXE_MPI)
@$(COMPILATOR_MPI) -o $(EXE_MPI) $(OBJS_MPI) $(LIBS_MPI) $(COMPILATOR_OPTIONS_MPI)
@strip $(EXE_MPI)
categorical.o: categorical.cpp
$(ECHO_NOMAD)
@$(COMPILE) categorical.cpp
categorical_MPI.o: categorical.cpp
$(ECHO_NOMAD)
@$(COMPILE_MPI) categorical.cpp -o categorical_MPI.o
$(L1) $(L1_MPI): ;
$(ECHO_NOMAD)
mpi: $(EXE_MPI)
all: $(EXE) $(EXE_MPI)
clean: ;
@echo " cleaning obj files"
@rm -f $(OBJS) $(OBJS_MPI)
del: ;
@echo " cleaning trash files"
@rm -f core *~
@echo " cleaning obj files"
@rm -f $(OBJS) $(OBJS_MPI)
@echo " cleaning exe file"
@rm -f $(EXE) $(EXE_MPI)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment