Skip to content

Commit

Permalink
VERSION 3.0.1.dev
Browse files Browse the repository at this point in the history
  • Loading branch information
blankjul committed Apr 29, 2019
1 parent d309099 commit dee4514
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 43 deletions.
8 changes: 4 additions & 4 deletions doc/source/algorithms/genetic_algorithm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"output_type": "stream",
"text": [
"Best solution found: \n",
"X = [0.99318015 0.99805751 0.99841552 0.99154133 0.99398002 0.98832975\n",
" 0.99559111 0.9838085 0.99401594 2.86412984 2.84443849 2.77771475\n",
" 0.99492776]\n",
"F = [-14.34353041]\n"
"X = [0.99478403 0.9946165 0.97894618 0.98044142 0.97800612 0.99734025\n",
" 0.9991285 0.96405249 0.99208299 2.77798425 2.97355975 2.86639229\n",
" 0.97567989]\n",
"F = [-14.27257716]\n"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions doc/source/algorithms/moead.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion doc/source/algorithms/nsga2.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions doc/source/algorithms/rnsga3.ipynb

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions doc/source/algorithms/unsga3.ipynb

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions doc/source/components/crossover.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions doc/source/components/mutation.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions doc/source/components/sampling.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/source/tutorial/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Tuturial
binary_problem.ipynb
discrete_problem.ipynb
tutorial_stopping_criterium.ipynb
repair.ipynb
constraint_handling.ipynb


206 changes: 206 additions & 0 deletions doc/source/tutorial/repair.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/restructuredtext"
},
"source": [
".. _nb_repair:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Repair Operator\n",
"\n",
"The repair operator is mostly problem dependent. Most commonly it is used to make sure the algorithm is only searching in the feasible space. It is applied after the offsprings have been reproduced. In the following, we are using the knapsack problem to demonstrate the repair operator in *pymoo*.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the well-known **Knapsack Problem**. In this problem, a knapsack has to be filled with items without violating the maximum weight constraint. Each item $j$ has a value $b_j \\geq 0$ and a weight $w_j \\geq 0$ where $j \\in \\{1, .., m\\}$. The binary decision vector $z = (z_1, .., z_m)$ defines, if an item is picked or not. The aim is to maximize the profit $g(z)$:\n",
"\n",
"\\begin{eqnarray}\n",
"max & & g(z) \\\\[2mm] \\notag \n",
"\\text{s.t.} & & \\sum_{j=1}^m z_j \\, w_j \\leq Q \\\\[1mm] \\notag \n",
"& & z = (z_1, .., z_m) \\in \\mathbb{B}^m \\\\[1mm] \\notag \n",
"g(z) & = & \\sum_{j=1}^{m} z_j \\, b_j \\\\[2mm] \\notag \n",
"\\end{eqnarray}\n",
"\n",
"Because the constraint $\\sum_{j=1}^m z_j \\, w_j \\leq Q$ is fairly easy to satisfy. Therefore, we make sure before evaluating the objective function, that this constraint is not violated.\n",
"\n",
"\n",
"In this framework, a Repair class has to be defined. problem and the population are given as input. The repaired population has to be returned."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pymoo.model.repair import Repair\n",
"\n",
"class ConsiderMaximumWeightRepair(Repair):\n",
"\n",
" def _do(self, problem, pop, **kwargs):\n",
" \n",
" # maximum capacity for the problem\n",
" Q = problem.C\n",
" \n",
" # the packing plan for the whole population (each row one individual)\n",
" Z = pop.get(\"X\")\n",
" \n",
" # the corresponding weight of each individual\n",
" weights = (Z * problem.W).sum(axis=1)\n",
" \n",
" # now repair each indvidiual i\n",
" for i in range(len(Z)):\n",
" \n",
" # the packing plan for i\n",
" z = Z[i]\n",
" \n",
" # while the maximum capacity violation holds\n",
" while weights[i] > Q:\n",
" \n",
" # randomly select an item currently picked\n",
" item_to_remove = random.choice(np.where(z)[0])\n",
" \n",
" # and remove it\n",
" z[item_to_remove] = False\n",
" \n",
" # adjust the weight\n",
" weights[i] -= problem.W[item_to_remove]\n",
" \n",
" # set the design variables for the population\n",
" pop.set(\"X\", Z)\n",
" return pop"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"------------------\n",
"Optimization with no Repair: \n",
"------------------\n",
"\n",
"==================================================\n",
"n_gen | n_eval | cv (min/avg) | favg | fopt \n",
"==================================================\n",
"1 | 200 | 154.00000 / 505.73500 | - | - \n",
"2 | 400 | 154.00000 / 363.68000 | - | - \n",
"3 | 600 | 58.00000 / 275.94000 | - | - \n",
"4 | 800 | 0.00000 / 197.59500 | -205.00000 | -205.0000000000\n",
"5 | 1000 | 0.00000 / 136.85500 | -219.66667 | -293.0000000000\n",
"6 | 1200 | 0.00000 / 82.15500 | -281.37500 | -436.0000000000\n",
"7 | 1400 | 0.00000 / 38.68000 | -262.23404 | -449.0000000000\n",
"8 | 1600 | 0.00000 / 8.63000 | -253.61017 | -449.0000000000\n",
"9 | 1800 | 0.00000 / 0.00000 | -262.31500 | -472.0000000000\n",
"10 | 2000 | 0.00000 / 0.00000 | -316.07000 | -516.0000000000\n",
"\n",
"------------------\n",
"Optimization with Repair: \n",
"------------------\n",
"\n",
"==================================================\n",
"n_gen | n_eval | cv (min/avg) | favg | fopt \n",
"==================================================\n",
"1 | 200 | 0.00000 / 0.00000 | -129.94500 | -418.0000000000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 | 400 | 0.00000 / 0.00000 | -214.11000 | -418.0000000000\n",
"3 | 600 | 0.00000 / 0.00000 | -268.92500 | -510.0000000000\n",
"4 | 800 | 0.00000 / 0.00000 | -328.63000 | -527.0000000000\n",
"5 | 1000 | 0.00000 / 0.00000 | -378.17500 | -563.0000000000\n",
"6 | 1200 | 0.00000 / 0.00000 | -418.38500 | -581.0000000000\n",
"7 | 1400 | 0.00000 / 0.00000 | -456.14000 | -644.0000000000\n",
"8 | 1600 | 0.00000 / 0.00000 | -489.85000 | -644.0000000000\n",
"9 | 1800 | 0.00000 / 0.00000 | -518.60500 | -677.0000000000\n",
"10 | 2000 | 0.00000 / 0.00000 | -538.90500 | -677.0000000000\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"from pymoo.factory import get_algorithm, get_crossover, get_mutation, get_sampling\n",
"from pymoo.optimize import minimize\n",
"from pymoo.rand import random\n",
"from pymop import create_random_knapsack_problem\n",
"\n",
"\n",
"method = get_algorithm(\"ga\",\n",
" pop_size=200,\n",
" sampling=get_sampling(\"bin_random\"),\n",
" crossover=get_crossover(\"bin_hux\"),\n",
" mutation=get_mutation(\"bin_bitflip\"),\n",
" elimate_duplicates=True)\n",
"\n",
"print(\"\\n------------------\\nOptimization with no Repair: \\n------------------\\n\")\n",
"\n",
"res = minimize(create_random_knapsack_problem(30),\n",
" method,\n",
" termination=('n_gen', 10),\n",
" disp=True)\n",
"\n",
"\n",
"print(\"\\n------------------\\nOptimization with Repair: \\n------------------\\n\")\n",
"\n",
"\n",
"method.repair = ConsiderMaximumWeightRepair()\n",
"\n",
"\n",
"res = minimize(create_random_knapsack_problem(30),\n",
" method,\n",
" termination=('n_gen', 10),\n",
" disp=True)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As it can be seen, the repair operator makes sure no infeasible solution is evaluated. Even though this exapmle seems to be quite easy, the repair operator makes especially sense for more complex constraints where domain specific knowledge is known."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 3 additions & 3 deletions pymoo/model/repair.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Repair:
This class is allows to repair individuals after crossover if necessary.
"""

def do(self, pop, problem, **kwargs):
return self._do(pop, problem, **kwargs)
def do(self, problem, pop, **kwargs):
return self._do(problem, pop, **kwargs)

@abstractmethod
def _do(self, pop, problem, **kwargs):
def _do(self, problem, pop, **kwargs):
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_extension_modules():

__name__ = "pymoo"
__author__ = "Julian Blank"
__version__ = '0.3.0'
__version__ = '0.3.1.dev'
__url__ = "https://github.com/msu-coinlab/pymoo"

kwargs = dict(
Expand Down

0 comments on commit dee4514

Please sign in to comment.