Commit 92fc635b authored by Chanelle Lee's avatar Chanelle Lee
Browse files

Finished parameter testing. All tests pass. Be aware that as some function...

Finished parameter testing. All tests pass. Be aware that as some function names have been changed, trial.py is currently incompatible with the rest of the code.
parent a6957423
......@@ -2,7 +2,8 @@
import numpy as np
def params(num_options, er, wr, k, pop_size, total_its, alpha):
def retParamsDict(num_options, evidence_rate, w_rate, pool_size, pop_size,
total_its, distrust):
"""
Returns parameters as a dictionary
......@@ -10,73 +11,104 @@ def params(num_options, er, wr, k, pop_size, total_its, alpha):
----------
num_options : int
number of options
eR : float
evidence_rate : float [0, 1]
evidence rate - chance of agents receiving evidence each iteration
wR : float
wR : float [0, 1]
weight for pooling operator
k : int
pool_size : int
number of agents for pooling
popSize : int
pop_size : int
population size
tIts : int
total_its : int
total number of iterations for which the simulation runs
alpha : float
distrust : float
distrust or alpha in the evidential updating operator. Agents' distrust
of the evidence source.
w : float
SProdOp parameter
"""
params = {'numOptions': int(num_options),
'totalIts': int(total_its),
'popSize': int(pop_size),
'distrust': float(alpha),
'k': int(k),
'eR': float(er),
'w': float(wr),
if not all([isinstance(x, int) for x in (num_options, pop_size, total_its,
pool_size)]):
raise TypeError("Passed value must be an int")
if not all([x > 0 for x in (num_options, pop_size, total_its)]):
raise ValueError("Passed value must be positive and non-zero")
if not all([0.0 <= x <= 1.0 for x in (evidence_rate, w_rate)]):
raise ValueError("Rates must be in [0,1]")
if not 0.0 < distrust < 1.0:
raise ValueError("Passed value must be in (0, 0.5)")
params = {'numOptions': num_options,
'totalIts': total_its,
'popSize': pop_size,
'distrust': distrust,
'poolSize': pool_size,
'evidenceRate': evidence_rate,
'w': w_rate,
}
return params
def optionWeights(n, case):
def retOptionWeights(n, case):
"""
Returns a list of the option weights which will be different depending on
which case is being used
Calculate sigma values for n options in the cases:
-1 -> equivalence: s1 = ... = sn
0 -> noiselessness: s1=1, s2=...=sn=0
1 -> case 1: s1 >~ s2 >~ ... >~ sn
2 -> case 2: s1 >> s2 = ... = sn
All weights set assuming that H_0 is the best and H_n is the worst
"""
def ret_weights_equivalence(n):
"""Returns n equivalent weights"""
return (1/n) * np.ones(n)
def _ret_equal_weights(n):
"""
Returns n equivalent weights
i.e.
w1 = ... = wn
"""
return (1 / n) * np.ones(n)
def ret_weights_noiseless(n):
"""Returns weight only for the best option"""
arr = np.zeros(2)
def _ret_noiseless_weights(n):
"""
Returns noiseless weights
i.e.
w1 = 1, w2 = ... = wn = 0
"""
arr = np.zeros(n)
arr[0] = 1
return arr
def ret_weights_case1(n):
"""Returns equally spaced weights from highest to lowest"""
return np.linspace(1, 0, n+2)[1:-1]/sum(np.linspace(0, 1, n+2)[1:-1])
def ret_weights_case2(n):
"""Returns one very high weight and then the rest are equivalent"""
return np.append(np.array([2*(1/n)]), (1-2*(1/n))/(n-1)*np.ones(n-1))
if case == -1:
weights = ret_weights_equivalence(n)
if case == 0:
weights = ret_weights_noiseless(n)
elif case == 1:
weights = ret_weights_case1(n)
elif case == 2:
weights = ret_weights_case2(n)
else:
raise ValueError("Case needs to be of values 0, 1 or 2")
# Should add in check for weights summing to one
return weights
def _ret_decreasing_weights(n):
"""
Returns weights that decrease as they get worse
i.e.
w1 >~ w2 >~ ... >~wn
"""
return (np.linspace(1, 0, n + 2)[1: -1] /
sum(np.linspace(0, 1, n + 2)[1: -1]))
def _ret_outlier_weights(n):
"""
Returns weights the best is significantly higher than the other which
are equal
i.e.
w1 >> w2 = ... = wn
"""
return np.append(np.array([2 * (1 / n)]),
(1 - 2 * (1 / n)) / (n - 1) * np.ones(n - 1))
if n < 1:
raise ValueError("n must be positive")
if not isinstance(n, int):
raise TypeError("n must be an integer")
weight_func_switcher = {"equal": _ret_equal_weights,
"noiseless": _ret_noiseless_weights,
"decreasing": _ret_decreasing_weights,
"outlier": _ret_outlier_weights}
weight_func = weight_func_switcher.get(case)
if weight_func is None:
raise ValueError("Invalid Case")
return weight_func(n)
import pytest
import numpy.testing
from noComparisonSimulation.parameters import *
def test_params_valid():
total_its = 20
pop_size = 10
num_options = 3
pool_size = 10
distrust = 0.001
w_rate = 1.0
evidence_rate = 0.7
params = retParamsDict(num_options, evidence_rate, w_rate, pool_size,
pop_size, total_its, distrust)
assert params["numOptions"] == num_options
assert params["popSize"] == pop_size
assert params["totalIts"] == total_its
assert params["distrust"] == distrust
assert params["poolSize"] == pool_size
assert params["evidenceRate"] == evidence_rate
assert params["w"] == w_rate
@pytest.mark.parametrize(
"num_options, evidence_rate, w_rate, pool_size, pop_size, total_its, "
"distrust", [
(-4, 0.7, 1.0, 4, 10, 20, 0.3),
(0, 0.7, 1.0, 4, 10, 20, 0.3),
(4, 1.5, 1.0, 4, 10, 20, 0.3),
(4, -0.5, 1.0, 4, 10, 20, 0.3),
(4, 1.0, 5, 4, 10, 20, 0.3),
(4, 1.0, -1.6, 4, 10, 20, 0.3),
(4, 1.0, 1.0, 4, 0, 20, 0.3),
(4, 1.0, 1.0, 4, 10, 0, 0.3),
(4, 1.0, 1.0, 4, 10, 20, 0.0),
(4, 1.0, 1.0, 4, 10, 20, 1.0)
])
def test_params_invalid_value(num_options, evidence_rate, w_rate, pool_size,
pop_size, total_its, distrust):
with pytest.raises(ValueError):
retParamsDict(num_options, evidence_rate, w_rate, pool_size, pop_size,
total_its, distrust)
@pytest.mark.parametrize("num_options, pool_size, pop_size, total_its", [
("4", 4, 10, 20),
(3.5, 4, 10, 20),
(4, 4, 5.3, 20),
(4, 4, 10, 10.5)
])
def test_params_invalid_type(num_options, pool_size, pop_size, total_its):
with pytest.raises(TypeError):
retParamsDict(num_options, 0.7, 1.0, pool_size, pop_size,
total_its, 0.001)
@pytest.mark.parametrize("n, case, expected", [
(3, "equal", np.array([1 / 3, 1 / 3, 1 / 3])),
(3, "noiseless", np.array([1, 0, 0])),
(3, "decreasing", np.array([0.5, 1 / 3, 1 / 6])),
(3, "outlier", np.array([2 / 3, 1 / 6, 1 / 6]))
])
def test_retOptionWeights_valid(n, case, expected):
numpy.testing.assert_allclose(retOptionWeights(n, case), expected)
def test_retOptionWeights_invalid_case():
with pytest.raises(ValueError):
retOptionWeights(5, "invalid_case")
@pytest.mark.parametrize("case", [
("equal"),
("noiseless"),
("decreasing"),
("outlier")
])
def test_retOptionWeights_invalid_n(case):
with pytest.raises(ValueError):
retOptionWeights(0, case)
with pytest.raises(ValueError):
retOptionWeights(-1, case)
with pytest.raises(TypeError):
retOptionWeights(1.5, case)
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