Skip to content

Commit

Permalink
Adjustement : Sending to 0.01 seconde, add reward according to the ti…
Browse files Browse the repository at this point in the history
…me waiting, testing with 10 000 / 30 000 cars and pedestrian -> next : fork parallelisation and front test

Signed-off-by: slohan <[email protected]>
  • Loading branch information
Aslhans committed Sep 16, 2019
1 parent 121a37f commit 83a6e4c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 23 deletions.
5 changes: 3 additions & 2 deletions example/manager0.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

agent = newAgent(0, consumerConfig, producerConfig, "cluster0", "manager0", "manager")
agent._setAgents([0])
cycleManager([agent], [2])
agent._start()
cycleManager([agent], [10000])
agent._start()
agent._save()
5 changes: 3 additions & 2 deletions example/manager1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

agent = newAgent(1, consumerConfig, producerConfig, "cluster0", "manager0", "manager")
agent._setAgents([1])
cycleManager([agent], [2])
agent._start()
cycleManager([agent], [10000])
agent._start()
agent._save()
10 changes: 5 additions & 5 deletions regularflow/utils_regularflow/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _setForbidenAgents(self, forbidenIds:list) :
self.forbidenAgents = list(newForbidenAgents)

def _managementCycleLife(self) :
sleep(5)
sleep(1)
self.communication._broadcastInit(self.otherAgents)
i = 0
while True:
Expand All @@ -61,7 +61,7 @@ def _managementCycleLife(self) :
continue
jsonData = loads(msg.value().decode('utf-8'))
print(jsonData)
sleep(1)
sleep(0.01)
fromWho = self.communication._managementDataSending(jsonData)
if i > self.nbIteration :
self.communication.consumer.close()
Expand All @@ -83,7 +83,7 @@ def _followerCycleLife(self) :
if (self.communication._killConsume(jsonData) == CLOSE):
self.communication.consumer.close()
break
print(jsonData, self.state.state)
print(jsonData, self.state.state, self.state.score)
self.communication._updateEnv(jsonData, self.otherAgents, self.state)
if (np.array_equal(saveState, self.state._getState()) == False) :
self.communication._broadcastMyState(self.otherAgents, self.state, self.forbidenAgents)
Expand All @@ -102,7 +102,7 @@ def _initDataset(self) :
if (self.communication._killConsume(jsonData) == CLOSE):
self.communication.consumer.close()
break
print(jsonData, self.state.state)
print(jsonData, self.state.state, self.state.score)
self.dataset._influencerDataProcess(jsonData, self.otherAgents, self.forbidenAgents)

def _start(self) :
Expand All @@ -120,6 +120,6 @@ def _start(self) :
def _save(self) :
self.qfunction.save_weights("./saves/save_" + self.classType + str(self.myId), save_format='tf')

def _retore(self, path: str) :
def _restore(self, path: str) :
self.qfunction(np.zeros([1, SIZELAYERONE]))
self.qfunction.load_weights(path)
48 changes: 35 additions & 13 deletions regularflow/utils_regularflow/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from confluent_kafka import Consumer, Producer
from confluent_kafka import TopicPartition
from json import dumps
from time import process_time
from .constant import INDEXCAR, INDEXPEDESTRIAN, CLOSE
import numpy as np

Expand Down Expand Up @@ -50,7 +51,7 @@ def _setProducer(self, config:dict) :

def _broadcastMyState(self, otherAgents: list, state: object, forbidenList: list) :
ownState = list(state._getOwnState())
score = int(state._getScore())
score = float(state._getScore())
data = {"from": self.myId, "state" : ownState, "score": score}
for agent in otherAgents :
for key in agent :
Expand Down Expand Up @@ -96,26 +97,42 @@ def _managementDataSending(self, jsonData: dict) :
else :
fromWho = jsonData[key]
if key == "state" :
nbPedestrian = jsonData[key][INDEXPEDESTRIAN]
nbCars = jsonData[key][INDEXCAR]
if (nbCars >= 4) :
maxCars = np.random.randint(1, 4)
else :
maxCars = np.random.randint(0, nbCars + 1)
if (maxCars == 0 and nbCars > 0) :
maxCars = 1
if (nbPedestrian >= 4) :
maxPedestrian = np.random.randint(1, 4)
else :
maxPedestrian = np.random.randint(0, nbPedestrian + 1)
if (maxPedestrian == 0 and nbPedestrian > 0):
maxPedestrian = 1
if (jsonData[key][0] == 0) :
if np.random.uniform(0, 1) < 1 and jsonData[key][INDEXPEDESTRIAN] > 0 :
data = {"from": -1, "cars" : jsonData[key][INDEXCAR] + 1, "pedestrian": jsonData[key][INDEXPEDESTRIAN] - 1}
if nbPedestrian > 0 :
data = {"from": -1, "cars" : nbCars + np.random.randint(0, 4), "pedestrian": nbPedestrian - maxPedestrian}
self._sendTo(data, fromWho, self.clusterTopic)
elif np.random.uniform(0, 1) < 1 and jsonData[key][INDEXPEDESTRIAN] <= 0:
data = {"from": -1, "cars" : jsonData[key][INDEXCAR] + 1}
elif nbPedestrian <= 0:
data = {"from": -1, "cars" : nbCars + np.random.randint(0, 4)}
self._sendTo(data, fromWho, self.clusterTopic)
elif jsonData[key][INDEXPEDESTRIAN] > 0 :
data = {"from": -1, "pedestrian": jsonData[key][INDEXPEDESTRIAN] - 1}
elif nbPedestrian > 0 :
data = {"from": -1, "pedestrian": nbPedestrian - maxPedestrian}
self._sendTo(data, fromWho, self.clusterTopic)
else :
if np.random.uniform(0, 1) < 1 and jsonData[key][INDEXCAR] > 0:
data = {"from": -1, "pedestrian" : jsonData[key][INDEXPEDESTRIAN] + 1, "cars": jsonData[key][INDEXCAR] - 1}
if nbCars > 0:
data = {"from": -1, "pedestrian" : nbPedestrian + np.random.randint(0, 4), "cars": nbCars - maxCars}
self._sendTo(data, fromWho, self.clusterTopic)
elif np.random.uniform(0, 1) < 1 and jsonData[key][INDEXCAR] <= 0:
data = {"from": -1, "pedestrian" : jsonData[key][INDEXPEDESTRIAN] + 1}
elif nbCars <= 0:
data = {"from": -1, "pedestrian" : nbPedestrian + np.random.randint(0, 4)}
self._sendTo(data, fromWho, self.clusterTopic)
elif jsonData[key][INDEXCAR] > 0 :
data = {"from": -1, "cars": jsonData[key][INDEXCAR] - 1}
elif nbCars > 0 :
data = {"from": -1, "cars": nbCars - maxCars}
self._sendTo(data, fromWho, self.clusterTopic)


return fromWho

#<-------------------------------------- LISTENING -------------------------------------------------------------------------->
Expand All @@ -133,6 +150,10 @@ def _fromOtherAgent(self, key: str, jsonData: dict, fromWho: int, state: object)
state._setOtherAgentScore(fromWho, jsonData[key])
if key == "reverse" :
state._setState(light=list(jsonData[key][::-1]))
if (state.light[0] == 0) :
state.clockCars = process_time()
else:
state.clockPedestrian = process_time()

def _fromExtern(self, key: str, jsonData: dict, state: object) :
if key == "cars" :
Expand All @@ -155,3 +176,4 @@ def _updateEnv(self, jsonData: dict, otherAgents: list, state: object) :
if fromWho == -1 :
self._sendToManager(state) #send state to manager
fromWho = -2

13 changes: 13 additions & 0 deletions regularflow/utils_regularflow/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import numpy as np
from time import process_time

__all__ = ["State"]

Expand All @@ -24,6 +25,9 @@ def __init__(self) :
self.otherAgentState: list = np.zeros([self.nbOtherAgents, np.size(self.ownState)])
self.state = np.concatenate((self.light, self.nCars, self.nPedestrian, self.otherAgentState.flat))
self.otherAgentScore: list = np.zeros([self.nbOtherAgents, 1])
self.clockCars = 0
self.clockPedestrian = 0
self.score = 0

def _update(self) :
self.light = np.array(self.light)
Expand Down Expand Up @@ -80,8 +84,17 @@ def _getScore(self) :
actual = self._getnCars() + self._getnPedestrian()
ancien = self._getSaveCars() + self._getSavePedestrian()
score = ancien - actual
if (score > 0) :
score = 5
if (self._getnCars() < 5 and self._getnPedestrian() < 5):
score = 10
self._setSave(saveCars=[self._getnCars()])
self._setSave(savePedestrian=[self._getnPedestrian()])
if (self.light[0] == 0 and (self._getnCars() > 0)) :
score += ((process_time() - self.clockCars) * -1)
elif (self.light[0] == 1 and (self._getnPedestrian() > 0)) :
score += ((process_time() - self.clockPedestrian) * -1)
self.score = score
return score

#revoir le system de score si il y a beaucoup de voiture sur l'autre agent il faut passer au rouge pour augmenter son score
Expand Down
5 changes: 5 additions & 0 deletions regularflow/utils_regularflow/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import tensorflow as tf
import numpy as np
from time import process_time

__all__ = ["Toolbox"]

Expand All @@ -33,5 +34,9 @@ def _take_action(self, eps: int, state: object):
action = self.qfunction(np.expand_dims(state._getState(), 0).astype("float64"))
newLight = list(self._one_hot(np.argmax(action), 2))
state._setState(light=newLight)
if (newLight[0] == 0) :
state.clockCars = process_time()
else:
state.clockPedestrian = process_time()
return newLight

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from setuptools import setup, find_packages


__version__ = "1.0.6"
__version__ = "1.1.2"

try:
# for pip >= 10
Expand Down

0 comments on commit 83a6e4c

Please sign in to comment.