Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.ensemble import ExtraTreesRegressor
- from sklearn.neural_network import MLPRegressor
- from sklearn.preprocessing import StandardScaler
- import warnings
- import datetime
- start = datetime.datetime.now().time()
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- import datetime
- temps = {}
- rains = {}
- humids = {}
- winds = {}
- with open("E:\\storage\\Weather.txt") as file:
- currDate = ""
- for line in file:
- arr = line.split(",")
- dateStr = str(arr[2]) + "/" + str(arr[1]) + "/" + str(arr[0])
- d = datetime.datetime.strptime(dateStr, "%d/%m/%Y")
- if dateStr != currDate:
- currDate = dateStr
- temps[dateStr] = []
- rains[dateStr] = []
- humids[dateStr] = []
- #winds[dateStr] = []
- realTime = float(arr[3])/60.
- temps[dateStr].append((realTime, float(arr[4])))
- humids[dateStr].append((realTime, int(arr[5])))
- #winds[dateStr].append((realTime, float(arr[6])))
- rains[dateStr].append((realTime, float(arr[7])))
- import math
- def getWeatherProperty(dateStr, realTime, propDict):
- arr = propDict[dateStr]
- fi = 0
- li = len(arr)-1
- secondLastIndex = -1000
- lastIndex = -1000
- closestRealTime = 0
- count = 0
- while(count < math.log(len(arr),2)+2):
- count+=1
- i = (fi+li)//2
- if abs(fi-li) == 1:
- if abs(realTime - arr[li][0]) > abs(arr[fi][0] - realTime):
- return arr[fi][1]
- else:
- return arr[li][1]
- """if secondLastIndex == i:
- print("Hello")
- if abs(arr[lastIndex][0] - arr[i][0]) > abs(arr[secondLastIndex][0] - arr[i][0]):
- return arr[secondLastIndex][1]
- else:
- return arr[lastIndex][1]"""
- if realTime > arr[i][0]:
- fi = i
- elif realTime < arr[i][0]:
- li = i
- elif realTime == arr[i][0]:
- return arr[i][1]
- secondLastIndex, lastIndex = lastIndex, i
- class Main:
- best = 0
- #bestPath = []
- import numpy as np
- baseFeatures = {}
- realHourScalers = {}
- regressors = {}
- """
- 31
- 27
- 0
- 1
- 2
- 4
- 28
- 5
- 6
- 7
- 9
- 10
- 12
- 14
- 15
- 19
- 20
- 21
- 22
- 24
- 25
- 30
- 29
- 26
- """
- ridesToVisit = [0,1,2,4,20,31,30,25,15,14,5]#,12,10,9]
- startTime = 480
- import pickle as pk
- for currentRide in range(0, 34):
- print(currentRide)
- if currentRide in [3,8,13,32,33,11, 16, 17, 18, 23]:
- continue
- rf = open("E:\\storage\\MKFeaturesLabels\\RandomForestRegressorV2-" + str(currentRide) + ".obj", 'rb')
- regressors[currentRide] = pk.load(rf)
- rf.close()
- tf = open("E:\\storage\\MKFeaturesLabels\\TestFeatures" + str(currentRide) + ".npy", 'rb')
- featuresGraph = np.load(tf)
- realHourIndex = len(featuresGraph)-1
- tf.close()
- realHourScalerFile = open("E:\\storage\\MKFeaturesLabels\\RealHourScaler"+ str(currentRide) + ".obj", 'rb')
- realHourScalers[currentRide] = pk.load(realHourScalerFile)
- realHourScalerFile.close()
- baseFeatures[currentRide] = featuresGraph
- def __init__(self):
- self.population = []
- self.cachedTotalFitness = -1
- #print(Chromosome([1, 4, 2, 31, 20, 0],self.startTime).getFitness())
- self.run()
- def randomSearch(self):
- max = 0
- for i in range(0,10000000):
- chromo = self.createRandomChromosome()
- f = chromo.getFitness()
- if f > max:
- max = f
- print(f)
- print(chromo.path)
- def test(self):
- from itertools import permutations
- max = -1000
- count = 0
- for i in permutations(Main.ridesToVisit):
- #count+=1
- #print(count)
- #print(i)
- c = Chromosome(i, Main.startTime)
- fit = c.getFitness()
- if fit > max:
- print(fit)
- max = fit
- def NNA(self):
- path = []
- cloned = list(Main.ridesToVisit)
- time = self.startTime
- while(len(path) < len(Main.ridesToVisit)):
- minWait = 10000
- minRide = -1
- for ride in cloned:
- features = Main.baseFeatures[ride]
- features[Main.realHourIndex] = time / 60.
- import numpy as np
- features[Main.realHourIndex] = Main.realHourScalers[ride].transform(np.matrix(features[Main.realHourIndex]))[0]
- wait = Main.regressors[ride].predict(features)[0]
- if wait < minWait:
- minWait = wait
- minRide = ride
- time += minWait
- path.append(minRide)
- cloned.remove(minRide)
- print(str(1e5/(time-self.startTime)))
- def run2(self):
- import numpy.random as rng
- for i in range(0, 1001):
- self.population.append(self.createRandomChromosome())
- for i in range(0,100000):
- print("iteration" + str(i))
- bestChromo = None
- maxFitness = 0
- for c in self.population:
- fitness = c.getFitness()
- if fitness > maxFitness:
- maxFitness = fitness
- bestChromo = c
- newpop = [bestChromo]
- while len(newpop) < len(self.population):
- c1 = self.selectChromosomeBasedOnFitness(None)
- c2 = self.selectChromosomeBasedOnFitness(None)
- if rng.uniform() < 0.3:
- c3 = c1.crossover(c2)
- else:
- c3 = c1
- if rng.uniform() < 0.3:
- c4 = c2.crossover(c1)
- else:
- c4 = c2
- if rng.uniform() < 0.2:
- newpop.append(c3.mutate())
- else:
- newpop.append(c3)
- if rng.uniform() < 0.2:
- newpop.append(c4.mutate())
- else:
- newpop.append(c4)
- self.population = newpop
- def run(self):
- import numpy.random as rng
- for i in range(0, 100):
- self.population.append(self.createRandomChromosome())
- for i in range(0, 100000):
- c1 = self.selectChromosomeBasedOnFitness(None)
- c2 = self.selectChromosomeBasedOnFitness(c1)
- if rng.uniform() < 0.7:
- c3 = c1.crossover(c2)
- else:
- c3 = c1
- if rng.uniform() < 0.7:
- c4 = c2.crossover(c1)
- else:
- c4 = c2
- #if rng.uniform() < 0.3:
- c5 = c3.mutate()
- #if rng.uniform() < 0.3:
- c6 = c4.mutate()
- #c3, c4 = c1.mutate(), c2.mutate()
- #c3,c4 = c1.crossover(c2),c2.crossover(c1)
- #c5,c6 = c3.mutate(),c4.mutate()
- cands = [c1,c2,c3,c4,c5,c6]
- maxFitness, secondFitness = -1000, -1000
- bestCand, secondCand = None, None
- for cand in cands:
- fitness = cand.getFitness()
- if fitness > maxFitness:
- secondFitness = maxFitness
- maxFitness = fitness
- secondCand = bestCand
- bestCand = cand
- elif fitness > secondFitness:
- secondFitness > fitness
- secondCand = cand
- self.population[self.population.index(c1)] = bestCand
- self.population[self.population.index(c2)] = secondCand
- self.cachedTotalFitness = -1
- def createRandomChromosome(self):
- path = list(Main.ridesToVisit)
- from random import shuffle
- shuffle(path)
- return Chromosome(path, Main.startTime)
- def sortPopulation(self):
- self.population.sort(key=lambda chromo:chromo.getFitness())
- def getTotalFitness(self):
- if self.cachedTotalFitness != -1:
- return self.cachedTotalFitness
- total = 0
- for c in self.population:
- fitness = c.getFitness()
- if fitness > Main.best:
- Main.best = fitness
- print(fitness)
- print(c.path)
- total += fitness
- self.cachedTotalFitness = total
- return total
- def selectChromosomeBasedOnFitness(self, exception):
- totalFitness = self.getTotalFitness()
- if totalFitness == 0:
- print("TOTAL FITNESS 0")
- exit(0)
- import numpy.random as rng
- threshold = rng.uniform()
- thresholdFitness = threshold * totalFitness
- cumFitness = 0
- for i in range(0, len(self.population)):
- c = self.population[i]
- cumFitness += c.getFitness()
- if cumFitness > thresholdFitness:
- if c != exception:
- return c
- else:
- if i == 0:
- return self.population[1]
- else:
- return self.population[i-1]
- class Chromosome:
- storedWaits = {}
- def __init__(self, p, s):
- self.cachedFitness = -1
- self.path = p
- self.startTime = s
- def getFitness(self):
- if self.cachedFitness != -1:
- return self.cachedFitness
- time = self.startTime
- for i in range(0, len(self.path)):
- if time > 1470:
- self.cachedFitness = 0
- return self.cachedFitness
- currentRide = self.path[i]
- features = Main.baseFeatures[currentRide]
- features[Main.realHourIndex] = time/60.
- import numpy as np
- features[Main.realHourIndex] = Main.realHourScalers[currentRide].transform(np.matrix(features[Main.realHourIndex]))[0]
- tfeatures = tuple(features)
- if tfeatures in Chromosome.storedWaits:
- wait = Chromosome.storedWaits[tfeatures]
- else:
- wait = Main.regressors[currentRide].predict(tfeatures)[0]
- Chromosome.storedWaits[tfeatures]=wait
- time += wait
- self.cachedFitness = 1e5/(time-self.startTime)
- return self.cachedFitness
- def mutate(self):
- mutatedPath = list(self.path)
- import numpy.random as rng
- firstIndex = rng.randint(0, len(self.path))
- secondIndex = firstIndex
- while(secondIndex == firstIndex):
- secondIndex = rng.randint(0, len(self.path))
- mutatedPath[firstIndex],mutatedPath[secondIndex]=mutatedPath[secondIndex],mutatedPath[firstIndex]
- return Chromosome(mutatedPath, self.startTime)
- def crossover(self, partner):
- offspring = [-1 for i in range(0,len(self.path))]
- import numpy.random as rng
- i1 = rng.randint(0,len(self.path))
- i2 = rng.randint(0,len(self.path))
- if i1 > i2:
- i1,i2 = i2,i1
- used = set()
- for i in range(i1,i2+1):
- offspring[i] = self.path[i]
- used.add(offspring[i])
- o = 0
- for el in partner.path:
- if el not in used:
- used.add(el)
- if i1 <= o and i2 >= o:
- o = i2+1
- offspring[o] = el
- o+=1
- return Chromosome(offspring, self.startTime)
- Main()
- end = datetime.datetime.now().time()
- print(str(end-start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement