Commit 5b8ae234 authored by Chanelle Lee's avatar Chanelle Lee
Browse files

Finished agent class testing. Slightly varied how aggregation works for...

Finished agent class testing. Slightly varied how aggregation works for agents. They are not considered part of the neighbourhood and add their beliefs on. This feels a better way of structuring the functions.
parent 33d7c6f9
......@@ -63,7 +63,7 @@ class CMakeBuild(build_ext):
setup(name='noComparisonSimulation',
version='0.1.3',
version='0.1.3dev',
author='Chanelle Lee',
description='A module which calculates simple aggregation for a population',
long_description='',
......
......@@ -35,11 +35,6 @@ class Agent(object):
numOptions : int
Number of options to choose between
"""
if not (isinstance(idNum, int) and isinstance(numOptions, int)):
raise TypeError("Passed value must be an integer")
if not (isinstance(distrust, float) and isinstance(w, float)):
raise TypeError("Passed value must be a float")
if not (0.0 < distrust < 0.5):
raise ValueError("Passed distrust invalid - must be in (0, 0.5)")
if not (0.0 <= w <= 1.0):
......@@ -54,11 +49,11 @@ class Agent(object):
if np.any(belief < 0):
raise ValueError("Passed belief must have positive values")
self.idNum = idNum
self.idNum = int(idNum)
self._belief = belief
self._distrust = distrust
self._w = w
self._numOptions = numOptions
self._distrust = float(distrust)
self._w = float(w)
self._numOptions = int(numOptions)
self.reached_decision = False
def setBelief(self, newBelief):
......@@ -67,10 +62,12 @@ class Agent(object):
N.B. Usage is only for removing the overhead during aggregation of
every agent calculating the same sum individually.
"""
if abs(np.sum(newBelief) - 1.0) < 0.0001:
self._belief = newBelief
if abs(np.sum(newBelief) - 1.0) > 0.0001:
raise ValueError("Can not set belief to, ", newBelief)
elif newBelief.shape != self._belief.shape:
raise ValueError("Incorrect shape for belief")
else:
raise ValueError('Can not set belief to, ', newBelief)
self._belief = newBelief
def getBelief(self):
"""Returns the agent's belief"""
......@@ -80,6 +77,8 @@ class Agent(object):
"""
Updates belief value based on the new piece of evidence received
"""
if not 0 <= evidenceID < self._numOptions:
raise ValueError("passed invalid evidence id")
self._belief = hf.update_on_evidence(evidenceID,
self._distrust,
self._belief)
......@@ -95,10 +94,19 @@ class Agent(object):
----------
neighbour : instance of the agent class
other agent for comparison
N.B. Does not check shape of beliefs
"""
return hf.check_neighbour_consistent(self._belief,
neighbour.getBelief())
def checkNeighbourhoodConsistent(self, neighbourhood):
beliefs = [n.getBelief() for n in neighbourhood]
poolSize = len(neighbourhood)
return hf.check_neighbourhood_consistent(self._numOptions,
poolSize,
beliefs)
def aggregate(self, neighbourhood):
"""
Checks which agents in the neighbourhood are consistent with the agent
......@@ -108,9 +116,11 @@ class Agent(object):
--------
neighbourhood : list
list of agent class instances
N.B. Agent is not part of the neighbourhood
"""
beliefs = [n.getBelief() for n in neighbourhood]
poolSize = len(neighbourhood)
beliefs.append(self.getBelief()) # Add agent's belief
poolSize = len(neighbourhood) + 1
if hf.check_neighbourhood_consistent(self._numOptions,
poolSize,
beliefs):
......
......@@ -81,7 +81,7 @@ class Population(object):
neighbourhood = movedAgents
# One agent calculates new belief from pooling
retBelief = neighbourhood[-1].aggregate(neighbourhood)
retBelief = neighbourhood[0].aggregate(neighbourhood[1:])
# All agents update to new belief
if retBelief is not None:
......
import pytest
import numpy.testing
from noComparisonSimulation.agent import *
......@@ -13,22 +14,12 @@ def test_initialise_valid():
agent = Agent(idNum, belief, distrust, w, numOptions)
assert agent.idNum == idNum
assert agent._belief == belief
assert agent._distrust == distrust
assert agent._w == w
assert agent._numOptions == numOptions
assert agent.reached_decision is False
@pytest.mark.parametrize("idNum, belief, distrust, w, numOptions", [
(1.5, np.array([0.6, 0.2, 0.2]), 0.001, 1, 3),
(1, np.array([0.6, 0.2, 0.2]), "distrust", 1, 3),
(1, np.array([0.6, 0.2, 0.2]), 0.001, "w", 3),
(1, np.array([0.6, 0.2, 0.2]), 0.001, 1, 3.5)
])
def test_initialise_invalid_type(idNum, belief, distrust, w, numOptions):
with pytest.raises(TypeError):
Agent(idNum, belief, distrust, w, numOptions)
numpy.testing.assert_almost_equal(agent._belief, belief)
@pytest.mark.parametrize("distrust, w", [
......@@ -51,6 +42,95 @@ def test_initialise_invalid_belief(belief):
Agent(1, belief, 0.001, 1, 3)
# def test_setBelief_valid():
# agent = Agent(1, 0.6, 0.001, 1, 3)
# agent.setBelief()
def test_setBelief_valid():
agent = Agent(1, np.array([0.6, 0.2, 0.2]), 0.001, 1, 3)
new_belief = np.array([0.8, 0.1, 0.1])
agent.setBelief(new_belief)
numpy.testing.assert_almost_equal(agent._belief, new_belief)
@pytest.mark.parametrize("new_belief", [
(np.array([0.8, 0.1, 0.0])),
(np.array([0.6, 0.1, 0.2, 0.1]))
])
def test_setBelief_invalid(new_belief):
agent = Agent(1, np.array([0.6, 0.2, 0.2]), 0.001, 1, 3)
with pytest.raises(ValueError):
agent.setBelief(new_belief)
def test_getBelief_valid():
belief = np.array([0.6, 0.2, 0.2])
agent = Agent(1, belief, 0.001, 1, 3)
numpy.testing.assert_almost_equal(agent.getBelief(), belief)
@pytest.mark.parametrize("agent, evidenceID, expected", [
(Agent(1, np.array([1.0, 0.0]), 0.001, 1, 2), 0,
np.array([1.0, 0.0])),
(Agent(1, np.array([1.0, 0.0]), 0.001, 1, 2), 1,
np.array([1.0, 0.0])), # Static fixed point doesn't change
(Agent(1, np.array([0.8, 0.2]), 0.001, 1, 2), 0,
np.array([0.9997, 0.0003])),
(Agent(1, np.array([0.8, 0.2]), 0.001, 1, 2), 1,
np.array([0.0040, 0.9960])),
(Agent(1, np.array([0.8, 0.2]), 0.2, 1, 2), 0,
np.array([0.9412, 0.0588])),
(Agent(1, np.array([0.6, 0.3, 0.1]), 0.001, 1, 3), 0,
np.array([0.9993, 0.0005, 0.0002])),
(Agent(1, np.array([0.6, 0.3, 0.1]), 0.001, 1, 3), 2,
np.array([0.0060, 0.0030, 0.9911]))
])
def test_updateOnEvidence_valid(agent, evidenceID, expected):
agent.updateOnEvidence(evidenceID)
numpy.testing.assert_almost_equal(agent._belief, expected, decimal=4)
@pytest.mark.parametrize("evidenceID", [
(3),
(-1)
])
def test_updateOnEvidence_invalid(evidenceID):
agent = Agent(1, np.array([0.6, 0.3, 0.1]), 0.001, 1, 3)
with pytest.raises(ValueError):
agent.updateOnEvidence(evidenceID)
@pytest.mark.parametrize("agent, neighbour, expected", [
(Agent(1, np.array([1.0, 0.0]), 0.001, 1, 2),
Agent(1, np.array([1.0, 0.0]), 0.001, 1, 2), True),
(Agent(1, np.array([0.6, 0.4]), 0.001, 1, 2),
Agent(1, np.array([0.4, 0.6]), 0.001, 1, 2), True),
(Agent(1, np.array([1.0, 0.0]), 0.001, 1, 2),
Agent(1, np.array([0.0, 1.0]), 0.001, 1, 2), False),
(Agent(1, np.array([0.6, 0.3, 0.1]), 0.001, 1, 3),
Agent(1, np.array([0.0, 0.25, 0.75]), 0.001, 1, 3), True),
(Agent(1, np.array([1.0, 0.0, 0.0]), 0.001, 1, 3),
Agent(1, np.array([0.0, 0.0, 1.0]), 0.001, 1, 3), False),
(Agent(1, np.array([1.0, 0.0, 0.0]), 0.001, 1, 3),
Agent(1, np.array([0.0, 1.0, 0.0]), 0.001, 1, 3), False)
])
def test_checkNeighbourConsistent_valid(agent, neighbour, expected):
assert agent.checkNeighbourConsistent(neighbour) == expected
# def test_checkNeighbourhoodConsistent_valid(agent, neighbourhood, expected):
# pass
@pytest.mark.parametrize("agent, neighbourhood, expected", [
(Agent(1, np.array([0.5, 0.5]), 0.001, 1, 2),
[Agent(1, np.array([0.5, 0.5]), 0.001, 1, 2)],
np.array([0.5, 0.5])),
(Agent(1, np.array([0.6, 0.4]), 0.001, 1, 2),
[Agent(1, np.array([0.6, 0.4]), 0.001, 1, 2)],
np.array([0.6923, 0.3077]))
])
def test_aggregate_valid(agent, neighbourhood, expected):
ret_belief = agent.aggregate(neighbourhood)
numpy.testing.assert_almost_equal(ret_belief, expected, decimal=4)
import pytest
import numpy.testing
import numpy as np
from noComparisonSimulation import helperFunctions as hf
def test_helperFunction_aggregate_MProdOp():
ret_belief = hf.aggregate_MProdOp(2, 2, np.array([[0.6, 0.4], [0.6, 0.4]]))
numpy.testing.assert_almost_equal(ret_belief, np.array([0.6923, 0.3077]),
decimal=4)
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