-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·305 lines (197 loc) · 8.94 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# !/usr/bin/env python3
# -*- coding_ctrl: utf-8 -*-
#########################################
# Created by: Joao Pedro Peters Barbosa #
# & Pedro Henrique Peters Barbosa #
# #
# email: joao.peters@engenharia.ufjf.br #
# or pedro.henrique@engenharia.ufjf.br #
#########################################
"""
Disciplina [210115] - Topicos Especiais em Otimizacao: Tecnicas Inteligentes
Desenvolvimento do programa referente ao primeiro trabalho da disciplina
Despacho Economico de Usinas Termoeletricas com ZOP - Aplicacao de ACO
Prof.: Ivo Chaves da Silva Júnior
"""
# ------------------------------------------------------------------------------------------------- #
# ------------------------------------ BIBLIOTECAS UTILIZADAS ----------------------------------- #
import imageio
import numpy as np
import os
import random
import timeit
from oct2py import octave as oct
from scipy.io import loadmat
from pt1_ACO_CVXOPT import CustoGeracao
from pt3_ACO_PLOT import PNGIFPLOT, BESTPLOT
### ... ::: FIM BIBLIOTECAS UTILIZADAS ::: ... ###
########################################################################################################################
pwd = os.getcwd()
start = timeit.default_timer()
########################################################################################################################
### ... ::: LEITURA DE ARQUIVOS ::: ... ###
# Base de Dados Utilizadas: escolher uma, comentar as outras.
# Cada base de dados retorna as caracte_Nristicas das usinas, suas zop e a
# demanda eletrica total do problema.
# file = 'Sistema_10_ZP'
# file = 'Sistema_15_ZP'
file = 'Sistema_40_ZP'
# Leitura do Arquivo
path_file = pwd + '/' + file + '.mat'
if os.path.isfile(path_file) is False:
oct.eval('cd ' + pwd)
oct.eval(file)
oct.eval('save -v7 ' + file + '.mat')
# Armazenamento dos valores lidos em variaveis
D = loadmat(path_file)
PD = D['PD'][0][0]
Dados_Usinas = D['Dados_Usinas']
### ... ::: FIM LEITURA DE ARQUIVOS ::: ... ###
########################################################################################################################
########################################################################################################################
### ... ::: DADOS DA COLONIA & SOLUCOES INICIAIS ::: ... ###
# Numero de Geradores
nger = Dados_Usinas[:, 0]
total_ger = int(np.amax(nger))
# Numero de ZOPs por gerador
nzop_ger = np.zeros(total_ger, dtype='int8')
aux = 0
nzop_ger[aux] = 1
for i in range(1, nger.shape[0]):
if nger[i] != nger[i - 1]:
aux = aux + 1
nzop_ger[aux] += 1
# Numero de Formigas na Colonia
nAnts = 100
# Escolha inicial das formigas
Ants = np.zeros((nAnts, total_ger), dtype='int8')
for frmg in range(0, nAnts):
for usina in range(0, total_ger):
if (nzop_ger[usina] > 1):
Ants[frmg, usina] = 1 + np.round(random.random() * (nzop_ger[usina] - 1))
else:
Ants[frmg, usina] = 1
### ... ::: FIM DADOS DA COLONIA & SOLUCOES INICIAIS ::: ... ###
########################################################################################################################
########################################################################################################################
### ... ::: PROCESSO ITERATIVO - COLONIA DE FORMIGAS ::: ... ###
# Parametro da Evaporacao
sigma_best = 0.05
sigma = 0.25
sigma_worst = 0.75
# Dimensao da Matriz de Feromonio
MFero = np.zeros((total_ger, int(np.max(nzop_ger))))
# Numero maximo de iteracoes
maxIter = 200
# Variavel para armazenar melhor valor da FOB
best_fval = np.inf
worst_fval = -np.inf
# Para Montagem de GIFs
Images = []
# Processo Iterativo
for iter in range(maxIter):
# CVXOPT de cada Formiga
Pg_usinas, fval_total, fval_usina, exitflag_ = CustoGeracao(nAnts, Ants, total_ger, nzop_ger, Dados_Usinas, PD)
# Menor valor obtido entre as escolhas das formigas
fval_min = np.min(fval_total)
idx_min = np.argmin(fval_total)
# Maior valor obtido entre as escolhas das formigas
fval_max = np.max(fval_total)
idx_max = np.argmax(fval_total)
# Armazenamento do valor otimo
if (fval_min < best_fval):
best_idx = idx_min
best_fval = fval_min
best_seq = Ants[idx_min, :].copy()
best_ger = Pg_usinas[idx_min, :].copy()
best_iter = iter + 1
# Armazenamento do pior valor
if (fval_max > worst_fval):
worst_idx = idx_max
worst_fval = fval_max
worst_seq = Ants[idx_max, :].copy()
worst_ger = Pg_usinas[idx_max, :].copy()
worst_iter = iter + 1
#--------------------------------------------------------------#
# CONSTRUCAO DAS SOLUCOES - ATUALIZACAO DA MATRIZ DE FEROMONIO #
#--------------------------------------------------------------#
for frmg in range(0, nAnts):
for ute in range(0, total_ger):
zop_ute = Ants[frmg, ute] - 1
MFero[ute, zop_ute] = MFero[ute, zop_ute] + (1 / fval_total[frmg])
#-------------------------------------------------------------------------------------------#
# MONTAGEM DA ROLETA (DETERMINACAO DO PERCENTUAL DE CADA SOLUCAO ENCONTRADA PELAS FORMIGAS) #
#-------------------------------------------------------------------------------------------#
for usina in range(0, total_ger):
# Apenas para usinas com ZOPs > 1
if (nzop_ger[usina] > 1):
# Total de Feromonio da Usina
Total_MFero = np.sum(MFero[usina, :])
# Pedaco da Roleta referente a solucao
pct = np.round((MFero[usina, :] / Total_MFero) * 100)
# Posicoes com rastro de Feromonio
rastro = np.where(pct > 0)[0]
# Determinacao do numero de elementos/posicoes com rastros de feromonio
ncol = rastro.shape[0]
# Contagem do numero de fatias de cada solucao
cont = 0
# Guarda ponto de troca das fatias/solucoes dentro da roleta
guarda = 0
cassino_col = []
for j in range(0, ncol):
while (cont < pct[rastro[j]] + guarda - 1):
cont += 1
cassino_col.append(j)
guarda = cont
COL = len(cassino_col)
#-------------------------------------------------#
# SORTEIO DAS SOLUCOES COM BASE NA ROLETA MONTADA #
#-------------------------------------------------#
for ant in range(0, nAnts):
decisao = random.random() * 100
# Formigas SEGUEM o rastro de Feromonio (80% de chance)
if decisao <= 80:
# Roleta em movimento
sorteio = np.round(random.random() * (COL - 1)) + 1
# Roleta parada
posicao = cassino_col[int(sorteio - 1)]
# Valor da raiz
Ants[ant, usina] = rastro[posicao] + 1
# Formigas procuram outra solucao ALEATORIA (20% de chance)
else:
# Valor da raiz
Ants[ant, usina] = random.randint(1, nzop_ger[usina])
#-----------------------------------#
# EVAPORACAO DA MATRIZ DE FEROMONIO #
#-----------------------------------#
# Atualizacao dinamica
for row in range(0, total_ger):
if (nzop_ger[row] == 1):
MFero[row, 0] = (1 - sigma_best) * MFero[row, 0]
else:
for col in range(0, nzop_ger[row]):
if (col + 1 == best_seq[row]):
MFero[row, col] = (1 - sigma_best) * MFero[row, col]
elif (col + 1 == worst_seq[row]):
MFero[row, col] = (1 - sigma_worst) * MFero[row, col]
else:
MFero[row, col] = (1 - sigma) * MFero[row, col]
#---------------------------------------------------------#
# GRAFICO DO COMPORTAMENTO: ANALISE DINAMICA DO FEROMONIO #
#---------------------------------------------------------#
plt, Images = PNGIFPLOT(file, iter, maxIter, nzop_ger, total_ger, MFero, Images, nAnts)
imageio.mimsave(pwd + '/CVXOPT_{}_{}Formigas_{}iter.gif'.format(file, nAnts, maxIter), Images, fps=10)
plt.savefig(pwd + '/CVXOPT_{}_{}Formigas_{}iter.png'.format(file, nAnts, maxIter), format='png')
### ... ::: FIM DO PROCESSO ITERATIVO - COLONIA DE FORMIGAS ::: ... ###
########################################################################################################################
########################################################################################################################
### ... ::: ORGANIZACAO FINAL ::: ... ###
stop = timeit.default_timer()
print(stop - start)
print(best_fval, worst_fval)
print(best_seq, worst_seq)
print(best_ger, worst_ger)
print(best_iter, worst_idx)
print(best_idx, worst_idx)
### ... ::: FIM ORGANIZACAO FINAL ::: ... ###
########################################################################################################################