Example #1
0
 def test_eval(self):
     """Tests ${expression}$ calls"""
     parser = PyMPL()
     parser.input = """
     s.t. con1: x + y <= ${abs((2**7)/5-135)}$ * z;
     var x1, >= ${2+6}$, <= ${10*5}$;
     """
     parser.parse(comment_cmds=False)
     self.assertIn("s.t. con1: x + y <= 110 * z;", parser.output)
     self.assertIn("var x1, >= 8, <= 50;", parser.output)
Example #2
0
 def test_stmt(self):
     """Tests $STMT{stmt} calls"""
     parser = PyMPL()
     parser.input = """
     $STMT{"s.t. con1: x + y <= {0} * z;".format(abs((2**7)/5-135))};
     $EXEC{stmt = "s.t. {0}: x >= 10;".format("test")};
     $STMT{stmt};
     """
     parser.parse(comment_cmds=False)
     self.assertIn("s.t. con1: x + y <= 110 * z;", parser.output)
     self.assertIn("s.t. test: x >= 10;", parser.output)
Example #3
0
 def test_var(self):
     """Tests $VAR[name]{typ, lb, ub} calls"""
     parser = PyMPL()
     parser.input = """
     $VAR[x]{"integer", 0, 10};
     $VAR[y]{"binary"};
     $VAR[z]{ub=abs((2**7)/5-135)};
     $VAR[^z]{"integer", 0, 10};
     $EXEC{VAR['y']("binary")};
     """
     parser.parse(comment_cmds=False)
     self.assertIn("var x, integer, >= 0, <= 10;", parser.output)
     self.assertIn("var y, binary;", parser.output)
     self.assertIn("var z, <= 110;", parser.output)
     self.assertNotIn("var ^z, integer, >= 0, <= 10;", parser.output)
     self.assertIn("var y, binary;", parser.output)
Example #4
0
 def test_set(self):
     """Tests $SET[name]{values} calls"""
     parser = PyMPL()
     parser.input = """
     $SET[A]{range(5)};
     $SET[B]{zip(range(5),range(5))};
     $SET[^C]{range(5)};
     """
     parser.parse(comment_cmds=False)
     self.assertIn("set A := {0,1,2,3,4};", parser.output)
     self.assertIn(
         "set B := {(0,0),(1,1),(2,2),(3,3),(4,4)};",
         parser.output
     )
     self.assertNotIn("set C := {0,1,2,3,4};", parser.output)
     self.assertNotIn("set ^C := {0,1,2,3,4};", parser.output)
Example #5
0
 def test_comments(self):
     """Tests valid comments"""
     parser = PyMPL()
     parser.input = """
     /* ... $SET[A]{range(5)}; ... */
     # $PARAM[VALUE]{10};
     # ... $PARAM[VALUE2]{10}; ...
     param a := "\\"/*";
     $PARAM[Y]{10};
     param b := "*/";
     """
     parser.parse(comment_cmds=True)
     self.assertNotIn("set A := {0,1,2,3,4};", parser.output)
     self.assertNotIn("param VALUE := 10;", parser.output)
     self.assertNotIn("param VALUE2 := 10;", parser.output)
     self.assertIn("param Y := 10;", parser.output)
     self.assertIn("# $PARAM[VALUE]{10};", parser.output)
     self.assertIn("/*EVALUATED:$PARAM[Y]{10};*/", parser.output)
Example #6
0
 def test_param(self):
     """Tests $PARAM[name]{value} calls"""
     parser = PyMPL()
     parser.input = """
     $PARAM[NAME]{"name"};
     $PARAM[VALUE]{10};
     $PARAM[D{I}]{{'a': 1, 'b': 2}};
     $PARAM[L0]{[1,2,3], i0=0};
     $PARAM[L1]{[1,2,3], i0=1};
     $PARAM[^NAME2]{"something"};
     """
     parser.parse(comment_cmds=False)
     self.assertIn("param NAME := 'name';", parser.output)
     self.assertIn("param VALUE := 10;", parser.output)
     self.assertIn("param D := ['a']1['b']2;", parser.output)
     self.assertIn("set I := {'a','b'};", parser.output)
     self.assertIn("param L0 := [0]1[1]2[2]3;", parser.output)
     self.assertIn("param L1 := [1]1[2]2[3]3;", parser.output)
     self.assertNotIn("param NAME2 := 'something';", parser.output)
     self.assertNotIn("param ^NAME2 := 'something';", parser.output)
Example #7
0
 def test_con(self):
     """Tests $CON[name]{lincomb, sign, rhs} calls"""
     parser = PyMPL()
     parser.input = """
     $VAR[x1]{}; $VAR[x2]{}; $VAR[x3]{};
     $CON[con1]{[("x1",5),("x2",15),("x3",10)],">=",20};
     $CON[con2]{[("x1",5)],">=",[("x2",-15),("x3",-10),20]};
     $CON[con3]{-20,">=",[("x1",-5),("x2",-15),("x3",-10)]};
     $CON[con4]{-20,">=",[(-5, "x1"),("x2",-15),(-10, "x3")]};
     $CON[con5]{[-20, "x1"],">=",[(-4, "x1"),("x2",-15),(-10, "x3")]};
     $CON[con6]{"x1",">=",[(-4, "x1"),20,("x2",-15),(-10, "x3")]};
     $CON[^xyz]{[("x1",5),("x2",15),("x3",10)],">=",20};
     """
     parser.parse(comment_cmds=False)
     self.assertIn("s.t. con1: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertIn("s.t. con2: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertIn("s.t. con3: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertIn("s.t. con4: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertIn("s.t. con5: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertIn("s.t. con6: +5*x1+15*x2+10*x3 >= 20;", parser.output)
     self.assertNotIn("s.t. xyz: +5*x1+15*x2+10*x3 >= 20;", parser.output)
Example #8
0
 def test_empty(self):
     """Tests empty files"""
     parser = PyMPL()
     parser.input = ""
     parser.parse(comment_cmds=False)
     self.assertEqual(parser.output, "")
Example #9
0
 def test_exceptions(self):
     """Tests if exceptions are thrown correctly"""
     parser = PyMPL()
     parser.input = """$EXEC{print 1/0};"""
     with self.assertRaises(ZeroDivisionError):
         parser.parse(comment_cmds=False)
     parser.input = """$SET[X]{0};"""
     with self.assertRaises(TypeError):
         parser.parse(comment_cmds=False)
     parser.input = """$FLOW[Z]{100, [10, 10]};"""
     with self.assertRaises(TypeError):
         parser.parse(comment_cmds=False)
     parser.input = """$FLOW[Z]{100, 10};"""
     with self.assertRaises(TypeError):
         parser.parse(comment_cmds=False)
     parser.input = """$SET[X]{};"""
     with self.assertRaises(TypeError):
         parser.parse(comment_cmds=False)
     parser.input = """$SET[X]{[1,2,3]};$SET[X]{[1,2]};"""
     with self.assertRaises(AssertionError):
         parser.parse(comment_cmds=False)
     parser.input = """$SET[2X]{[1,2,3]};"""
     with self.assertRaises(AssertionError):
         parser.parse(comment_cmds=False)