Professional Documents
Culture Documents
openGA Manual
openGA Manual
net/publication/320944793
CITATIONS READS
0 1,267
1 author:
Arash Mohammadi
Deakin University
21 PUBLICATIONS 135 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Arash Mohammadi on 31 October 2018.
Arash Mohammadi, Institute for Intelligent Systems Research and Innovation (IISRI)
arash.m at research.deakin.edu.au
Introduction
Motivation and aims
Despite currently there are some available C++ based GA libraries, the following
raised my motivation to write this new GA library
• This code is aiming for GA problems when cost functions are expensive
in terms of calculation. A thread pool is used to increase the speed of GA
for chromosome evaluations.
openGA Page 1 of 21
• Separation of middle computation and final cost. Unfortunately, in many
applications, the chromosomes need to run heavy simulations and they
have to store valuable additional information into the cost beyond the
final cost(s). This library, opens the hand of the designers to write their
customized middle costs. Then, converting them to the final cost at the
last stage.
• Rejection of chromosomes after computation. Another problem of many
GA libraries including MATLAB is that they have a nonlinear condition,
while the condition is not known unless heavy computation is performed.
This computation can be no longer used for evaluation. In this GA library,
the user code is able to reject a chromosome even after its heavy evaluation
computation. In another term, the nonlinear condition can be mixed with
evaluation.
• Flexible crossover and mutation. One of the problem of many GA libraries
including MATLAB is to perform naive crossover and mutation. In this
library, performing crossover and mutation is totally up to the opinion of
the user.
openGA Page 2 of 21
Requirements
In the recent edition of openGA, the standard C++ libraries are sufficient and
there is no dependency to any external library. Therefore the users can run
openGA straightaway.
Settings
Optimization mode
The main setting is related to the problem mode which provides the following
options:
Multithreading
Multithreading can improve or degrade the performance of optimization. It
should be noted that threads impose extra overheads. The number of threads
can be adjusted via N threads. By default, this parameter is approximately
equal to the supported hardware concurrent threads. In the multithreading
mode, by default, each thread is given a chromosome to evaluate when they are
free. If the evaluation process is very fast, it is more efficient that the thread
responsibilities are divided at the beginning. This adjustment is possible by
setting dynamic threading to false.
openGA Page 3 of 21
Type Default
SO
IGA
MO
problem mode X X X enum class SOGA
multi threading X - X bool true
dynamic threading X - X bool true
N threads X - X int CPU cores
verbose X X X bool false
population X X X uint 50
generation max X X X int 100
calculate SO total fitness X - - function nullptr
calculate IGA total fitness - X - function nullptr
calculate MO objectives - - X function nullptr
distribution - - X function nullptr
objective reductions
init genes X X X function nullptr
eval genes X - X function nullptr
eval genes IGA - X - function nullptr
crossover X X X function nullptr
mutate X X X function nullptr
set shrink scale X X X function nullptr
SO report generation X X - function nullptr
MO report generation - - X function nullptr
custom refresh X - X function nullptr
elite count X X - int 5
crossover fraction X X X double 0.7
mutation fraction X X X double 0.3
idle delay us X X X long 1000
tol stall average X X - double 1e-4
average stall max X X - int 10
tol stall best X X - double 1e-6
best stall max X X - int 10
reference vector divisions - - X uint auto
enable reference vectors - - X bool true
openGA Page 4 of 21
Stop reason
Genetic Algorithm may stop because of one of the following reasons:
Multi-Objective Optimization
In multi-objective GA, each evaluation does not lead into only a single cost
but multiple objectives to be minimized. Therefore, the output of optimization
is not a single chromosome as the best solution, but a set of nondominated
solutions called pareto-optimal solutions.
The applied multi-objective GA is based on NSGA-III proposed by [Deb and Jain, 2014]
[Jain and Deb, 2014].
openGA Page 5 of 21
• It is assumed, IGA is involved in a heavy computation via eval genes IGA.
This function has access to the previous evaluated genes in the same
generation and it can make decision based on them. For example if a
solution is too close to the available solutions, it can be rejected. This
function is no called to evaluate the middle cost of the generation elites
again.
• Sum of these three terms have to be exactly equal to 1.0 : crossover fraction,
mutation fraction and elit fraction=elite count/population.
openGA Assist
OpenGA assist is a web-based user interface to produce an automatic code based
on the given information from the client. It generates a complete source code for
each change in the given settings. This helps the programmer by producing a
base code. However, the given code is not necessarily correct. Hence, this code
should be reviewed and edited by the programmer. For example, the generated
code handles every genetic input as floating point. If any gene is given as string,
the crossover and mutation process will face with a compiler error. Therefore,
the clients should edit these operators according to their need. If the boundary
of a gene is chosen as 10 instead of 10.0 there might be some compiler warnings
for comparing floating point numbers with an integer. Whether the final code
should use multi-threading or not is guessed according to the overhead of the
solution computation. To run assist in your browser, open index.html from the
assist folder.
openGA Page 6 of 21
Figure 1: openGA assist user interface
License
This library is free and distributed under Mozilla Public License Version 2.0.
Contact author
Any suggestion, recommendation, bug report and question related to this library
is highly welcome. I may be also interested in involving in bigger projects. I
am Arash Mohammadi and you can contact me via email
arash.m at research.deakin.edu.au
openGA Page 7 of 21
If you have found this library useful for your work, please cite the following
paper [Mohammadi et al., 2017]
Bibtex:
@inproceedings { mohammadi2017openga ,
t i t l e ={openGA , a C++ G e n e t i c A l g o r i t h m l i b r a r y } ,
a u t h o r={Mohammadi , Arash and Houshyar Asadi , Shady Mohamed and N e l s o n , Kyle and
Nahavandi , S a e i d } ,
b o o k t i t l e ={Systems , Man , and C y b e r n e t i c s (SMC) , 2017 IEEE I n t e r n a t i o n a l C o n f e r e n c e
on } ,
p a g e s ={002051 − −002056} ,
y e a r ={2017} ,
o r g a n i z a t i o n ={IEEE}
}
openGA Page 8 of 21
Sample codes
Single objective GA
Average cost
200 Best cost
Cost
100
0
0 2 4 6 8 10 12 14 16 18 20
Generation step
#include <string>
#include <iostream>
#include <fstream>
#include "openga.hpp"
struct MySolution
{
double x;
double y;
struct MyMiddleCost
{
// This is where the results of simulation
// is stored but not yet finalized.
double cost distance2;
openGA Page 9 of 21
double cost sqsin;
};
MySolution mutate(
const MySolution& X base,
const std::function<double(void)> &rnd01,
double shrink scale)
{
MySolution X new;
bool in range x,in range y;
do{
X new=X base;
X new.x+=0.2∗(rnd01()−rnd01())∗shrink scale;
X new.y+=0.2∗(rnd01()−rnd01())∗shrink scale;
in range x= (X new.x>=−10.0 && X new.x<10.0);
openGA Page 10 of 21
in range y= (X new.y>=−10.0 && X new.y<10.0);
} while(!in range x | | !in range y);
return X new;
}
MySolution crossover(
const MySolution& X1,
const MySolution& X2,
const std::function<double(void)> &rnd01)
{
MySolution X new;
double r;
r=rnd01();
X new.x=r∗X1.x+(1.0−r)∗X2.x;
r=rnd01();
X new.y=r∗X1.y+(1.0−r)∗X2.y;
return X new;
}
output file
<<generation number<<"\t"
<<best genes.x<<"\t"
<<best genes.y<<"\t"
<<last generation.average cost<<"\t"
openGA Page 11 of 21
<<last generation.best total cost<<"\n";
}
int main()
{
output file.open("./bin/result so1.txt");
output file<<"step"<<"\t"<<"x best"<<"\t"<<"y best"<<"\t"<<"
cost avg"<<"\t"<<"cost best"<<"\n";
EA::Chronometer timer;
timer.tic();
GA Type ga obj;
ga obj.problem mode= EA::GA MODE::SOGA;
ga obj.multi threading=true;
ga obj.idle delay us=1; // switch between threads quickly
ga obj.verbose=false;
ga obj.population=20;
ga obj.generation max=1000;
ga obj.calculate SO total fitness=calculate SO total fitness;
ga obj.init genes= init genes;
ga obj.eval solution= eval solution;
ga obj.mutate= mutate;
ga obj.crossover= crossover;
ga obj.SO report generation= SO report generation;
ga obj.best stall max=10;
ga obj.elite count=10;
ga obj.crossover fraction=0.7;
ga obj.mutation rate=0.4;
ga obj.solve();
output file.close();
return 0;
}
Multi-objective GA
#include <string>
#include <iostream>
#include <fstream>
#include "openga.hpp"
openGA Page 12 of 21
10
6
cost 2
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6
cost 1
struct MySolution
{
double x;
double y;
struct MyMiddleCost
{
// This is where the results of simulation
// is stored but not yet finalized.
double cost A;
double cost B;
};
openGA Page 13 of 21
typedef EA::Genetic<MySolution,MyMiddleCost> GA Type;
typedef EA::GenerationType<MySolution,MyMiddleCost> Generation Type;
MySolution mutate(
const MySolution& X base,
const std::function<double(void)> &rnd01,
double shrink scale)
{
MySolution X new;
bool in range x,in range y;
do{
X new=X base;
X new.x+=0.2∗(rnd01()−rnd01())∗shrink scale;
X new.y+=0.2∗(rnd01()−rnd01())∗shrink scale;
in range x= (X new.x>=0.0 && X new.x<10.0);
in range y= (X new.y>=0.0 && X new.y<10.0);
} while(!in range x | | !in range y);
return X new;
}
MySolution crossover(
const MySolution& X1,
const MySolution& X2,
const std::function<double(void)> &rnd01)
{
MySolution X new;
double r;
r=rnd01();
X new.x=r∗X1.x+(1.0−r)∗X2.x;
openGA Page 14 of 21
r=rnd01();
X new.y=r∗X1.y+(1.0−r)∗X2.y;
return X new;
}
openGA Page 15 of 21
}
output file.close();
}
int main()
{
EA::Chronometer timer;
timer.tic();
GA Type ga obj;
ga obj.problem mode= EA::GA MODE::NSGA III;
ga obj.multi threading=true;
ga obj.idle delay us=1; // switch between threads quickly
ga obj.verbose=false;
ga obj.population=40;
ga obj.generation max=100;
ga obj.calculate MO objectives= calculate MO objectives;
ga obj.init genes=init genes;
ga obj.eval solution=eval solution;
ga obj.mutate=mutate;
ga obj.crossover=crossover;
ga obj.MO report generation=MO report generation;
ga obj.crossover fraction=0.7;
ga obj.mutation rate=0.4;
ga obj.solve();
Interactive GA
openGA Page 16 of 21
#include <string>
#include "openga.hpp"
#include "gui.hpp"
#include <fstream>
struct MySolution
{
double R,G,B;
struct MyMiddleCost
{
double R,G,B;
double cost user score;
};
openGA Page 17 of 21
MySolution mutate(
const MySolution& X base,
const std::function<double(void)> &rnd01,
double shrink scale)
{
MySolution X new;
(void) shrink scale;
bool in range R,in range G,in range B;
do{
X new=X base;
X new.R+=100∗(rnd01()−rnd01());
X new.G+=100∗(rnd01()−rnd01());
X new.B+=100∗(rnd01()−rnd01());
in range R= (X new.R>=0.0 && X new.R<255.0);
in range G= (X new.G>=0.0 && X new.G<255.0);
in range B= (X new.B>=0.0 && X new.B<255.0);
} while(!in range R | | !in range G | | !in range B);
return X new;
}
MySolution crossover(
const MySolution& X1,
const MySolution& X2,
const std::function<double(void)> &rnd01)
{
MySolution X new;
double r;
r=rnd01();
X new.R=r∗X1.R+(1.0−r)∗X2.R;
r=rnd01();
X new.G=r∗X1.G+(1.0−r)∗X2.G;
r=rnd01();
X new.B=r∗X1.B+(1.0−r)∗X2.B;
return X new;
}
openGA Page 18 of 21
refresh gui();
std::cout<<"How much do you like this ("<<X.genes.to string()<<")
blue color (0−100%)? ";
std::cin>>X.middle costs.cost user score;
X.total cost=100.0−X.middle costs.cost user score;
}
}
output file
<<generation number<<"\t"
<<best genes.to string()<<"\t"
<<100.0−last generation.average cost<<"\t"
<<100.0−last generation.best total cost<<"\n";
}
int main()
{
output file.open("./bin/result iga1.txt");
output file<<"step"<<"\t"<<"color best"<<"\t"<<"cost avg"<<"\t"<<"
cost best"<<"\n";
init gui();
GA Type ga obj;
ga obj.problem mode= EA::GA MODE::IGA;
ga obj.verbose=false;
ga obj.population=15;
ga obj.generation max=20;
ga obj.calculate IGA total fitness=calculate IGA total fitness;
ga obj.init genes= init genes;
ga obj.eval solution IGA= eval solution IGA;
ga obj.mutate= mutate;
ga obj.crossover= crossover;
ga obj.SO report generation= SO report generation;
ga obj.elite count=3;
openGA Page 19 of 21
double non elit fraction=1−double(ga obj.elite count)/double(ga obj
.population);
ga obj.crossover fraction=non elit fraction;
ga obj.mutation rate=0.1;
ga obj.solve();
output file.close();
return 0;
}
References
[Dalvandi et al., 2010] Dalvandi, A., Behbahani, P. A., and DiPaola, S. (2010).
Exploring persian rug design using a computational evolutionary approach.
In EVA.
[Deb and Jain, 2014] Deb, K. and Jain, H. (2014). An evolutionary many-
objective optimization algorithm using reference-point-based nondominated
sorting approach, part i: Solving problems with box constraints. IEEE Trans.
Evolutionary Computation, 18(4):577–601.
[Jain and Deb, 2014] Jain, H. and Deb, K. (2014). An evolutionary many-
objective optimization algorithm using reference-point based nondominated
sorting approach, part ii: Handling constraints and extending to an adaptive
approach. IEEE Trans. Evolutionary Computation, 18(4):602–622.
[Kim and Cho, 2000] Kim, H.-S. and Cho, S.-B. (2000). Application of interac-
tive genetic algorithm to fashion design. Engineering applications of artificial
intelligence, 13(6):635–644.
[Lewis, 2008] Lewis, M. (2008). Evolutionary visual art and design. In The art
of artificial evolution, pages 3–37. Springer.
[Mohammadi et al., 2017] Mohammadi, A., Houshyar Asadi, S. M., Nelson, K.,
and Nahavandi, S. (2017). openga, a c++ genetic algorithm library. In
Systems, Man, and Cybernetics (SMC), 2017 IEEE International Conference
on, pages 002051–002056. IEEE.
[Serag et al., 2008] Serag, A., Ono, S., and Nakayama, S. (2008). Using interac-
tive evolutionary computation to generate creative building designs. Artificial
life and Robotics, 13(1):246–250.
openGA Page 20 of 21
[Takagi, 2001] Takagi, H. (2001). Interactive evolutionary computation: Fusion
of the capabilities of ec optimization and human evaluation. Proceedings of
the IEEE, 89(9):1275–1296.
[Tokui et al., 2000] Tokui, N., Iba, H., et al. (2000). Music composition with
interactive evolutionary computation. In Proceedings of the 3rd international
conference on generative art, volume 17, pages 215–226.
openGA Page 21 of 21