-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitness.py
61 lines (50 loc) · 1.79 KB
/
fitness.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
# coding: utf8
#############################################################################
#
# This file is part of evolveRobot.
#
# Contributors:
# - created by Valentin Owczarek
#############################################################################
import threading
import queue
from indi import *
from config import Config
# Tasklet/TasksManager/TaskDispatcher/DIstributeComputation
class Fitness(object):
def __init__(self):
self.name="Abstract fitness fun"
self.bestOverAll = None
self.bestNumber = 0
#MultiThreading
self.bestOverAllLock = threading.Lock()
self.queue = queue.Queue()
self.nbThread = Config.nbThread
for _ in range(self.nbThread):
t = threading.Thread(target=Fitness.worker, args=[self])
t.daemon = True
t.start()
def worker(self):
while True:
item = self.queue.get()
fit = self.simulate(item)
item.fitness = fit
#lock the best
with self.bestOverAllLock:
if self.bestOverAll == None:
self.bestOverAll = item
# print("NONE record {0}".format(self.bestOverAll.fitness))
if self.bestOverAll.fitness > item.fitness:
# print("New record {0}/{1}".format(item.fitness, self.bestOverAll.fitness))
self.bestOverAll = item
self.bestNumber += 1
self.queue.task_done()
def computeValues(self, n):
#load the queue
for task in n:
self.queue.put(task)
#wait until all task done
self.queue.join()
return 1
def simulate(self, n, idTask=0):
raise NotImplementedError