Beispiel #1
0
    def test_to_quadratic_program(self):
        """Test to_quadratic_program"""
        tsp = Tsp(self.graph)
        op = tsp.to_quadratic_program()
        # Test name
        self.assertEqual(op.name, 'TSP')
        # Test variables
        self.assertEqual(op.get_num_vars(), 16)
        for var in op.variables:
            self.assertEqual(var.vartype, VarType.BINARY)
        # Test objective
        obj = op.objective
        self.assertEqual(obj.sense, QuadraticObjective.Sense.MINIMIZE)
        self.assertEqual(obj.constant, 0)
        self.assertDictEqual(obj.linear.to_dict(), {})
        for edge, val in obj.quadratic.to_dict().items():
            self.assertEqual(val, self.graph.edges[edge[0]//4, edge[1]//4]['weight'])

        # Test constraint
        lin = op.linear_constraints
        self.assertEqual(len(lin), 8)
        for i in range(4):
            self.assertEqual(lin[i].sense, Constraint.Sense.EQ)
            self.assertEqual(lin[i].rhs, 1)
            self.assertEqual(lin[i].linear.to_dict(), {4*i: 1, 4*i+1: 1, 4*i+2: 1, 4*i+3: 1})
        for i in range(4):
            self.assertEqual(lin[4+i].sense, Constraint.Sense.EQ)
            self.assertEqual(lin[4+i].rhs, 1)
            self.assertEqual(lin[4+i].linear.to_dict(), {i: 1, 4+i: 1, 8+i: 1, 12+i: 1})
Beispiel #2
0
 def test_create_random_instance(self):
     """Test create_random_instance"""
     tsp = Tsp.create_random_instance(n=3, seed=123)
     graph = tsp.graph
     edge_weight = [graph.edges[edge]['weight'] for edge in graph.edges]
     expected_weight = [48, 91, 63]
     self.assertEqual(edge_weight, expected_weight)
Beispiel #3
0
 def test_edgelist(self):
     """Test _edgelist"""
     tsp = Tsp(self.graph)
     self.assertEqual(tsp._edgelist(self.result), [(0, 1), (1, 2), (2, 3), (3, 0)])
Beispiel #4
0
 def test_interpret(self):
     """Test interpret"""
     tsp = Tsp(self.graph)
     self.assertEqual(tsp.interpret(self.result), [0, 1, 2, 3])