-
Notifications
You must be signed in to change notification settings - Fork 5
/
parameters_opt_ga.py
223 lines (167 loc) · 7.74 KB
/
parameters_opt_ga.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 10:14:34 2017
@author: mario
"""
import numpy as np
import csv
#==============================================================================
# # PARAMETROS
#==============================================================================
INPUT1 = 'Data/arr_alg_pattern_size_event_tracking3.csv' #
INPUT2 = 'Data/sampled_grid_event_tracking.csv' # used in evaluation function (fs_ga_func.py)
INPUT3 = 'Constants/combination.csv' # used in pop_valid_creation, evaluation
# and invalid_route_count functions
# (fs_ga_func.py and fs_MainOptim....py)
INPUT4 = 'Constants/ListaCoordenadasConvRefMetros3.csv' # used in create_tour
# (fs_ga_func.py)
INPUT5 = 'Constants/intersection_routes.csv'# used in intersec_count_f function
#(fs_intersec_finding_func.py)
INPUT6 = 'best_last_pop.csv'#
#INPUT7 = 'sampled_grid_event_tracking2.csv' # Not used in GA
INPUT8 = 'Constants/in_lake.csv'
INPUT9 = 'Constants/in_lake_center.csv'
INPUT10 = 'Constants/allowed_routes_positive.csv'
OUTPUT1 = 'Results/best_indiv_test_ngen100_sim1.csv'
OUTPUT2 = 'Results/improve_rate_ngen100_sim1.csv'
OUTPUT3 = 'Results/ImproveRate_Solution_ngen100_sim1.png'
OUTPUT4 = 'Results/Best_Solution_ngen100_sim1.csv'
OUTPUT5 = 'Results/best_last_pop_test.csv'
N_BEACON = 60
N_USED_BEACON = 60
N_SIM = 1
CXPB = 0.8
MUTPB = 0.2
NGEN = 100
POPU = 100
ELIT_RATE = 0.2
FRANJA = 20
ATT_FACTOR = 1000 # Intentos para encontrar siguiente baliza en poblacion inicial valida
ATT_POPU = 10000 # Intentos para encontrar una poblacion inicial validad de POPU individuos
LAKE_SIZE = 68720000
LAKE_SIZE_X = 12000
LAKE_SIZE_Y = 14000
GRID_SIZE = 200 # metros
GRID_X_DIV = LAKE_SIZE_X/GRID_SIZE # numero de cuadros sobre el eje x
GRID_Y_DIV = LAKE_SIZE_Y/GRID_SIZE # numero de cuadros sobre el eje y
# =============================================================================
# FIT_FUNC_TYPE = 2
# STRATEGY_PHASE = 1 #NO CAMBIAR HASTA ENCONTRAR FUNCION PARA SELECCION DE
# # SUB_GRUPO DE BALIZAS PARA FASE DE INTENSFICACION
# =============================================================================
'''
1 - Death Penalty + Penalty Factor -- km2
2 - Penalty Factor -- coverage %
3 - Exponential Penalty Factor -- coverage %
4 - Penalty Factor -- size km2
5 - Penalty Factor -- ROI
6 - Death Penalty
7 - Death Penalty -- ROI variation
8 - Penalty Factor -- ROI variation
'''
EXPERIMENT = 'test'
#==============================================================================
# # CONSTANTES
#==============================================================================
#==============================================================================
# arr_sampled_grid_pattern = np.loadtxt(INPUT2, dtype = 'uint8',
# delimiter =',')
#==============================================================================
###############################################################################
# =============================================================================
# if STRATEGY_PHASE == 1:
# arr_subgroup = np.arange(60,dtype='uint8')
#
# else:
# #PENDIENTE MECANISMO DE SELECCION DE SUBGRUPO DE BALIZAS!!!!!
#
# arr_reg1= np.loadtxt('reg1_ev_track.csv' ,dtype = 'uint8', delimiter =',')
# arr_reg2= np.loadtxt('reg2_ev_track.csv' ,dtype = 'uint8', delimiter =',')
#
#
# arr_subgroup = np.concatenate((arr_reg1,arr_reg2))
#
#
# =============================================================================
###############################################################################
#==============================================================================
# ###################Importar rutas validas################################
# Importa archivo combination.csv
# combination.csv = Matriz 60 x 60
# Indica si una ruta es valida desde una baliza a otra (fila a columna)
#==============================================================================
arr_allowed_routes = np.loadtxt(INPUT3,dtype = 'uint8',delimiter =',' )
arr_allowed_routes_pos = np.loadtxt(INPUT10,dtype = 'uint8',delimiter =',')
###############################################################################
lst_centers = []
for x in range(GRID_X_DIV):
sublst_centers = []
for y in range(GRID_Y_DIV):
sublst_centers.append([x, y])
lst_centers.append(sublst_centers)
arr_centers = np.array(lst_centers)
arr_centers_coord = GRID_SIZE*arr_centers+GRID_SIZE/2
#==============================================================================
# ###################Importar matriz de intersecciones#########################
# Importa archivo intersection_routes.csv
# intersection_routes.csv = Matriz 3,600 x 3,600
# Las lineas y columnas representan las rutas
# x= N_BEACONS*Baliza_origen1 + Baliza_destino1
# y= N_BEACONS*Baliza_origen2 + Baliza_destino2
# La interseccion en la matriz indica si hay interseccion o no entre las
# rutas (0 o 1)
# Ej.: Ruta 1 = b1b2, Ruta 2 = b3b4
# Verificar en x = 1*60 + 2 = 62 e y = 3*60 + 4 = 184
#==============================================================================
ifile = open(INPUT5, "rb") #
reader = csv.reader(ifile)
intersec_routes = [] # lista con intersecciones entre rutas (matrix 3,600 x 3,600)
for lst_intersec_ori in reader:
sub_lst_intersec = [] # sublista correspondiente a una linea de la matriz
for lst_intersec_dst in lst_intersec_ori:
if lst_intersec_dst != '':
sub_lst_intersec.append(lst_intersec_dst)
intersec_routes.append(sub_lst_intersec)
ifile.close()
###############################################################################
#==============================================================================
# ###########Importar Coordenadas de Archivo###################################
# Importa archivoListaCoordenadasConvRefMetros.csv
# ListaCoordenadasConvRefMetros.csv = Matriz 60 x 2
# Cada linea de la matriz representa las coordenadas en metros de cada baliza
# Ej.: b0 = (4860, 13500)
# El origen es un punto de referencia
#==============================================================================
with open(INPUT4, 'rb') as f:
reader = csv.reader(f)
coord_orig = list(reader)
###Coordenadas para el calculo del rango de accion en numeros decimales########
list_coord = [] # lista de coordenadas de balizas en formato (x,y)
for n in coord_orig:
coord = [float(n[0]), float(n[1])]
list_coord.append(coord)
#############Coordenadas para el algoritmo genetico en numeros complejos#######
list_coord2 = [] # lista de coordenadas de balizas en formato (x + jy)
# (numero complejo)
for n in coord_orig:
list_coord2.append(complex(float(n[0]),float(n[1])))
list_coord_subgroup =[]
for element in np.arange(60,dtype='uint8'):
list_coord_subgroup.append(list_coord2[element])
cities = list_coord2 # 1- coordenadas de la ciudad en el TSP, se utiliza para
# create_tour y graficar
# cities = list_coord_subgroup # 2- subgrupo de balizas
#print 'len cities', len(cities)
arr_inlake_square = np.loadtxt(INPUT8,dtype ='uint8', delimiter =',')
lst_centers = []
for x in range(GRID_X_DIV):
sublst_centers = []
for y in range(GRID_Y_DIV):
sublst_centers.append([x, y])
lst_centers.append(sublst_centers)
arr_centers = np.array(lst_centers)
arr_centers_coord = GRID_SIZE*arr_centers+GRID_SIZE/2
#arr_alg_pattern = np.loadtxt(INPUT1,dtype ='uint8', delimiter =',')
arr_inlake_center = np.loadtxt(INPUT9,dtype ='uint8', delimiter =',')
samp_pattern = np.zeros((GRID_X_DIV,GRID_Y_DIV),dtype = 'uint8')