Commit 8e60ae27 authored by Chanelle Lee's avatar Chanelle Lee
Browse files

Fixed all errors with the logging on botsim

parent e5b07364
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
%config IPCompleter.greedy=True
```
%% Cell type:code id: tags:
``` python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
```
%% Cell type:code id: tags:
``` python
beliefDF = pd.read_csv('botSim_beliefs.csv', names=['time', 'agent', 'iteration', 'option', 'belief'], index_col='time')
```
%% Cell type:code id: tags:
``` python
beliefDF.head()
```
%%%% Output: execute_result
agent iteration option belief
time
2019-03-05 16:12:58 3 0 0 0.076923
2019-03-05 16:12:58 3 0 1 0.230769
2019-03-05 16:12:58 3 0 2 0.230769
2019-03-05 16:12:58 3 0 3 0.230769
2019-03-05 16:12:58 3 0 4 0.230769
%% Cell type:code id: tags:
``` python
beliefDF.loc[beliefDF['iteration']==0]
```
%%%% Output: execute_result
agent iteration option belief
time
2019-03-05 16:12:58 3 0 0 0.076923
2019-03-05 16:12:58 3 0 1 0.230769
2019-03-05 16:12:58 3 0 2 0.230769
2019-03-05 16:12:58 3 0 3 0.230769
2019-03-05 16:12:58 3 0 4 0.230769
2019-03-05 16:13:05 0 0 0 0.230769
2019-03-05 16:13:05 0 0 1 0.076923
2019-03-05 16:13:05 0 0 2 0.230769
2019-03-05 16:13:05 0 0 3 0.230769
2019-03-05 16:13:05 0 0 4 0.230769
2019-03-05 16:13:09 2 0 0 0.230769
2019-03-05 16:13:09 2 0 1 0.230769
2019-03-05 16:13:09 2 0 2 0.230769
2019-03-05 16:13:09 2 0 3 0.076923
2019-03-05 16:13:09 2 0 4 0.230769
2019-03-05 16:13:13 4 0 0 0.230769
2019-03-05 16:13:13 4 0 1 0.230769
2019-03-05 16:13:13 4 0 2 0.230769
2019-03-05 16:13:13 4 0 3 0.076923
2019-03-05 16:13:13 4 0 4 0.230769
2019-03-05 16:13:22 1 0 0 0.230769
2019-03-05 16:13:22 1 0 1 0.230769
2019-03-05 16:13:22 1 0 2 0.230769
2019-03-05 16:13:22 1 0 3 0.230769
2019-03-05 16:13:22 1 0 4 0.076923
2019-03-05 16:13:42 0 0 0 0.157895
2019-03-05 16:13:42 1 0 0 0.157895
2019-03-05 16:13:42 0 0 1 0.157895
2019-03-05 16:13:42 3 0 0 0.157895
2019-03-05 16:13:42 2 0 0 0.157895
2019-03-05 16:13:42 1 0 1 0.157895
2019-03-05 16:13:42 0 0 2 0.473684
2019-03-05 16:13:42 3 0 1 0.157895
2019-03-05 16:13:42 2 0 1 0.157895
2019-03-05 16:13:42 1 0 2 0.473684
2019-03-05 16:13:42 0 0 3 0.052632
2019-03-05 16:13:42 3 0 2 0.473684
2019-03-05 16:13:42 2 0 2 0.473684
2019-03-05 16:13:42 1 0 3 0.052632
2019-03-05 16:13:42 0 0 4 0.157895
2019-03-05 16:13:42 3 0 3 0.052632
2019-03-05 16:13:42 2 0 3 0.052632
2019-03-05 16:13:42 1 0 4 0.157895
2019-03-05 16:13:42 3 0 4 0.157895
2019-03-05 16:13:42 2 0 4 0.157895
2019-03-05 16:13:43 4 0 0 0.034077
2019-03-05 16:13:43 4 0 1 0.011359
2019-03-05 16:13:43 4 0 2 0.920067
2019-03-05 16:13:43 4 0 3 0.000421
2019-03-05 16:13:43 4 0 4 0.034077
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
......@@ -11,7 +11,7 @@ from myThreads import ControllerThread
logger = logging.getLogger(__name__)
def main(numSims=1):
def main(numSims=10):
logging.config.fileConfig('configuration.ini')
# Connect to V-REP
clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)
......@@ -20,14 +20,20 @@ def main(numSims=1):
else:
logger.critical('Could not connect to streaming server')
time.sleep(5)
for noise in []
simNum = 0
paramNum = 0
for noise, distrust in ((0.18, 0.25), (0.35, 0.37), (0.53, 0.411),
(0.7, 0.43)):
paramNum += 1
for i in range(0, numSims):
sretCode = vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
simNum += 1
sretCode = vrep.simxStartSimulation(clientID,
vrep.simx_opmode_oneshot)
if sretCode > 1:
logger.critical('Simulation not started! {}'.format(sretCode))
sys.exit('Could not start simulation {}'.format(sretCode))
time.sleep(5)
controllerJobs = start(noise)
controllerJobs = start(paramNum, simNum, noise, distrust)
try:
while any([p.isAlive() for p in controllerJobs]):
time.sleep(1)
......@@ -35,7 +41,8 @@ def main(numSims=1):
print('exception "{}" occurred'.format(type(exc)))
sys.exit(1)
time.sleep(5)
eretCode = vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
eretCode = vrep.simxStopSimulation(clientID,
vrep.simx_opmode_oneshot)
if eretCode > 1:
logger.critical('Simulation not finished'
+ ' properly! {}'.format(eretCode))
......@@ -43,20 +50,20 @@ def main(numSims=1):
time.sleep(240)
def start(noise):
def start(paramNum, simNum, noise, distrust):
optionsList = [0, 1, 2, 3, 4]
w = 1.0
noise = noise
poolSize = 5
distrust = 0.0
distrust = distrust
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,
numEpucks, distrust))
ePucks.append(EPuckSim(paramNum, simNum, i, p, w, optionsList,
noise, poolSize, numEpucks, distrust))
time.sleep(0.1)
controllerJobs = [ControllerThread(name='controller{}'.format(e.ID),
......
......@@ -34,8 +34,8 @@ class EPuckSim(object):
AVOID_WEIGHTLEFT = (0.05, 0.1, 0.1, -0.1, -0.1, -0.05)
AVOID_WEIGHTRIGHT = (-0.05, -0.1, -0.1, 0.1, 0.1, 0.05)
def __init__(self, ePuckID, belief, w, optionList, noise, poolSize,
popSize, distrust=0.0):
def __init__(self, paramNum, simNum, ePuckID, belief, w, optionList,
noise, poolSize, 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,10 @@ class EPuckSim(object):
else:
logger.critical('Connection not successful')
sys.exit('Could not connect ePuck {}'.format(ePuckID))
# logging variables
self.paramNum = paramNum
self.simNum = simNum
self.i = ePuckID
self.ID = EPuckSim.EPUCK_NAMES[ePuckID]
self.name = 'ePuck'+EPuckSim.EPUCK_NAMES[ePuckID]
......@@ -132,6 +136,31 @@ class EPuckSim(object):
return orientation
###############################################################################
# Logging Functions
###############################################################################
def logBelief(self, timestep, belief):
for j, b in enumerate(belief):
belief_logger.info('{0}, {1}, {2}, {3}, {4}, {5}'
''.format(self.paramNum,
self.simNum,
self.i,
timestep,
j,
b))
def logEvidence(self, evidence):
if len(evidence) == 2:
evidence_logger.info('{0}, {1}, {2}, {3}, {4}, {5}, {6}'
''.format(self.paramNum,
self.simNum,
self.i,
*evidence[0],
*evidence[1]))
else:
logger.critical(('Logging evidence with length:' +
' {}'.format(len(evidence))))
###############################################################################
# Init Functions
###############################################################################
......@@ -427,7 +456,7 @@ class EPuckSim(object):
elif state == 2:
self.visitSite(self._sites[self.siteList[1]])
elif state == 3:
logEvidence(self.i, self._evidence)
self.logEvidence(self._evidence)
self.updateOnComparison(self._evidence)
self._evidence = []
self._state += 1
......@@ -479,7 +508,7 @@ class EPuckSim(object):
newBelief /= newBelief.sum()
self._belief = newBelief
logBelief(self.i, self._belief, self._timestep)
self.logBelief(self._timestep, self._belief)
logger.debug('New belief is now {0}'.format(self._belief))
###############################################################################
......@@ -563,7 +592,7 @@ class EPuckSim(object):
pass
elif not self._msgReceived[2] in neighbours:
try:
if int(self._msgReceived[0][-1]):
if 0 < int(self._msgReceived[0][-1]):
# neighbour has heard from entire population
neighbours.append(self._msgReceived[2])
except ValueError:
......@@ -618,14 +647,40 @@ class EPuckSim(object):
return False
def finish(self):
if self.agentIsWithinSite(self._sites[self._opinion]):
if self.agentIsWithinHome():
self.stop()
self.checkFinishComms()
return True
else:
self.moveToSite(self._sites[self._opinion])
self.moveToHome()
self.avoidance()
return False
def checkFinishComms(self):
self.msgToSend = (' '.join(str(item) for item in self._belief) +
' ' + str(2))
msgName = 'Opinion'
self._bThread = self.createBroadcastingThread(msgName)
# Keep receiving messages until all neighbours have indicated ready to
# finish simulation
neighbours = []
while (len(neighbours) < self._popSize-1):
self.receiveMessage('Opinion')
if self._msgReceived is None:
pass
elif not self._msgReceived[2] in neighbours:
try:
if int(self._msgReceived[0][-1]) == 2:
# neighbour has heard from entire population for this
# aggregation or has completely finished
neighbours.append(self._msgReceived[2])
except ValueError:
logger.warning('Value error in message')
self._msgReceived = None
# Ready to finish broadcasting as all neighbours have heard msg
time.sleep(5)
self.endBroadcastingThread()
###############################################################################
# Finite State Machine Functions
###############################################################################
......@@ -641,8 +696,7 @@ class EPuckSim(object):
return False
else:
logger.info('Finishing')
return True
return self.finish()
###############################################################################
###############################################################################
......@@ -667,21 +721,6 @@ 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
......
Markdown is supported
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