11#!/usr/bin/env python
22
33import unittest
4- import numbers , fractions
4+ import numbers
5+ import fractions
56
67import qsoptex
78
9+ from six import b , u
10+
11+
812class TestVariableType (unittest .TestCase ):
913 def test_variable_str (self ):
10- ''' Test that a variable of bytes type works'''
14+ """ Test that a variable of bytes type works"""
1115 self ._problem = qsoptex .ExactProblem ()
12- self ._problem .add_variable (name = b'x' )
16+ self ._problem .add_variable (name = b ( 'x' ) )
1317
1418 def test_variable_unicode (self ):
15- ''' Test that a variable of unicode type works'''
19+ """ Test that a variable of unicode type works"""
1620 self ._problem = qsoptex .ExactProblem ()
17- self ._problem .add_variable (name = u'x' )
21+ self ._problem .add_variable (name = u ('x' ))
22+
1823
1924class TestSmallExactProblem (unittest .TestCase ):
2025 def setUp (self ):
2126 self ._problem = qsoptex .ExactProblem ()
22- self ._problem .add_variable (name = 'x' , objective = 2 , lower = 3.5 , upper = 17.5 )
23- self ._problem .add_variable (name = 'y' , objective = - 1 , lower = None , upper = 2 )
24- self ._problem .add_linear_constraint (qsoptex .ConstraintSense .EQUAL , {'x' : 1 , 'y' : 1 }, rhs = 0 )
27+ self ._problem .add_variable (
28+ name = 'x' , objective = 2 , lower = 3.5 , upper = 17.5 )
29+ self ._problem .add_variable (
30+ name = 'y' , objective = - 1 , lower = None , upper = 2 )
31+ self ._problem .add_linear_constraint (
32+ qsoptex .ConstraintSense .EQUAL , {'x' : 1 , 'y' : 1 }, rhs = 0 , name = 'c1' )
2533 self ._problem .set_objective_sense (qsoptex .ObjectiveSense .MAXIMIZE )
2634
2735 def test_reaches_status_optimal (self ):
@@ -30,12 +38,14 @@ def test_reaches_status_optimal(self):
3038
3139 def test_reaches_status_infeasible (self ):
3240 self ._problem .add_variable (name = 'z' , lower = 200 , upper = None )
33- self ._problem .add_linear_constraint (qsoptex .ConstraintSense .GREATER , {'y' : 1 , 'z' : - 1 }, rhs = 0 )
41+ self ._problem .add_linear_constraint (
42+ qsoptex .ConstraintSense .GREATER , {'y' : 1 , 'z' : - 1 }, rhs = 0 )
3443 status = self ._problem .solve ()
3544 self .assertEqual (status , qsoptex .SolutionStatus .INFEASIBLE )
3645
3746 def test_reaches_status_unbounded (self ):
38- self ._problem .add_variable (name = 'z' , lower = 200 , upper = None , objective = 1 )
47+ self ._problem .add_variable (
48+ name = 'z' , lower = 200 , upper = None , objective = 1 )
3949 status = self ._problem .solve ()
4050 self .assertEqual (status , qsoptex .SolutionStatus .UNBOUNDED )
4151
@@ -74,12 +84,12 @@ def test_solution_get_all_values_are_correct(self):
7484 def test_solution_value_index_negative (self ):
7585 self ._problem .solve ()
7686 with self .assertRaises (IndexError ):
77- x = self ._problem .get_value (- 1 )
87+ self ._problem .get_value (- 1 )
7888
7989 def test_solution_value_index_too_high (self ):
8090 self ._problem .solve ()
8191 with self .assertRaises (IndexError ):
82- x = self ._problem .get_value (10 )
92+ self ._problem .get_value (10 )
8393
8494 def test_get_status_method (self ):
8595 s = self ._problem .solve ()
@@ -96,10 +106,42 @@ def test_rerun_solve(self):
96106
97107 # Modified problem
98108 self ._problem .add_variable (name = 'z' , objective = 1 , lower = 0 , upper = 200 )
99- self ._problem .add_linear_constraint (qsoptex .ConstraintSense .LESS , {'z' : 1 , 'x' : - 10 }, rhs = - 50 )
109+ self ._problem .add_linear_constraint (
110+ qsoptex .ConstraintSense .LESS , {'z' : 1 , 'x' : - 10 }, rhs = - 50 )
100111 self ._problem .solve ()
101112
102- self .assertEqual (self ._problem .get_objective_value (), fractions .Fraction ('355/2' ))
113+ self .assertEqual (
114+ self ._problem .get_objective_value (), fractions .Fraction ('355/2' ))
115+
116+ def test_delete_variable (self ):
117+ self ._problem .add_variable (name = 'z' , objective = 1 , lower = 0 , upper = 1 )
118+ status = self ._problem .solve ()
119+ self .assertEqual (status , qsoptex .SolutionStatus .OPTIMAL )
120+ obj = self ._problem .get_objective_value ()
121+ self .assertEqual (obj , fractions .Fraction ('107/2' ))
122+
123+ self ._problem .delete_variable ('z' )
124+
125+ # Solve problem again without z
126+ status = self ._problem .solve ()
127+ self .assertEqual (status , qsoptex .SolutionStatus .OPTIMAL )
128+ obj = self ._problem .get_objective_value ()
129+ self .assertEqual (obj , fractions .Fraction ('105/2' ))
130+
131+ def test_delete_constraint (self ):
132+ self ._problem .add_linear_constraint (
133+ qsoptex .ConstraintSense .LESS , {'x' : 1 }, rhs = 15 , name = 'constr2' )
134+ status = self ._problem .solve ()
135+ self .assertEqual (status , qsoptex .SolutionStatus .OPTIMAL )
136+ self .assertEqual (self ._problem .get_value ('x' ), 15 )
137+
138+ self ._problem .delete_linear_constraint ('constr2' )
139+
140+ status = self ._problem .solve ()
141+ self .assertEqual (status , qsoptex .SolutionStatus .OPTIMAL )
142+ self .assertEqual (
143+ self ._problem .get_value ('x' ), fractions .Fraction ('35/2' ))
144+
103145
104146if __name__ == '__main__' :
105147 unittest .main ()
0 commit comments