Advertisement
karspider

ga

Dec 11th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.51 KB | None | 0 0
  1. from sklearn.ensemble import ExtraTreesRegressor
  2. from sklearn.neural_network import MLPRegressor
  3. from sklearn.preprocessing import StandardScaler
  4. import warnings
  5. import datetime
  6. start = datetime.datetime.now().time()
  7.  
  8. warnings.filterwarnings("ignore", category=DeprecationWarning)
  9.  
  10. import datetime
  11. temps = {}
  12. rains = {}
  13. humids = {}
  14. winds = {}
  15. with open("E:\\storage\\Weather.txt") as file:
  16. currDate = ""
  17. for line in file:
  18. arr = line.split(",")
  19. dateStr = str(arr[2]) + "/" + str(arr[1]) + "/" + str(arr[0])
  20. d = datetime.datetime.strptime(dateStr, "%d/%m/%Y")
  21. if dateStr != currDate:
  22. currDate = dateStr
  23. temps[dateStr] = []
  24. rains[dateStr] = []
  25. humids[dateStr] = []
  26. #winds[dateStr] = []
  27. realTime = float(arr[3])/60.
  28. temps[dateStr].append((realTime, float(arr[4])))
  29. humids[dateStr].append((realTime, int(arr[5])))
  30. #winds[dateStr].append((realTime, float(arr[6])))
  31. rains[dateStr].append((realTime, float(arr[7])))
  32. import math
  33. def getWeatherProperty(dateStr, realTime, propDict):
  34. arr = propDict[dateStr]
  35. fi = 0
  36. li = len(arr)-1
  37. secondLastIndex = -1000
  38. lastIndex = -1000
  39. closestRealTime = 0
  40. count = 0
  41. while(count < math.log(len(arr),2)+2):
  42. count+=1
  43. i = (fi+li)//2
  44. if abs(fi-li) == 1:
  45. if abs(realTime - arr[li][0]) > abs(arr[fi][0] - realTime):
  46. return arr[fi][1]
  47. else:
  48. return arr[li][1]
  49. """if secondLastIndex == i:
  50. print("Hello")
  51. if abs(arr[lastIndex][0] - arr[i][0]) > abs(arr[secondLastIndex][0] - arr[i][0]):
  52. return arr[secondLastIndex][1]
  53. else:
  54. return arr[lastIndex][1]"""
  55. if realTime > arr[i][0]:
  56. fi = i
  57. elif realTime < arr[i][0]:
  58. li = i
  59. elif realTime == arr[i][0]:
  60. return arr[i][1]
  61. secondLastIndex, lastIndex = lastIndex, i
  62.  
  63. class Main:
  64. best = 0
  65. #bestPath = []
  66. import numpy as np
  67. baseFeatures = {}
  68. realHourScalers = {}
  69. regressors = {}
  70. """
  71. 31
  72. 27
  73. 0
  74. 1
  75. 2
  76. 4
  77. 28
  78. 5
  79. 6
  80. 7
  81. 9
  82. 10
  83. 12
  84. 14
  85. 15
  86. 19
  87. 20
  88. 21
  89. 22
  90. 24
  91. 25
  92. 30
  93. 29
  94. 26
  95. """
  96. ridesToVisit = [0,1,2,4,20,31,30,25,15,14,5]#,12,10,9]
  97. startTime = 480
  98. import pickle as pk
  99. for currentRide in range(0, 34):
  100. print(currentRide)
  101. if currentRide in [3,8,13,32,33,11, 16, 17, 18, 23]:
  102. continue
  103. rf = open("E:\\storage\\MKFeaturesLabels\\RandomForestRegressorV2-" + str(currentRide) + ".obj", 'rb')
  104. regressors[currentRide] = pk.load(rf)
  105. rf.close()
  106. tf = open("E:\\storage\\MKFeaturesLabels\\TestFeatures" + str(currentRide) + ".npy", 'rb')
  107. featuresGraph = np.load(tf)
  108. realHourIndex = len(featuresGraph)-1
  109. tf.close()
  110. realHourScalerFile = open("E:\\storage\\MKFeaturesLabels\\RealHourScaler"+ str(currentRide) + ".obj", 'rb')
  111. realHourScalers[currentRide] = pk.load(realHourScalerFile)
  112. realHourScalerFile.close()
  113. baseFeatures[currentRide] = featuresGraph
  114.  
  115.  
  116. def __init__(self):
  117. self.population = []
  118. self.cachedTotalFitness = -1
  119. #print(Chromosome([1, 4, 2, 31, 20, 0],self.startTime).getFitness())
  120. self.run()
  121.  
  122.  
  123. def randomSearch(self):
  124. max = 0
  125. for i in range(0,10000000):
  126. chromo = self.createRandomChromosome()
  127. f = chromo.getFitness()
  128. if f > max:
  129. max = f
  130. print(f)
  131. print(chromo.path)
  132.  
  133.  
  134. def test(self):
  135. from itertools import permutations
  136. max = -1000
  137. count = 0
  138. for i in permutations(Main.ridesToVisit):
  139. #count+=1
  140. #print(count)
  141. #print(i)
  142. c = Chromosome(i, Main.startTime)
  143. fit = c.getFitness()
  144. if fit > max:
  145. print(fit)
  146. max = fit
  147.  
  148. def NNA(self):
  149. path = []
  150. cloned = list(Main.ridesToVisit)
  151. time = self.startTime
  152. while(len(path) < len(Main.ridesToVisit)):
  153. minWait = 10000
  154. minRide = -1
  155. for ride in cloned:
  156. features = Main.baseFeatures[ride]
  157. features[Main.realHourIndex] = time / 60.
  158. import numpy as np
  159. features[Main.realHourIndex] = Main.realHourScalers[ride].transform(np.matrix(features[Main.realHourIndex]))[0]
  160. wait = Main.regressors[ride].predict(features)[0]
  161. if wait < minWait:
  162. minWait = wait
  163. minRide = ride
  164. time += minWait
  165. path.append(minRide)
  166. cloned.remove(minRide)
  167. print(str(1e5/(time-self.startTime)))
  168.  
  169. def run2(self):
  170. import numpy.random as rng
  171. for i in range(0, 1001):
  172. self.population.append(self.createRandomChromosome())
  173. for i in range(0,100000):
  174. print("iteration" + str(i))
  175. bestChromo = None
  176. maxFitness = 0
  177. for c in self.population:
  178. fitness = c.getFitness()
  179. if fitness > maxFitness:
  180. maxFitness = fitness
  181. bestChromo = c
  182.  
  183. newpop = [bestChromo]
  184. while len(newpop) < len(self.population):
  185. c1 = self.selectChromosomeBasedOnFitness(None)
  186. c2 = self.selectChromosomeBasedOnFitness(None)
  187. if rng.uniform() < 0.3:
  188. c3 = c1.crossover(c2)
  189. else:
  190. c3 = c1
  191. if rng.uniform() < 0.3:
  192. c4 = c2.crossover(c1)
  193. else:
  194. c4 = c2
  195. if rng.uniform() < 0.2:
  196. newpop.append(c3.mutate())
  197. else:
  198. newpop.append(c3)
  199. if rng.uniform() < 0.2:
  200. newpop.append(c4.mutate())
  201. else:
  202. newpop.append(c4)
  203. self.population = newpop
  204.  
  205.  
  206.  
  207. def run(self):
  208. import numpy.random as rng
  209.  
  210. for i in range(0, 100):
  211. self.population.append(self.createRandomChromosome())
  212. for i in range(0, 100000):
  213. c1 = self.selectChromosomeBasedOnFitness(None)
  214. c2 = self.selectChromosomeBasedOnFitness(c1)
  215. if rng.uniform() < 0.7:
  216. c3 = c1.crossover(c2)
  217. else:
  218. c3 = c1
  219. if rng.uniform() < 0.7:
  220. c4 = c2.crossover(c1)
  221. else:
  222. c4 = c2
  223. #if rng.uniform() < 0.3:
  224. c5 = c3.mutate()
  225. #if rng.uniform() < 0.3:
  226. c6 = c4.mutate()
  227. #c3, c4 = c1.mutate(), c2.mutate()
  228. #c3,c4 = c1.crossover(c2),c2.crossover(c1)
  229. #c5,c6 = c3.mutate(),c4.mutate()
  230. cands = [c1,c2,c3,c4,c5,c6]
  231. maxFitness, secondFitness = -1000, -1000
  232. bestCand, secondCand = None, None
  233. for cand in cands:
  234. fitness = cand.getFitness()
  235. if fitness > maxFitness:
  236. secondFitness = maxFitness
  237. maxFitness = fitness
  238. secondCand = bestCand
  239. bestCand = cand
  240. elif fitness > secondFitness:
  241. secondFitness > fitness
  242. secondCand = cand
  243. self.population[self.population.index(c1)] = bestCand
  244. self.population[self.population.index(c2)] = secondCand
  245. self.cachedTotalFitness = -1
  246.  
  247. def createRandomChromosome(self):
  248. path = list(Main.ridesToVisit)
  249. from random import shuffle
  250. shuffle(path)
  251. return Chromosome(path, Main.startTime)
  252.  
  253. def sortPopulation(self):
  254. self.population.sort(key=lambda chromo:chromo.getFitness())
  255.  
  256. def getTotalFitness(self):
  257. if self.cachedTotalFitness != -1:
  258. return self.cachedTotalFitness
  259. total = 0
  260. for c in self.population:
  261. fitness = c.getFitness()
  262. if fitness > Main.best:
  263. Main.best = fitness
  264. print(fitness)
  265. print(c.path)
  266. total += fitness
  267. self.cachedTotalFitness = total
  268. return total
  269.  
  270. def selectChromosomeBasedOnFitness(self, exception):
  271. totalFitness = self.getTotalFitness()
  272. if totalFitness == 0:
  273. print("TOTAL FITNESS 0")
  274. exit(0)
  275. import numpy.random as rng
  276. threshold = rng.uniform()
  277. thresholdFitness = threshold * totalFitness
  278. cumFitness = 0
  279. for i in range(0, len(self.population)):
  280. c = self.population[i]
  281. cumFitness += c.getFitness()
  282. if cumFitness > thresholdFitness:
  283. if c != exception:
  284. return c
  285. else:
  286. if i == 0:
  287. return self.population[1]
  288. else:
  289. return self.population[i-1]
  290.  
  291.  
  292. class Chromosome:
  293. storedWaits = {}
  294. def __init__(self, p, s):
  295. self.cachedFitness = -1
  296. self.path = p
  297. self.startTime = s
  298.  
  299. def getFitness(self):
  300. if self.cachedFitness != -1:
  301. return self.cachedFitness
  302. time = self.startTime
  303. for i in range(0, len(self.path)):
  304. if time > 1470:
  305. self.cachedFitness = 0
  306. return self.cachedFitness
  307. currentRide = self.path[i]
  308. features = Main.baseFeatures[currentRide]
  309. features[Main.realHourIndex] = time/60.
  310. import numpy as np
  311. features[Main.realHourIndex] = Main.realHourScalers[currentRide].transform(np.matrix(features[Main.realHourIndex]))[0]
  312. tfeatures = tuple(features)
  313. if tfeatures in Chromosome.storedWaits:
  314. wait = Chromosome.storedWaits[tfeatures]
  315. else:
  316. wait = Main.regressors[currentRide].predict(tfeatures)[0]
  317. Chromosome.storedWaits[tfeatures]=wait
  318. time += wait
  319. self.cachedFitness = 1e5/(time-self.startTime)
  320. return self.cachedFitness
  321.  
  322. def mutate(self):
  323. mutatedPath = list(self.path)
  324. import numpy.random as rng
  325. firstIndex = rng.randint(0, len(self.path))
  326. secondIndex = firstIndex
  327. while(secondIndex == firstIndex):
  328. secondIndex = rng.randint(0, len(self.path))
  329. mutatedPath[firstIndex],mutatedPath[secondIndex]=mutatedPath[secondIndex],mutatedPath[firstIndex]
  330. return Chromosome(mutatedPath, self.startTime)
  331.  
  332. def crossover(self, partner):
  333. offspring = [-1 for i in range(0,len(self.path))]
  334. import numpy.random as rng
  335. i1 = rng.randint(0,len(self.path))
  336. i2 = rng.randint(0,len(self.path))
  337.  
  338. if i1 > i2:
  339. i1,i2 = i2,i1
  340. used = set()
  341. for i in range(i1,i2+1):
  342. offspring[i] = self.path[i]
  343. used.add(offspring[i])
  344. o = 0
  345. for el in partner.path:
  346. if el not in used:
  347. used.add(el)
  348. if i1 <= o and i2 >= o:
  349. o = i2+1
  350. offspring[o] = el
  351. o+=1
  352. return Chromosome(offspring, self.startTime)
  353.  
  354.  
  355.  
  356. Main()
  357. end = datetime.datetime.now().time()
  358. print(str(end-start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement