-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDE_phoenix.py
More file actions
530 lines (394 loc) · 16.6 KB
/
DE_phoenix.py
File metadata and controls
530 lines (394 loc) · 16.6 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 13 15:08:00 2017
@author: Johannes
"""
import random as rnd
import numpy as np
import math
# - - - - - - - - - - - - - - - - - - - - - - - D E F I N I N G C L A S S E S - - - - - - - - - - - - - - - - - - - - - - - - - -
class individual():
def __init__(self,genome):
self.genome = genome
self.profit = 0
def update_profit(self):
self.profit = evaluate_self(self)
class problem():
#by Marieke
class powerplant():
def __init__(self, planttype):
if planttype == 1:
self.kwhPerPlants = 50000
self.costPerPlant = 10000
self.maxPlants = 100
if planttype == 2:
self.kwhPerPlants = 600000
self.costPerPlant = 80000
self.maxPlants = 50
if planttype == 3:
self.kwhPerPlants = 4000000
self.costPerPlant = 400000
self.maxPlants = 3
def plantTypeCost(self,energy_amount):
cost = self.costPerPlant
kwhPerPlant = self.kwhPerPlants
maxPlants = self.maxPlants
# if s non-positive, return 0
if(energy_amount <= 0):
return 0
#if x larger than possible generation, return infinite
psblgen = kwhPerPlant * maxPlants
if(energy_amount > psblgen):
return float('inf')
#otherwise find amount of plants needed to generate s
plantsNeeded = math.ceil(energy_amount / kwhPerPlant)
#return cost (amount of plants * cost per plant)
return plantsNeeded * cost
class market():
def __init__(self, market):
if market == 1:
self.maxPrice = 0.45
self.maxDemand = 2000000
if market == 2:
self.maxPrice = 0.25
self.maxDemand = 30000000
if market == 3:
self.maxPrice = 0.2
self.maxDemand = 20000000
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def init():
global powerplant1
global powerplant2
global powerplant3
global max_power
global max_demand
global market1
global market2
global market3
powerplant1 = problem.powerplant(1)
powerplant2 = problem.powerplant(2)
powerplant3 = problem.powerplant(3)
market1 = problem.market(1)
market2 = problem.market(2)
market3 = problem.market(3)
max_power = (
powerplant1.kwhPerPlants * powerplant1.maxPlants +
powerplant2.kwhPerPlants * powerplant2.maxPlants +
powerplant3.kwhPerPlants * powerplant3.maxPlants )
max_demand = (
market1.maxDemand + market2.maxDemand + market3.maxDemand )
def initalize_population(agentcount):
population = []
while agentcount != 0:
e1 = rnd.randint(0,(powerplant1.kwhPerPlants * powerplant1.maxPlants))
e2 = rnd.randint(0,(powerplant2.kwhPerPlants * powerplant2.maxPlants))
e3 = rnd.randint(0,(powerplant3.kwhPerPlants * powerplant3.maxPlants))
s1 = rnd.randint(0,market1.maxDemand)
s2 = rnd.randint(0,market2.maxDemand)
s3 = rnd.randint(0,market3.maxDemand)
p1 = rnd.uniform(0,market1.maxPrice)
p2 = rnd.uniform(0,market2.maxPrice)
p3 = rnd.uniform(0,market3.maxPrice)
new_gene = [e1,e2,e3,s1,s2,s3,p1,p2,p3]
new_individual = individual(new_gene)
new_individual.update_profit
population.append(new_individual)
agentcount -= 1
return population
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"""
donor_selection -> differential_mutation -> genetic_crossover - > gene_edit - > selection
"""
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def donor_selection(population, scaling_factor):
new_population = []
for individual in population:
copy_population = list(population)
base_vector = rnd.choice(copy_population)
copy_population.remove(base_vector)
x1 = rnd.choice(copy_population)
copy_population.remove(x1)
x2 = rnd.choice(copy_population)
survivor = differential_mutation(individual, base_vector, x1, x2, scaling_factor)
new_population.append(survivor)
return new_population
def differential_mutation(target, base_vector, x1_vector, x2_vector, scaling_factor):
target_genes = target.genome
distance_vector = np.subtract(x1_vector.genome, x2_vector.genome)
distance_vector = distance_vector * scaling_factor
donor_genome = np.add(base_vector.genome,distance_vector)
gene_edit(target)
child = genetic_crossover(target_genes, donor_genome)
gene_edit(child)
fighting_pit = selection(target,child)
gene_edit(fighting_pit)
return fighting_pit
def genetic_crossover(target_genes, donor_genes):
pointer = 0
new_genome = []
while pointer != len(target_genes):
cross = rnd.uniform(0,1)
if cross > crossoverRate:
new_genome.append(target_genes[pointer])
else:
new_genome.append(donor_genes[pointer])
pointer += 1
child = individual(new_genome)
child.update_profit()
return child
def gene_edit(individual):
"""
This function searchs for genes that project outside of the boundaries of the search space and returns them into the boundaries
"""
edited_genes = []
production = individual.genome[:3]
distributer1 = individual.genome[3]
distributer2 = individual.genome[4]
distributer3 = individual.genome[5]
price1 = individual.genome[6]
price2 = individual.genome[7]
price3 = individual.genome[8]
for product in production:
if product > max_power or product < 0:
edited_genes.append(max_power)
else:
edited_genes.append(product)
if distributer1 > market1.maxDemand:
edited_genes.append(rnd.uniform(0,market1.maxDemand))
elif distributer1 < 0:
edited_genes.append(0)
else:
edited_genes.append(distributer1)
if distributer2 > market2.maxDemand:
edited_genes.append(rnd.uniform(0,market2.maxDemand))
elif distributer2 < 0:
edited_genes.append(0)
else:
edited_genes.append(distributer2)
if distributer3 > market3.maxDemand:
edited_genes.append(rnd.uniform(0,market3.maxDemand))
elif distributer3 < 0:
edited_genes.append(0)
else:
edited_genes.append(distributer3)
if price1 > market1.maxPrice or price1 < 0:
edited_genes.append(market1.maxPrice)
else:
edited_genes.append(price1)
if price2 > market2.maxPrice or price2 < 0:
edited_genes.append(market2.maxPrice)
else:
edited_genes.append(price2)
if price3 > market3.maxPrice or price3 < 0:
edited_genes.append(market3.maxPrice)
else:
edited_genes.append(price3)
individual.genome = edited_genes
individual.update_profit()
return individual
def selection(parent, child):
parent.update_profit()
child.update_profit()
if parent.profit >= child.profit:
return parent
else:
return child
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def find_best(population):
#by Johannes
"""
This function simply iterates through a population and returns the fittest individual
INPUT:
- A list of individuals, the population
OUTPUT:
- An object of the type "individual" that has the highest revenue value
"""
best = rnd.choice(population)
best.update_profit()
for individual in population:
individual.update_profit()
if individual.profit > best.profit:
best = individual
return best
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def evaluate_self(individual):
production_genome = individual.genome[:3]
distribution_genome = individual.genome[3:6]
price_genome = individual.genome[6:9]
production_cost = evaluate_production_costs(production_genome)
market_demand_1 = demand(price_genome[0], market1)
market_demand_2 = demand(price_genome[1], market2)
market_demand_3 = demand(price_genome[2], market3)
bias = 0
total_distribution = distribution_genome[0] + distribution_genome[1] + distribution_genome[2]
bought_cost = (
purchasing_cost(distribution_genome[0],market_demand_1) +
purchasing_cost(distribution_genome[1],market_demand_2) +
purchasing_cost(distribution_genome[2],market_demand_3) * costprice)
total_costs = production_cost + bought_cost + bias
revenue = (
min(distribution_genome[0],market_demand_1) * price_genome[0] +
min(distribution_genome[1],market_demand_2) * price_genome[1] +
min(distribution_genome[2],market_demand_3) * price_genome[2] )
profit = revenue - total_costs
return profit
def purchasing_cost(distribution, demand):
if distribution >= demand:
return 0
else:
cost = (demand - distribution)
return cost
def evaluate_production_costs(plant_genome):
cost_plant_1 = plantTypeCost(plant_genome[0],powerplant1)
cost_plant_2 = plantTypeCost(plant_genome[1],powerplant2)
cost_plant_3 = plantTypeCost(plant_genome[2],powerplant3)
total_production_costs = cost_plant_1 + cost_plant_2 + cost_plant_3
return total_production_costs
def plantTypeCost(s, plant):
"""
calculates the cost we will have to build n plants of type p
INPUT
- s (desired amount of energy)
- planttype
"""
kwhPerPlant = plant.kwhPerPlants
maxPlants = plant.maxPlants
costPerPlant = plant.costPerPlant
# if s non-positive, return 0
if(s <= 0):
return 0
#if x larger than possible generation, return infinite
psblgen = kwhPerPlant * maxPlants
if(s > psblgen):
return float('inf')
#otherwise find amount of plants needed to generate s
plantsNeeded = math.ceil(s / kwhPerPlant)
#return cost (amount of plants * cost per plant)
return plantsNeeded * costPerPlant
def demand(sellingPrice, market):
#by Marieke
"""
gives us the open demand of a market
INPUT
- sellingPrice (the price at which we sell energy)
- maxPrice (maximum price customers are willing to pay)
- maxDemand (total demand of a market)
OUTPUT
-
"""
maxPrice = market.maxPrice
maxDemand = market.maxDemand
#if the selling price is greater than what customers want to pay, return 0
if (sellingPrice > maxPrice):
return 0
if sellingPrice < 0:
return 0
#if nothing is produced for market
if (sellingPrice <= 0):
return maxDemand
#else determine the demand based on the selling price
demand = maxDemand - sellingPrice**2 * maxDemand / maxPrice**2
return demand
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def user_input():
#by Yannic(?)
"""
Output List with control Parameters: [crossoverRate,scalingFactor,populationSize]
"""
#Our three Control Parameters
crossoverRate = -1
scalingFactor = -1
populationSize = -1
costprice = -1
#output = [crossoverRate,scalingFactor,populationSize]
output = []
default = int(input("Do yo want to use default values? [0]Yes [else]No: "))
if default == 0:
#crossoverRate
output.append(0.5)
#scalingFactor
output.append(0.5)
#populationSize
output.append(20)
#costprice
output.append(0.6)
else:
print("")
#Crossover Rate Cr e [0,1]
while (crossoverRate < 0) or (crossoverRate > 1):
if crossoverRate == -1:
crossoverRate = float(input("Please specify Crossover Rate in [0,1]: "))
else:
crossoverRate = int(input("Crossover rate must be must be in [0,1]: "))
output.append(crossoverRate)
print("")
#Scaling factor F e (0,1)
while (scalingFactor <= 0) or (scalingFactor >= 1):
if scalingFactor == -1:
scalingFactor = float(input("Please specify Scaling Factor in [0,1]: "))
else:
scalingFactor = float(input("Scaling Factor must be must be in [0,1]: "))
output.append(scalingFactor)
print("")
#population size N > 4
while (populationSize < 5):
if populationSize == -1:
print("Recommended Population size: 5-10 times the dimension of the problem.")
populationSize = int(input("Please specify Population Size: "))
else:
populationSize = int(input("Population Size must be bigger than 4: "))
output.append(populationSize)
#Scaling factor F e (0,1)
while (costprice <= 0) or (costprice > 1):
if costprice == -1:
costprice = float(input("Please specify Cost Price in [0,1]: "))
else:
costprice = float(input("Cost Price must be must be in [0,1]: "))
output.append(costprice)
return output
def __MAIN__():
init()
userinput = user_input()
global costprice
global crossoverRate
crossoverRate = userinput[0]
scalingFactor = userinput[1]
agentcount = userinput[2]
costprice = userinput[3]
gencount = 500
pop = initalize_population(agentcount)
current_best = rnd.choice(pop)
while gencount != 0:
pop = donor_selection(pop,scalingFactor)
gen_best = find_best(pop)
if gen_best.profit > current_best.profit:
current_best = gen_best
print("Found new individual with fitness: ",current_best.profit)
else:
gencount -= 1
print("some Data:")
print("\n")
print("total energy produced: ",current_best.genome[0] + current_best.genome[1] + current_best.genome[2] ,"units of energy")
print("\n")
print("Plant 1 produced ",gen_best.genome[0],"units of energy")
print("Plant 2 produced ",gen_best.genome[1],"units of energy")
print("Plant 3 produced ",gen_best.genome[2],"units of energy")
print("\n")
print("total energy distributed: ",gen_best.genome[3] + gen_best.genome[4] + gen_best.genome[5] ,"units of energy")
print("\n")
print("Energy bought:",(gen_best.genome[3] + gen_best.genome[4] + gen_best.genome[5]) - (current_best.genome[0] + current_best.genome[1] + current_best.genome[2]))
print("\n")
print("Market 1 received ",gen_best.genome[3],"units of energy for a price of ",gen_best.genome[6])
print("Market 2 received ",gen_best.genome[4],"units of energy for a price of ",gen_best.genome[7])
print("Market 3 received ",gen_best.genome[5],"units of energy for a price of ",gen_best.genome[8])
print("\n")
print("\n")
print("Best value attained: ",int(round(gen_best.profit)))
print("")
print("Crossover Rate: ",crossoverRate)
print("Scaling Factor: ",scalingFactor)
print("Population Size: ",agentcount)
print("Cost Price: ",costprice)
print("")
print("####----------------------------------------------------------###")
__MAIN__()