-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICA.py
125 lines (78 loc) · 3.77 KB
/
ICA.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
from country import Country
from helpers import *
from constants import Constant
from empire import Empire
import numpy as np
import time
class Ica:
def __init__(self):
self.countries = []
self.empires = []
self.colonies = []
def createCountries(self, **args):
count = args['count']
for i in range(count):
dimensions = generator(Constant.REPRESENTATION[0], Constant.REPRESENTATION[1])
self.countries.append(Country(dimensions))
return self.countries
def createEmpires(self, **args):
countries = args['countries']
costs = []
for country in countries:
costs.append(country.getCost())
costs = np.array(costs)
sorted_indicates = np.argsort(costs)
new_countries = []
for i in sorted_indicates:
new_countries.append(self.countries[i])
new_countries = np.array(new_countries)
self.countries = new_countries
empires = self.countries[: Constant.IMPERIALIST_COUNT]
self.colonies = self.countries[Constant.IMPERIALIST_COUNT:]
for i in empires:
self.empires.append(Empire(i))
empires_costs = np.array([np.sum(empire.getCost()) for empire in self.empires])
P = np.absolute(np.divide(empires_costs, np.sum(empires_costs)))
for colony in self.colonies:
k = randomSelection(P)
self.empires[k].addColony(colony)
return self.empires
def absorb(self):
for empire in self.empires:
for colony in empire.getColonies():
colony_vector = colony.getRepresentation()
X = (np.random.uniform(0, colony_vector * Constant.BETTA)).astype(int)
colony.setRepresentation(colony_vector + X)
imperialist = empire.getImperialist()
if colony.getCost() < imperialist.getCost():
temp = imperialist.getRepresentation()
imperialist.setRepresentation(colony.getRepresentation())
colony.setRepresentation(temp)
def competition(self):
if len(self.empires) == 1:
return self.empires[0]
empiresTotalCost = np.array([empire.getCost() for empire in self.empires])
weakest_empire_index = np.argmax(empiresTotalCost)
weakest_empire = self.empires[weakest_empire_index]
P = np.divide(empiresTotalCost, empiresTotalCost.sum())
P = np.flip(P, 0)
if weakest_empire.getColoniesCount() > 0:
weakest_empire_colonies_cost = np.array([colony.getCost() for colony in weakest_empire.getColonies()])
weakest_colony_index = np.argmax(weakest_empire_colonies_cost)
weakest_colony = weakest_empire.getColony(weakest_colony_index)
winning_empire_index = randomSelection(P)
winning_empire = self.empires[winning_empire_index]
winning_empire.addColony(weakest_colony)
weakest_empire.deleteColony(weakest_colony_index)
print("'{}' Empire <= '{}' Colony of '{}' Empire".format(winning_empire.name, weakest_colony.name,
weakest_empire.name), end='\n')
elif weakest_empire.getColoniesCount() == 0:
winning_empire_index = randomSelection(P)
winning_empire = self.empires[winning_empire_index]
winning_empire.addColony(weakest_empire.getImperialist())
del self.empires[self.empires.index(weakest_empire)]
print("***************************")
print("'{}' Empire x '{}' Empire".format(winning_empire.name, weakest_empire.name))
print("***************************", end='\n\n')
time.sleep(.100)
self.competition()