|
1 | 1 | from collections import deque
|
2 | 2 | import secrets
|
| 3 | +from random import randrange |
3 | 4 | from random import random
|
| 5 | + |
4 | 6 | import math
|
5 | 7 |
|
6 | 8 | global unitCounts
|
|
12 | 14 | global intervalsArray
|
13 | 15 | intervalsArray = []
|
14 | 16 |
|
| 17 | +def readFilesAndCreateObjects(): |
| 18 | + secondFile = open("second.txt", 'rt') |
| 19 | + lines = secondFile.read().split('\n') |
| 20 | + intervalCounts = int(lines.pop(0)) |
| 21 | + for line in lines: |
| 22 | + res = line.split(',') |
| 23 | + intervalsArray.append(Interval(res[0], res[1])) |
| 24 | + secondFile.close() |
| 25 | + |
| 26 | + firstFile = open("first.txt", "rt") |
| 27 | + lines = firstFile.read().split('\n') |
| 28 | + unitCounts = int(lines.pop(0)) |
| 29 | + for line in lines: |
| 30 | + res = line.split(',') |
| 31 | + unitArray.append(Unit(res[0], res[1], res[2], intervalCounts)) |
| 32 | + firstFile.close() |
| 33 | + |
15 | 34 |
|
16 | 35 | class Unit:
|
17 | 36 | def __init__(self, id, capacity, repairCount,intervalsCount):
|
@@ -42,27 +61,194 @@ def __init__(self, id, demand):
|
42 | 61 | self.intervalID = int(id)
|
43 | 62 | self.intervalDemand = int(demand)
|
44 | 63 |
|
45 |
| -def readFilesAndCreateObjects(): |
46 |
| - secondFile = open("second.txt", 'rt') |
47 |
| - lines = secondFile.read().split('\n') |
48 |
| - intervalCounts = int(lines.pop(0)) |
49 |
| - for line in lines: |
50 |
| - res = line.split(',') |
51 |
| - intervalsArray.append(Interval(res[0], res[1])) |
52 |
| - secondFile.close() |
53 |
| - |
54 |
| - firstFile = open("first.txt", "rt") |
55 |
| - lines = firstFile.read().split('\n') |
56 |
| - unitCounts = int(lines.pop(0)) |
57 |
| - for line in lines: |
58 |
| - res = line.split(',') |
59 |
| - unitArray.append(Unit(res[0], res[1], res[2], intervalCounts)) |
60 |
| - firstFile.close() |
61 |
| - |
62 |
| - |
| 64 | +class Population: |
| 65 | + def __init__(self,size,initialize): |
| 66 | + self.size = size |
| 67 | + self.individulas = [None] * size |
| 68 | + if initialize=='true': |
| 69 | + for i in range(size): |
| 70 | + indiv = generateIndividual() |
| 71 | + self.individulas[i] = indiv |
| 72 | + self.saveIndividual(i,indiv) |
| 73 | + |
| 74 | + def saveIndividual(self,index,indiv): |
| 75 | + self.individulas[index] = indiv |
| 76 | + |
| 77 | + def getIndividual(self,i): |
| 78 | + return self.individulas[i] |
| 79 | + |
| 80 | + # tested |
| 81 | + def getFittestInPop(self): |
| 82 | + fittest = self.individulas[0] |
| 83 | + for i in range(len(self.individulas)): |
| 84 | + if self.findMinimumIntervalCapacityInOrders(fittest) <= self.findMinimumIntervalCapacityInOrders(self.individulas[i]): |
| 85 | + fittest = self.individulas[i] |
| 86 | + return fittest |
| 87 | + |
| 88 | + def getFittestFitnessRate(self): |
| 89 | + fit = self.getFittestInPop() |
| 90 | + return self.findMinimumIntervalCapacityInOrders(fit) |
| 91 | + |
| 92 | + # tested |
| 93 | + def findMinimumIntervalCapacityInOrders(self,orderList): |
| 94 | + minimumInterval = -1 |
| 95 | + minimum = self.calcIntervalExtraCapacity(orderList, 0) |
| 96 | + minimumIntervalsList = [] |
| 97 | + for intervalIndex in range(len(intervalsArray)): |
| 98 | + extraCapacity = self.calcIntervalExtraCapacity(orderList, intervalIndex) |
| 99 | + if extraCapacity < minimum: |
| 100 | + minimum = extraCapacity |
| 101 | + minimumInterval = intervalIndex |
| 102 | + minimumIntervalsList = [] |
| 103 | + elif extraCapacity == minimum: |
| 104 | + minimumIntervalsList.append(intervalIndex) |
| 105 | + if len(minimumIntervalsList) != 0: |
| 106 | + if minimumInterval > -1: |
| 107 | + minimumIntervalsList.append(minimumInterval) |
| 108 | + minimumInterval = secrets.choice(minimumIntervalsList) |
| 109 | + minimumIntervalsList = [] |
| 110 | + # return minimumInterval |
| 111 | + return self.calcIntervalExtraCapacity(orderList, minimumInterval) |
| 112 | + |
| 113 | + # tested |
| 114 | + def calcIntervalExtraCapacity(self,orderList: [], intervalIndex: int): |
| 115 | + sum = 0 |
| 116 | + for unitIndex in range(len(unitArray)): |
| 117 | + if orderList[unitIndex][intervalIndex] == 0: |
| 118 | + sum = sum + unitArray[unitIndex].unitCapacity |
| 119 | + return sum - intervalsArray[intervalIndex].intervalDemand |
| 120 | + |
| 121 | + # for test |
| 122 | + def calcAllNeededInOrders(self,orderList): |
| 123 | + neededCapcacity = 0 |
| 124 | + for index in range(len(intervalsArray)): |
| 125 | + extraCapacity = self.calcIntervalExtraCapacity(orderList, index) |
| 126 | + if extraCapacity < 0: |
| 127 | + neededCapcacity = neededCapcacity + extraCapacity |
| 128 | + print(str(index) + " & Extra Capacity is " + str( |
| 129 | + (self.calcIntervalExtraCapacity(orderList, index)))) |
| 130 | + return neededCapcacity ## # |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +class Algorithm: |
| 135 | + def __init__(self,uniformRate,mutationRate,tournamentSize,elitism): |
| 136 | + self.uniformRate = uniformRate |
| 137 | + self.mutationRate = mutationRate |
| 138 | + self.tournamentSize = tournamentSize |
| 139 | + self.elitism = elitism |
| 140 | + |
| 141 | + def evelopePopulation(self,pop:Population): |
| 142 | + newPopulation = Population(pop.size,'false') |
| 143 | + |
| 144 | + # keep the best |
| 145 | + if self.elitism=='true': |
| 146 | + newPopulation.saveIndividual(0,pop.getFittestInPop()) |
| 147 | + |
| 148 | + # crossover Population |
| 149 | + if self.elitism=='true': |
| 150 | + self.elitismOffset = 1 |
| 151 | + else : |
| 152 | + self.elitismOffset = 0; |
| 153 | + |
| 154 | + #crossover |
| 155 | + |
| 156 | + for i in range(self.elitismOffset,pop.size,1): |
| 157 | + indiv1 = self.tournamentSelection(pop) |
| 158 | + indiv2 = self.tournamentSelection(pop) |
| 159 | + newIndiv = self.crossover(indiv1,indiv2) |
| 160 | + newPopulation.saveIndividual(i,newIndiv) |
| 161 | + # print(str(i) + " is") |
| 162 | + # printIndividuals(newIndiv) |
| 163 | + # print('changed') |
| 164 | + # printIndividuals(newPopulation.individulas) |
| 165 | + |
| 166 | + for i in range(self.elitismOffset,pop.size,1): |
| 167 | + mutatedIndiv = self.mutate(newPopulation.getIndividual(i)) |
| 168 | + newPopulation.saveIndividual(i,mutatedIndiv) |
| 169 | + |
| 170 | + return newPopulation |
| 171 | + |
| 172 | + # tested |
| 173 | + def crossover(self,indiv1,indiv2): |
| 174 | + newIndiv = [None] * len(indiv1) |
| 175 | + for i in range(len(indiv1)): |
| 176 | + if random() <= self.uniformRate: |
| 177 | + newIndiv[i] = indiv1[i] |
| 178 | + else: |
| 179 | + newIndiv[i] = indiv2[i] |
| 180 | + return newIndiv |
| 181 | + |
| 182 | + def mutate(self,indiv): |
| 183 | + for i in range(len(indiv)): |
| 184 | + if random() <= self.mutationRate: |
| 185 | + randomGenePoll = secrets.choice(unitArray[i].unitPools) |
| 186 | + indiv[i] = randomGenePoll |
| 187 | + return indiv |
| 188 | + |
| 189 | + def tournamentSelection(self,pop:Population): |
| 190 | + tournomentPop = Population(self.tournamentSize,'false') |
| 191 | + for i in range(self.tournamentSize): |
| 192 | + randomID = randrange(pop.size) |
| 193 | + tournomentPop.saveIndividual(i,pop.getIndividual(randomID)) |
| 194 | + |
| 195 | + fittest = tournomentPop.getFittestInPop() |
| 196 | + # print(tournomentPop.individulas[0]) |
| 197 | + # print(tournomentPop.individulas[1]) |
| 198 | + # print(self.crossover(tournomentPop.individulas[0],tournomentPop.individulas[1])) |
| 199 | + |
| 200 | + # for test |
| 201 | + # print(tournomentPop.calcAllNeededInOrders(fittest)) |
| 202 | + return fittest |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +def generateIndividual(): |
| 207 | + individual = [] |
| 208 | + for unit in unitArray: |
| 209 | + individual.append(secrets.choice(unit.unitPools)) |
| 210 | + return individual |
| 211 | + |
| 212 | +def printIndividuals(indivLists:list): |
| 213 | + for i in indivLists: |
| 214 | + print(i) |
| 215 | + |
| 216 | + |
| 217 | +def printPop(pop:Population,number): |
| 218 | + print('pop test' + str(number)) |
| 219 | + for i in range(len(pop.individulas)): |
| 220 | + print('indiv' + str(i)) |
| 221 | + print(pop.calcAllNeededInOrders(pop.individulas[i])) |
| 222 | + print() |
63 | 223 |
|
64 | 224 | def main() :
|
65 | 225 | readFilesAndCreateObjects()
|
66 |
| - |
| 226 | + pop = Population(10,'true') |
| 227 | + # printIndividuals(pop.individulas) |
| 228 | + |
| 229 | + # printPop(pop,1) |
| 230 | + |
| 231 | + # print(pop.getFittestInPop()) |
| 232 | + # print(pop.getFittestFitnessRate()) |
| 233 | + algorithm = Algorithm(0.5,0.015,5,'true') |
| 234 | + newPop = algorithm.evelopePopulation(pop) |
| 235 | + # print(algorithm.mutate(pop.individulas[9])) |
| 236 | + # algorithm.tournamentSelection(pop) |
| 237 | + # print(pop.getFittestInPop()) |
| 238 | + # print(pop.calcAllNeededInOrders(pop.getFittestInPop())) |
| 239 | + |
| 240 | + generationCount = 100 |
| 241 | + for generationIndex in range(generationCount): |
| 242 | + if pop.getFittestFitnessRate() <= newPop.getFittestFitnessRate(): |
| 243 | + print("generation: "+ str(generationIndex) + " fittest: " + str(newPop.getFittestFitnessRate())) |
| 244 | + pop = newPop |
| 245 | + newPop = algorithm.evelopePopulation(pop) |
| 246 | + |
| 247 | + print() |
| 248 | + print('answer is') |
| 249 | + fittest = pop.getFittestInPop() |
| 250 | + print(fittest) |
| 251 | + print(pop.calcAllNeededInOrders(fittest)) |
| 252 | + print("fittest fitness is: " + str(pop.getFittestFitnessRate())) |
67 | 253 |
|
68 | 254 | main()
|
0 commit comments