forked from PasaOpasen/geneticalgorithm2-6.8.7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmut_indexes.py
58 lines (42 loc) · 1.16 KB
/
mut_indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 31 20:18:39 2021
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
from geneticalgorithm2 import geneticalgorithm2 as ga
from geneticalgorithm2 import AlgorithmParams
def f(X):
return np.sum(X)
dim = 7
varbound = [(0,10)]*dim
model = ga(
function = f,
dimension=dim,
variable_type='real',
variable_boundaries=varbound,
algorithm_parameters=AlgorithmParams(
max_num_iteration=None,
mutation_probability=0.2,
elit_ratio=0.01,
crossover_probability=0.5,
parents_portion=0.3,
crossover_type='uniform',
mutation_type='uniform_by_center',
selection_type='roulette',
max_iteration_without_improv=None
)
)
pop = np.full((10, dim), 5, dtype = np.float32)
model.run(no_plot = False,
start_generation = (pop, None)
)
print(model.result.last_generation.variables)
# freeze dims [0,1,2,3] for mutation -- they won't be changed
model.run(no_plot = False,
start_generation = (pop, None),
mutation_indexes= [6, 5, 4]
)
print(model.result.last_generation.variables)