Comments (17)
It works now!
I have implemented the Fonseca and Fleming function for n=2
test-wise:
import pygmo as pg
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
class my_udp:
def fitness(self, x):
f1 = 1-np.exp(-((x[0]-1/np.sqrt(1))**2+(x[1]-1/np.sqrt(2))**2))
f2 = 1-np.exp(-((x[0]+1/np.sqrt(1))**2+(x[1]+1/np.sqrt(2))**2))
return [f1, f2]
def get_nobj(self):
return 2
def get_bounds(self):
return ([-4]*2, [4]*2)
pro = pg.problem(my_udp())
pop = pg.population(pro, size=100)
algo = pg.algorithm(pg.nsga2(gen=1000))
algo.set_verbosity(100)
pop = algo.evolve(pop)
fits, vectors = pop.get_f(), pop.get_x()
print(fits, vectors)
df = pd.DataFrame(fits)
ax = df.plot(kind='scatter', x=0, y=1, grid=True)
ax.set_xlabel('f1(x)')
ax.set_ylabel('f2(x)')
plt.show()
Result:
Nevertheless, I think some simple examples within the documentation would help new users getting started.
In case you do write a new tutorial on how to write a moo problem and solve it .... we do welcome PR
If you want, I can adapt my example to be more generic (variable n
) and add it somewhere where it can be found whether in the docs or in the examples.
Thanks already for your help!
from pagmo2.
The error message is, in this case, misleading it should be:
Constraints defined in<class '__main__.constr_ex_problem'> instance. NSGA-II cannot deal with them.
Algorithms that have constarints (also linear) cannot be solved by nsga-II. Box constraints (i.e. the bounds) though can.
from pagmo2.
I am writing quickly from mobile .... sorry for the brevity.
I suggest you to look into the docs of the class problem in the python version. There you can see all the optional methods that empower you to define all sorts of problems...
In your case it's the method 'get_nobj(self)' that needs to be implemented and needs to return the number of objectives ....
In case you do write a new tutorial on how to write a moo problem and solve it .... we do welcome PR
from pagmo2.
Looking at the algorithm list, only improved harmony search seems to be able to solve constr. MO problems:
https://esa.github.io/pagmo2/docs/algorithm_list.html
Note that IHS is not avaialble yet in any released PaGMO/PyGMO version (it will be in the next version, 2.7).
Another possibility is to unconstrain your problem using the unconstrain meta problem:
https://esa.github.io/pagmo2/docs/python/problems/py_problems.html#pygmo.unconstrain
This will transform your constrained problem into an unconstrained one, which can then be solved with NSGA2. Of course, whether this approach is sound depends highly on your problem.
from pagmo2.
If you pass me some snippets I could also integrate in in the docs via PR..
from pagmo2.
Or is it as simple as returning a multi-dimensional array in the fitness function?
def fitness(self, x):
return [x*x, 1/x]
But where can I pass the senses (min/max) or weights, etc.. Some information would be nice ;-)
from pagmo2.
I am writing quickly from mobile .... sorry for the brevity.
I suggest you to look into the docs of the class problem in the python version. There you can see all the optional methods that empower you to define all sorts of problems...In your case it's the method 'get_nobj(self)' that needs to be implemented and needs to return the number of objectives ....
In case you do write a new tutorial on how to write a moo problem and solve it .... we do welcome PR
Thanks for your quick answer! I'll give it a try and post my progress here.
from pagmo2.
Sure, a tutorial on coding and solving the Fonseca and Fleming UDP would be helpful. Feel free to issue a PR. Do not import pandas though as we do not force it as a third party dep.
from pagmo2.
Sure, a tutorial on coding and solving the Fonseca and Fleming UDP would be helpful. Feel free to issue a PR. Do not import pandas though as we do not force it as a third party dep.
I have now tried to model the "Constr.-Ex problem" since it also contains some (linear) constraints and would be a bit more illustrative than the example from above:
import pygmo as pg
class constr_ex_problem:
"""
The Constr-Ex problem.
See:
https://en.wikipedia.org/wiki/
Test_functions_for_optimization
#Test_functions_for_multi-objective_optimization
"""
def fitness(self, x):
f1 = x[0]
f2 = (1+x[1])/x[0]
ci1 = -x[1]-9*x[0]+6
ci2 = x[1]-9*x[0]+1
return [f1, f2, ci1, ci2]
def get_nobj(self):
return 2
def get_bounds(self):
return ([0.1, 0], [1, 5])
def get_nic(self):
return 2
prob = pg.problem(constr_ex_problem())
pop = pg.population(prob, size=500)
# https://github.com/esa/pagmo2/blob/master/include/pagmo/algorithms/
algo = pg.algorithm(pg.nsga2(gen=250))
algo.set_verbosity(100)
pop = algo.evolve(pop)
fits, vectors = pop.get_f(), pop.get_x()
print(fits)
But i get an error due to non-linear constraints:
ValueError:
function: evolve
where: /home/xyz/.anaconda3/include/pagmo/algorithms/nsga2.hpp, 144
what: Non linear constraints detected in <class '__main__.constr_ex_problem'> instance. NSGA-II cannot deal with them.
From my point of view the constraints are linear and only one of the objectives is non-linear.
Am I missing something here?
Thanks in advance!
from pagmo2.
The error message is, in this case, misleading it should be:
Constraints defined in<class 'main.constr_ex_problem'> instance. NSGA-II cannot deal with them.Algorithms that have constarints (also linear) cannot be solved by nsga-II. Box constraints (i.e. the bounds) though can.
Thanks for your answer. Is there any algorithm implemented that can solve constrained multi-objective problems?
from pagmo2.
Looking at the algorithm list, only improved harmony search seems to be able to solve constr. MO problems:
https://esa.github.io/pagmo2/docs/algorithm_list.html
Note that IHS is not avaialble yet in any released PaGMO/PyGMO version (it will be in the next version, 2.7).
Another possibility is to unconstrain your problem using the unconstrain meta problem:
https://esa.github.io/pagmo2/docs/python/problems/py_problems.html#pygmo.unconstrain
This will transform your constrained problem into an unconstrained one, which can then be solved with NSGA2. Of course, whether this approach is sound depends highly on your problem.
@ckaldemeyer
Thanks for your answer and explanation! I'll check it and then provide a suitable example via PR
from pagmo2.
Where exactly in the documentation can I place the example?
My first idea would be here within the section Coding your own problem (UDP)
as Coding a User Defined Problem with multiple objectives
.
I have created a simple example using an archipelago with three islands:
"""Example that illustrates multi-objective optimization using pygmo."""
import pygmo as pg
import numpy as np
from multiprocessing import Process, freeze_support
class FonsecaFleming():
"""
User defined problem: Fonseca and Fleming function.
See: https://en.wikipedia.org/wiki/Test_functions_for_optimization
"""
def __init__(self, n):
"""Pass dimensions to constructor."""
self.n = n
def fitness(self, x):
"""Define objectives."""
f1 = 1-np.exp(-sum([(x[n]-1/np.sqrt(n))**2 for n in range(1, self.n)]))
f2 = 1-np.exp(-sum([(x[n]+1/np.sqrt(n))**2 for n in range(1, self.n)]))
return [f1, f2]
def get_nobj(self):
"""Return number of objectives."""
return 2
def get_bounds(self):
"""Return bounds of decision variables."""
return ([-4]*self.n, [4]*self.n)
def get_name(self):
"""Return function name."""
return "Fonseca and Fleming function"
def optimize(popsize=100, generations=10, islands=3, evolutions=10,
prob=pg.problem(FonsecaFleming(5))):
"""Start optimization process."""
# set up algorithm and optimize
algo = pg.algorithm(pg.nsga2(gen=generations))
archi = pg.archipelago(islands, algo=algo, prob=prob, pop_size=popsize)
# evolution
fits_log, vectors_log = [], []
for e in range(0, evolutions):
# evolve islands
archi.evolve(e)
archi.wait()
# extract data
vectors = [isl.get_population().get_x() for isl in archi]
vectors_log.append(vectors)
fits = [isl.get_population().get_f() for isl in archi]
fits_log.append(fits)
return [fits_log, vectors_log]
if __name__ == '__main__':
freeze_support()
Process(target=optimize).start()
If the code is fine for you, I could provide a PR. Or I just start with the PR and we keep on discussing there. As you like it..
from pagmo2.
Before opening the PR:
- I think the place Coding your own problem (UDP) is OK
- Your example, though, puts too much emphasis on the solution method. Since the tutorial is on coding the UDP, not solving it, I would just show the solution without the use of multiprocessing etc. just call
pop=algo.evolve(pop)
from pagmo2.
Before opening the PR:
I think the place Coding your own problem (UDP) is OK Your example, though, puts too much emphasis on the solution method. Since the tutorial is on coding the UDP, not solving it, I would just show the solution without the use of multiprocessing etc. just call pop=algo.evolve(pop)
Allright. I'll then provide a PR!
from pagmo2.
Cool, thanks!
from pagmo2.
For PR, see: #191
from pagmo2.
Hi @ckaldemeyer , thanks for your great example. Could you elaborate on the reasons why you evolve your archipelago multiple times (10x) in your multiprocessing example ? Knowing that the nsga algorithm is already working on 10 generations ? Is it a requirement that the number of evolutions should match the number of generations ?
from pagmo2.
Related Issues (20)
- Installation problems HOT 1
- Installl erro HOT 1
- install problem HOT 6
- [BUG] mhaco asserts if initial population does not verify constraints
- MinGW installation problem HOT 1
- how to run pagmo2 in parallel cluster? HOT 1
- [BUG] redundant code in moead.cpp HOT 2
- [BUG] Could NOT find pagmo_IPOPT (missing: PAGMO_IPOPT_INCLUDE_DIR) HOT 1
- [BUG] No 2.19.0 tag HOT 2
- [BUG] moead_gen is not documented HOT 2
- [FEATURE] PAGMO_VERSION HOT 2
- Scaling of design variables and
- How to optimize with algorithms::ipopt? HOT 2
- [BUG] Maximize function with Ipopt: incorrect population returned HOT 2
- [BUG] Compilation problem with IPOPT HOT 7
- Question: Use of Hessian with Ipopt
- pagmo-devel 2.19 build 3 not found by meson build system HOT 5
- Passing arguments in a user defined problem HOT 6
- Uninitialized variables may be used in cec2014 problem
- MPI Island class for cluster computing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pagmo2.