Skip to content

Commit 96d23b1

Browse files
committed
update visualization
1 parent ec251b6 commit 96d23b1

39 files changed

+1838
-6457
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6+
# vim
7+
.vim/*
68
# vscode
79
.vscode/*
810

.vim/coc-settings.json

-5
This file was deleted.

examples/knapsack/knapsack.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from geneticpython.core.individual import BinaryIndividual
88
from geneticpython import Population, GAEngine
99
from geneticpython.core.operators import RouletteWheelSelection, UniformCrossover, FlipBitMutation, RouletteWheelReplacement
10+
from geneticpython.utils.visualization import plot_single_objective_history
1011

1112
Item = namedtuple("Item", ['index', 'value', 'weight'])
1213

@@ -47,9 +48,7 @@ def read_input():
4748
selection_size=100,
4849
crossover=crossover,
4950
mutation=mutation,
50-
replacement=replacement,
51-
max_iter=1000)
52-
51+
replacement=replacement)
5352

5453
@engine.maximize_objective
5554
def fitness(indv):
@@ -69,8 +68,8 @@ def fitness(indv):
6968

7069

7170
# engine.create_seed(seed)
72-
history = engine.run()
73-
print(history.history)
71+
history = engine.run(generations=1000)
7472
ans = engine.get_best_indv()
7573
print(ans)
7674
# print(engine.population)
75+
plot_single_objective_history({'geneticpython': history})

examples/wusn/multi_hop_nsgaii.py

+25-54
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
# Created by ngocjr7 on [14-06-2020 21:22:52]
88
"""
99
from __future__ import absolute_import
10+
11+
import sys
12+
import os
13+
14+
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
15+
sys.path.append(os.path.join(WORKING_DIR, '../../../geneticpython'))
16+
1017
import time
1118
import json
1219
import random
@@ -21,16 +28,11 @@
2128
from utils import WusnInput
2229
from parameters import *
2330

24-
from geneticpython.operators import TournamentSelection, SBXCrossover, PolynomialMutation
31+
from geneticpython.core.operators import TournamentSelection, SBXCrossover, PolynomialMutation
2532
from geneticpython import Population
26-
from geneticpython.individual import NetworkRandomKeys
33+
from geneticpython.core.individual import NetworkRandomKeys
2734
from geneticpython.engines import NSGAIIEngine
28-
29-
import sys
30-
import os
31-
32-
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
33-
sys.path.append(os.path.join(WORKING_DIR, '../../../geneticpython'))
35+
from geneticpython.utils.visualization import save_history_as_gif
3436

3537

3638
class MultiHopIndividual(NetworkRandomKeys):
@@ -78,7 +80,7 @@ def init_bias_genes(length, n_relays_edges, rand: Random = Random()):
7880
# break
7981
# return
8082

81-
population = Population(indv_temp, POPULATION_SIZE)
83+
population = Population(indv_temp, MH_POP_SIZE)
8284

8385
@population.register_initialization
8486
def init_population(rand: Random = Random()):
@@ -97,20 +99,19 @@ def init_population(rand: Random = Random()):
9799
ret.append(new_indv)
98100
return ret
99101

100-
selection = TournamentSelection(tournament_size=TOURNAMENT_SIZE)
102+
selection = TournamentSelection(tournament_size=MH_TOURNAMENT_SIZE)
101103
crossover = SBXCrossover(
102-
pc=CROSSOVER_PROB, distribution_index=CROSSOVER_DISTRIBUTION_INDEX)
104+
pc=MH_CRO_PROB, distribution_index=MH_CRO_DI)
103105
mutation = PolynomialMutation(
104-
pm=MUTATION_PROB, distribution_index=MUTATION_DISTRIBUTION_INDEX)
106+
pm=MH_MUT_PROB, distribution_index=MH_MUT_DI)
105107

106108
engine = NSGAIIEngine(population, selection=selection,
107109
crossover=crossover,
108110
mutation=mutation,
109-
selection_size=SELECTION_SIZE,
110-
max_iter=MAX_ITER,
111-
random_state=SEED)
111+
selection_size=MH_SLT_SIZE,
112+
random_state=MH_SEED)
112113

113-
@engine.register_objective
114+
@engine.minimize_objective
114115
def objective1(indv):
115116
network = indv.decode()
116117
if network.is_valid:
@@ -120,7 +121,7 @@ def objective1(indv):
120121

121122
best_mr = defaultdict(lambda: float('inf'))
122123

123-
@engine.register_objective
124+
@engine.minimize_objective
124125
def objective2(indv):
125126
nonlocal best_mr
126127
network = indv.decode()
@@ -132,43 +133,7 @@ def objective2(indv):
132133
else:
133134
return float('inf')
134135

135-
f1_max = None
136-
f2_max = None
137-
if visualization:
138-
@engine.register_reporter
139-
def report(gen):
140-
if gen % 5 == 0 or gen < 20:
141-
nonlocal f1_max, f2_max
142-
solutions = engine.get_all_solutions()
143-
# print(solutions)
144-
f1_arr = [solution.objectives[0]
145-
for solution in solutions if solution.objectives[0] != float('inf')]
146-
f2_arr = [solution.objectives[1]
147-
for solution in solutions if solution.objectives[1] != float('inf')]
148-
149-
if f1_max:
150-
f1_arr.append(f1_max)
151-
if f2_max:
152-
f2_arr.append(f2_max)
153-
if len(f1_arr) > 0:
154-
f1_max = max(f1_arr)
155-
f1_max_temp = f1_max * 1.05
156-
else:
157-
f1_max_temp = None
158-
if len(f2_arr) > 0:
159-
f2_max = max(f2_arr)
160-
f2_max_temp = f2_max * 1.05
161-
else:
162-
f2_max_temp = None
163-
164-
visualize_solutions(solutions,
165-
'results/multi_hop/{}/solutions-gen-{}.png'.format(
166-
basename, (3 - len(str(gen))) * '0' + str(gen)),
167-
title='solutions-gen-{}'.format(
168-
(3 - len(str(gen))) * '0' + str(gen)),
169-
show=False, f1_max=f1_max_temp, f2_max=f2_max_temp)
170-
171-
engine.run()
136+
history = engine.run(generations=MH_GENS)
172137

173138
pareto_front = engine.get_pareto_front()
174139
solutions = engine.get_all_solutions()
@@ -183,6 +148,12 @@ def report(gen):
183148
save_results(pareto_front, solutions, best_mr,
184149
out_dir, visualization=visualization)
185150

151+
152+
save_history_as_gif(history,
153+
title="NSGAII",
154+
objective_name=['relays', 'energy'],
155+
gen_filter=lambda x : (x % 5 == 0),
156+
out_dir=out_dir)
186157

187158
if __name__ == '__main__':
188159
solve('data/medium/multi_hop/uu-dem1_r25_1_40.json', visualization=True)

examples/wusn/networks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
99
sys.path.append(os.path.join(WORKING_DIR, '../../../geneticpython'))
1010

11-
from geneticpython.individual import Network
11+
from geneticpython.core.individual import Network
1212
from collections import deque
1313
from utils.input import WusnInput, WusnConstants
1414
from utils.point import distance

examples/wusn/parameters.py

+71-17
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,80 @@
88

99
import os
1010

11-
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
12-
13-
SEED = 42
14-
15-
POPULATION_SIZE = 10
16-
17-
SELECTION_SIZE = 10
18-
19-
MAX_ITER = 2
20-
21-
TOURNAMENT_SIZE = 5
11+
SH_MODEL = "0.0.1"
12+
MH_MODEL = "test"
2213

23-
CROSSOVER_PROB = 0.9
14+
WORKING_DIR = os.path.dirname(os.path.abspath(__file__))
2415

25-
CROSSOVER_DISTRIBUTION_INDEX = 20
16+
SH_SEED = 42
17+
SH_POP_SIZE = 10
18+
SH_SLT_SIZE = 10
19+
SH_GENS = 2
20+
SH_TOURNAMENT_SIZE = 5
21+
SH_CRO_PROB = 0.9
22+
SH_CRO_DI = 20
23+
SH_MUT_PROB = 1.0/30
24+
SH_MUT_DI = 5
2625

27-
MUTATION_PROB = 1.0/30
26+
MH_SEED = 42
27+
MH_POP_SIZE = 10
28+
MH_SLT_SIZE = 10
29+
MH_GENS = 2
30+
MH_TOURNAMENT_SIZE = 5
31+
MH_X_PROB = 0.9
32+
MH_CRO_DI = 20
33+
MH_MUT_PROB = 1.0/30
34+
MH_MUT_DI = 5
35+
ALPHA = 2
36+
BETA = 6
2837

29-
MUTATION_DISTRIBUTION_INDEX = 5
38+
def __load_sh_model(model):
39+
global SH_SEED, SH_POP_SIZE, SH_SLT_SIZE, SH_GENS
40+
global SH_TOURNAMENT_SIZE, SH_CRO_PROB, SH_CRO_DI, SH_MUT_DI, SH_MUT_PROB
41+
if model == 'test':
42+
SH_SEED = 42
43+
SH_POP_SIZE = 10
44+
SH_SLT_SIZE = 10
45+
SH_GENS = 2
46+
SH_TOURNAMENT_SIZE = 5
47+
SH_CRO_PROB = 0.9
48+
SH_CRO_DI = 20
49+
SH_MUT_PROB = 1.0/30
50+
SH_MUT_DI = 5
51+
elif model == '0.0.1':
52+
SH_SEED = 42
53+
SH_POP_SIZE = 100
54+
SH_SLT_SIZE = 100
55+
SH_GENS = 100
56+
SH_TOURNAMENT_SIZE = 5
57+
SH_CRO_PROB = 0.9
58+
SH_CRO_DI = 20
59+
SH_MUT_PROB = 1.0/30
60+
SH_MUT_DI = 5
3061

31-
ALPHA = 2
62+
def __load_mh_model(model):
63+
global MH_SEED, MH_POP_SIZE, MH_SLT_SIZE, MH_GENS
64+
global MH_TOURNAMENT_SIZE, MH_CRO_PROB, MH_CRO_DI, MH_MUT_DI, MH_MUT_PROB
65+
if model == 'test':
66+
MH_SEED = 42
67+
MH_POP_SIZE = 10
68+
MH_SLT_SIZE = 10
69+
MH_GENS = 2
70+
MH_TOURNAMENT_SIZE = 5
71+
MH_CRO_PROB = 0.9
72+
MH_CRO_DI = 20
73+
MH_MUT_PROB = 1.0/30
74+
MH_MUT_DI = 5
75+
elif model == '0.0.1':
76+
MH_SEED = 42
77+
MH_POP_SIZE = 100
78+
MH_SLT_SIZE = 100
79+
MH_GENS = 100
80+
MH_TOURNAMENT_SIZE = 5
81+
MH_CRO_PROB = 0.9
82+
MH_CRO_DI = 20
83+
MH_MUT_PROB = 1.0/30
84+
MH_MUT_DI = 5
3285

33-
BETA = 6
86+
__load_sh_model(SH_MODEL)
87+
__load_mh_model(MH_MODEL)
Loading
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
44 0.0016010961579917128
2-
45 0.0016011382640617783
3-
46 0.0018011582640617783
4-
47 0.0014011182640617783
5-
48 0.0014011057826279405
6-
49 0.0016011257826279404
7-
50 0.0012010970816107834
8-
51 0.001401099939588667
9-
52 0.001400986551478805
10-
53 0.0014011174203246693
11-
54 0.0012010974203246693
12-
56 0.0016011257826279404
1+
30 0.0010010770816107832
2+
31 0.0010010657826279403
3+
32 0.0010010424397270295
4+
33 0.0010010424397270295
5+
34 0.0010010073457078846
6+
35 0.0010009969630479626
7+
36 0.0010009240064270208
8+
37 0.001000893148119278
9+
38 0.0008010582640617784
10+
39 0.0008010582640617784
11+
40 0.0008010582640617784
12+
41 0.0008010582640617784
13+
42 0.0008010582640617784
14+
43 0.0008010582640617784
15+
44 0.0008010582640617784
16+
45 0.0008010582640617784
17+
46 0.0010010000005773938
18+
47 0.0010010424397270295
19+
48 0.0010010424397270295
20+
49 0.0010010424397270295
21+
50 0.0010010424397270295
22+
51 0.0010010424397270295
23+
52 0.0010010657826279403
24+
53 0.0010010424397270295
25+
54 0.0010010424397270295
26+
55 0.0012010200005773937
27+
56 0.0012010654141478814
28+
57 0.0012010857826279405
29+
58 0.0012010857826279405
30+
59 0.0010010782640617784

examples/wusn/results/single_hop/ga-dem1_r25_1/pareto-front.json

+142-22
Large diffs are not rendered by default.
Binary file not shown.

0 commit comments

Comments
 (0)