def test_raises_when_no_name(): with pytest.raises(ValueError): load_dcop(""" description: dcop description objective: max """)
def test_raises_when_no_objective(): with pytest.raises(ValueError): load_dcop(""" name: dcop name description: dcop description """)
def test_raises_when_invalid_objective(): with pytest.raises(ValueError): load_dcop(""" name: dcop name description: dcop description objective: foo """)
def test_variable_with_noisy_cost_function(self): self.dcop_str += """ variables: v1: domain : d1 cost_function: 0.2 * v1 noise_level: 0.05 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.variables), 1) self.assertIn('v1', dcop.variables) v1 = dcop.variable('v1') all_equal = True for v in dcop.domain('d1').values: noisy_cost = v1.cost_for_val(v) self.assertGreaterEqual(v*0.2+0.05, noisy_cost) self.assertLessEqual(v*0.2-0.05, v * 0.2) all_equal = all_equal and (noisy_cost == v*0.2) # if all cost where exactly equal to the cost function, it means we # probably did not apply the noise self.assertFalse(all_equal)
def test_extensional_constraint_one_var(self): self.dcop_str += """ constraints: cost_v1: type: extensional default: 10 variables: v1 values: 2: 2 3: 5 4: 0 | 3 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.constraints), 1) c = dcop.constraint('cost_v1') self.assertEqual(c.name, 'cost_v1') self.assertEqual(len(c.dimensions), 1) self.assertEqual(c.dimensions[0].name, 'v1') self.assertEqual(c(v1=2), 2) self.assertEqual(c(v1=5), 3) self.assertEqual(c(v1=0), 4) self.assertEqual(c(v1=3), 4) self.assertEqual(c(v1=7), 10) # Default value
def test_agents_with_default_route(self): self.dcop_str += """ agents: a1: capacity: 100 a2: capacity: 100 a3: capacity: 100 routes: default: 42 a1: a2: 10 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.agents), 3) self.assertIn('a1', dcop.agents) self.assertIn('a2', dcop.agents) self.assertEqual(dcop.agent('a2').default_route, 42) # route given in yaml str: self.assertEqual(dcop.agent('a1').route('a2'), 10) self.assertEqual(dcop.agent('a2').route('a1'), 10) # internal route : 0 self.assertEqual(dcop.agent('a1').route('a1'), 0) self.assertEqual(dcop.agent('a2').route('a2'), 0) # route not given in str : defaults to default_route self.assertEqual(dcop.agent('a1').route('a3'), 42) self.assertEqual(dcop.agent('a2').route('a3'), 42)
def test_load_name_and_description(): dcop = load_dcop(""" name: dcop name description: dcop description objective: min """) assert dcop.name == "dcop name" assert dcop.description, "dcop description" assert dcop.objective, "min"
def test_load_name_without_desc(): dcop = load_dcop(""" name: dcop name objective: max """) assert dcop.name == 'dcop name' assert dcop.objective == 'max' assert dcop.description == ''
def test_load_name_and_description(): dcop = load_dcop(""" name: dcop name description: dcop description objective: min """) assert dcop.name== 'dcop name' assert dcop.description, 'dcop description' assert dcop.objective, 'min'
def test_must_host(self): self.dcop_str += """ distribution_hints: must_host: a1: [v1] """ dcop = load_dcop(self.dcop_str) hints = dcop.dist_hints self.assertIsNotNone(hints) self.assertIn('v1', hints.must_host('a1'))
def test_must_host_returns_empty(self): self.dcop_str += """ distribution_hints: must_host: a1: [v1] """ dcop = load_dcop(self.dcop_str) hints = dcop.dist_hints self.assertIsNotNone(hints) self.assertEqual(len(hints.must_host('a5')), 0)
def test_host_with(self): self.dcop_str += """ distribution_hints: host_with: v1: [c1] """ dcop = load_dcop(self.dcop_str) hints = dcop.dist_hints self.assertIn('c1', hints.host_with('v1')) self.assertIn('v1', hints.host_with('c1'))
def test_variable_with_initial_value(self): self.dcop_str += """ variables: v1: domain : d1 initial_value: 1 """ dcop = load_dcop(self.dcop_str) v1 = dcop.variable('v1') self.assertEqual(v1.initial_value, 1)
def test_load_name_long_desc(): dcop = load_dcop(""" name: dcop name description: A long dcop description that span on several lines. Lorem ipsum sed dolores et tutti quanti. objective: max """) assert dcop.name == 'dcop name' assert dcop.description == 'A long dcop description that span on several ' \ 'lines. Lorem ipsum sed dolores et tutti ' \ 'quanti.'
def test_load_name_long_desc(): dcop = load_dcop(""" name: dcop name description: A long dcop description that span on several lines. Lorem ipsum sed dolores et tutti quanti. objective: max """) assert dcop.name == "dcop name" assert (dcop.description == "A long dcop description that span on several " "lines. Lorem ipsum sed dolores et tutti " "quanti.")
def test_must_host_several(self): self.dcop_str += """ distribution_hints: must_host: a1: [v1, c1] """ dcop = load_dcop(self.dcop_str) hints = dcop.dist_hints self.assertIsNotNone(hints) self.assertIn("v1", hints.must_host("a1")) self.assertIn("c1", hints.must_host("a1"))
def test_boolean_domain(self): self.dcop_str += """ domains: d1_bool: type: d1_type values: [true, false] """ dcop = load_dcop(self.dcop_str) d = dcop.domains['d1_bool'] self.assertEqual(d.name, 'd1_bool') self.assertEqual(d.type, 'd1_type') self.assertEqual(len(d.values), 2) self.assertEqual(d.values, (True, False))
def test_variable_with_cost_function(self): self.dcop_str += """ variables: v1: domain : d1 cost_function: 0.2 * v1 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.variables), 1) self.assertIn('v1', dcop.variables) v1 = dcop.variable('v1') self.assertEqual(v1.cost_for_val(1), 0.2)
def test_simple_domain(self): self.dcop_str += """ domains: d1: type: d1_type values: [0, 1, 2] """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.domains), 1) d = dcop.domains['d1'] self.assertEqual(d.name, 'd1') self.assertEqual(d.type, 'd1_type')
def test_host_with_several(self): self.dcop_str += """ distribution_hints: host_with: v1: [c1, v2] """ dcop = load_dcop(self.dcop_str) hints = dcop.dist_hints self.assertIn("c1", hints.host_with("v1")) self.assertIn("c1", hints.host_with("v2")) self.assertIn("v1", hints.host_with("c1")) self.assertIn("v1", hints.host_with("v2")) self.assertIn("v2", hints.host_with("v1")) self.assertIn("v2", hints.host_with("c1"))
def test_range_int_domain(self): self.dcop_str += """ domains: d1: type: d1_type values: [0 .. 10] """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.domains), 1) d = dcop.domains['d1'] self.assertEqual(d.name, 'd1') self.assertEqual(d.type, 'd1_type') self.assertEqual(len(d.values), 11) self.assertEqual(d.values, tuple(range(11)))
def test_string_domain(self): self.dcop_str += """ domains: d1: type: d1_type values: [A, B, C] """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.domains), 1) d = dcop.domains['d1'] self.assertEqual(d.name, 'd1') self.assertEqual(d.type, 'd1_type') self.assertEqual(len(d.values), 3) self.assertEqual(d.values, ('A', 'B', 'C'))
def test_one_agent(self): self.dcop_str += """ agents: a1: capacity: 100 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.agents), 1) self.assertIn('a1', dcop.agents) a1 = dcop.agent('a1') self.assertEqual(a1.name, 'a1') self.assertEqual(a1.capacity, 100)
def test_variable(self): self.dcop_str += """ variables: v1: domain : d1 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.variables), 1) self.assertIn('v1', dcop.variables) v1 = dcop.variable('v1') self.assertEqual(v1.name, 'v1') self.assertEqual(v1.domain.name, 'd1')
def test_two_var_constraint(self): self.dcop_str += """ constraints: ws: type: intention function: v1 * 4 - 2 * v2 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.constraints), 1) c = dcop.constraint('ws') self.assertEqual(c.name, 'ws') self.assertEqual(c(v1=10, v2=3), 34)
def test_one_var_constraint(self): self.dcop_str += """ constraints: cost_v1: type: intention function: v1 * 0.4 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.constraints), 1) c = dcop.constraint('cost_v1') self.assertEqual(c.name, 'cost_v1') self.assertEqual(c(v1=10), 4)
def test_string_domain(self): self.dcop_str += """ domains: d1: type: d1_type values: [A, B, C] """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.domains), 1) d = dcop.domains["d1"] self.assertEqual(d.name, "d1") self.assertEqual(d.type, "d1_type") self.assertEqual(len(d.values), 3) self.assertEqual(d.values, ("A", "B", "C"))
def test_external_var_constraint(self): self.dcop_str += """ constraints: cond: type: intention function: 10 if e1 else 0 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.constraints), 1) c = dcop.constraint('cond') self.assertEqual(c.name, 'cond') self.assertEqual(c(e1=True), 10) self.assertEqual(c(e1=False), 0)
def test_variable(self): self.dcop_str += """ external_variables: ext_var1: domain: d1 initial_value: 2 """ dcop = load_dcop(self.dcop_str) self.assertEqual(len(dcop.external_variables), 1) self.assertIn('ext_var1', dcop.external_variables) e1 = dcop.get_external_variable('ext_var1') self.assertEqual(e1.name, 'ext_var1') self.assertEqual(e1.domain.name, 'd1')
def test_agents_no_route_def(self): self.dcop_str += """ agents: a1: capacity: 100 a2: capacity: 100 a3: capacity: 100 """ dcop = load_dcop(self.dcop_str) # no defaul given self.assertEqual(dcop.agent('a1').route('a3'), 1)