Commit 454cef7c authored by Chanelle Lee's avatar Chanelle Lee
Browse files

merge conflicts resolved

parents e2334c47 0c790314
......@@ -10,8 +10,6 @@ from ePucks import EPuckSim
from myThreads import ControllerThread
logger = logging.getLogger(__name__)
logger_b = logging.getLogger('beliefLogger')
logger_p = logging.getLogger('poolLogger')
def main(numSims=1):
......@@ -30,21 +28,12 @@ def main(numSims=1):
logger.critical('Simulation not started! {}'.format(sretCode))
sys.exit('Could not start simulation {}'.format(sretCode))
time.sleep(5)
logger_b.info('Now on simulation {}'.format(i))
logger_p.info('Now on simulation {}'.format(i))
controllerJobs = start()
try:
controllerJobs = setUp()
# Start all threads
for p in controllerJobs:
p.start()
time.sleep(0.1)
# Wait for all threads to finish
for p in controllerJobs:
p.wait()
except KeyboardInterrupt:
print "Ctrl-C pressed..."
for p in controllerJobs:
p.stop()
while any([p.isAlive() for p in controllerJobs]):
time.sleep(1)
except BaseException as exc:
print('exception "{}" occurred'.format(type(exc)))
sys.exit(1)
time.sleep(5)
eretCode = vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
......@@ -55,22 +44,27 @@ def main(numSims=1):
time.sleep(240)
def setUp(noise):
def start(noise):
optionsList = [0, 1, 2, 3, 4]
w = 1.0
noise = noise
poolSize = 2
poolSize = 5
distrust = 0.0
numEpucks = 5 # 20
ePucks = []
populationBeliefs = (1.0/len(optionsList)) * np.ones((numEpucks,
len(optionsList)))
for i, p in enumerate(populationBeliefs):
ePucks.append(EPuckSim(i, p, w, optionsList, noise, poolSize))
ePucks.append(EPuckSim(i, p, w, optionsList, noise, poolSize,
numEpucks, distrust))
time.sleep(0.1)
controllerJobs = [ControllerThread(name='controller{}'.format(e.ID),
ePuck=e) for e in ePucks]
for p in controllerJobs:
p.start()
time.sleep(0.1)
return controllerJobs
......
......@@ -101,3 +101,4 @@ format="%(asctime)s [%(levelname)s] (%(threadName)-10s) %(name)s: %(message)s"
[formatter_dataFormatter]
format=%(asctime)s, %(message)s
datefmt=%Y-%m-%d %H:%M:%S
......@@ -35,7 +35,7 @@ class EPuckSim(object):
AVOID_WEIGHTRIGHT = (-0.05, -0.1, -0.1, 0.1, 0.1, 0.05)
def __init__(self, ePuckID, belief, w, optionList, noise, poolSize,
distrust=0.0):
popSize, distrust=0.0):
self.clientID = vrep.simxStart('127.0.0.1', 19900+ePuckID, True, True,
5000, 5) # Connect to V-REP
if self.clientID != -1:
......@@ -44,6 +44,7 @@ class EPuckSim(object):
else:
logger.critical('Connection not successful')
sys.exit('Could not connect ePuck {}'.format(ePuckID))
self.i = ePuckID
self.ID = EPuckSim.EPUCK_NAMES[ePuckID]
self.name = 'ePuck'+EPuckSim.EPUCK_NAMES[ePuckID]
self.scriptObjectName = ('ePuck_transparentBody'
......@@ -62,6 +63,7 @@ class EPuckSim(object):
# Belief and aggregation attributes
self._poolSize = poolSize
self._popSize = popSize
self._belief = np.array(belief)
self._w = w
self._optionList = optionList
......@@ -330,7 +332,6 @@ class EPuckSim(object):
''.format(res, retStrings))
logger.debug('Exiting: receiveMessage')
###############################################################################
# Move to Target Functions - new from epucks code
###############################################################################
......@@ -341,9 +342,7 @@ class EPuckSim(object):
self._evidence.append([site.ID, self.retLightReading()])
self._researchState += 1
logger.debug('Visited Site#%s and received evidence', str(site.ID))
evidence_logger.info('{}, {}, {}'.format(self.ID,
site.ID,
self._evidence[-1][-1]))
else:
self.moveToSite(site)
self.avoidance()
......@@ -428,6 +427,7 @@ class EPuckSim(object):
elif state == 2:
self.visitSite(self._sites[self.siteList[1]])
elif state == 3:
logEvidence(self.i, self._evidence)
self.updateOnComparison(self._evidence)
self._evidence = []
self._state += 1
......@@ -479,9 +479,7 @@ class EPuckSim(object):
newBelief /= newBelief.sum()
self._belief = newBelief
belief_logger.info('{0}, {1}, {2}'.format(self.ID,
self._belief,
self._timestep))
logBelief(self.i, self._belief, self._timestep)
logger.debug('New belief is now {0}'.format(self._belief))
###############################################################################
......@@ -559,7 +557,7 @@ class EPuckSim(object):
# Keep receiving messages until all neighbours have indicated ready to
# finish aggregation
neighbours = []
while (len(neighbours) < self._poolSize-1):
while (len(neighbours) < self._popSize-1):
self.receiveMessage('Opinion')
if self._msgReceived is None:
pass
......@@ -643,7 +641,7 @@ class EPuckSim(object):
return False
else:
logger.info('Finishing')
return self.finish()
return True
###############################################################################
......@@ -669,6 +667,21 @@ def betaOp_multiProp(w, opinions):
return nOp
def logBelief(i, belief, timestep):
for j, b in enumerate(belief):
belief_logger.info('{0}, {1}, {2}, {3}'.format(i, timestep, i, b))
def logEvidence(i, evidence):
if len(evidence) == 2:
evidence_logger.info('{0}, {1}, {2}, {3}, {4}'.format(i,
*evidence[0],
*evidence[1]))
else:
logger.critical('Logging evidence with length: {}'.format(len(evidence)
))
def velocityClamp(velocity, minV, maxV):
if velocity < minV:
return minV
......
......@@ -28,7 +28,7 @@ class ControllerThread(threading.Thread):
def __init__(self, name, ePuck):
super(ControllerThread, self).__init__()
# self.daemon = True
self.daemon = True
self.ePuck = ePuck
self._stop_event = threading.Event()
self.setName(name)
......@@ -47,12 +47,16 @@ class ControllerThread(threading.Thread):
def run(self):
logger.debug('ControllerThread is starting.')
retFlag = False
while not self.stopped() and retFlag is False:
retFlag = self.ePuck.controller()
self.ePuck.move()
time.sleep(5)
self.ePuck.cleanUp()
logger.debug('ControllerThread has stopped')
try:
while not self.stopped() and retFlag is False:
retFlag = self.ePuck.controller()
self.ePuck.move()
time.sleep(5)
self.ePuck.cleanUp()
logger.debug('ControllerThread has stopped')
except BaseException as exc:
print('reraising Exception {}'.format(exc))
raise typeof(exc)(str(exc))
class BroadcastingThread(threading.Thread):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment