CONOPT
Loading...
Searching...
No Matches
square.py
Go to the documentation of this file.
7
8import sys
9import os
10
11import conopt as co
12
13sys.path.append('../common/')
14import std
15
16
17class SquareModelData(co.ModelData):
18 def __init__(self):
19 super().__init__()
20
21 def buildModel(self):
22 """
23 adding the variables and constraints to the model
24 @ingroup PYTHON1THREAD_SQUARE
25 """
26 # Model x0 + x1 = 10
27 # x0 - x1 = 0
28 # adding the variables to the model
29 self.x0 = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 0.0)
30 self.x1 = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity, 0.0)
31
32 # adding the constraints to the model
33 self.cons1 = self.addConstraint(
34 co.ConstraintType_Eq, 10.0, [self.x0, self.x1], [1, 1], [0, 0]
35 )
36 self.cons2 = self.addConstraint(
37 co.ConstraintType_Eq, 0.0, [self.x0, self.x1], [0, 0], [1, 1]
38 )
39
40 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
41 """
42 @copydoc conopt.ModelData.evaluateNonlinearTerm
43 @ingroup PYTHON1THREAD_SQUARE
44 """
45 g = 0
46 if rowno == self.cons1:
47 pass
48
49 elif rowno == self.cons2:
50 g = x[self.x0] - x[self.x1]
51
52 return g
53
54 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
55 """
56 @copydoc conopt.ModelData.evaluateNonlinearJacobian
57 @ingroup PYTHON1THREAD_SQUARE
58 """
59 jac = []
60 if rowno == self.cons1:
61 pass
62
63 elif rowno == self.cons2:
64 jac.append(1)
65 jac.append(-1)
66
67 return jac
68
69
70if __name__ == '__main__':
71 name = os.path.basename(__file__)[:-3]
72
73 conopt = co.Conopt(name)
74 model = SquareModelData()
75 msghdlr = std.TutMessageHandler(name)
76
77 model.buildModel()
78
79 conopt.loadModel(model)
80 conopt.setMessageHandler(msghdlr)
81
82 # tell conopt this is a square system
83 conopt.squareModel(1)
84
85 # getting the license variables
86 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
87 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
88 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
89 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
90 if (
91 license_int_1 is not None
92 and license_int_2 is not None
93 and license_int_3 is not None
94 and license_text is not None
95 ):
96 conopt.setLicense(
97 int(license_int_1),
98 int(license_int_2),
99 int(license_int_3),
100 license_text,
101 )
102
103 coi_error = conopt.solve()
104
105 if coi_error:
106 std.python_log(conopt, 'Errors encountered during solution')
107 elif (conopt.modelStatus() != 16) or (conopt.solutionStatus() != 1):
108 std.python_log(conopt, 'Solver or Model status not as expected (1,16)')
109
110 # printing the final status of the optimisation
111 conopt.printStatus()
112
113 std.python_log(conopt, 'Successful Solve')
buildModel(self)
adding the variables and constraints to the model
Definition square.py:21
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition square.py:54
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition square.py:40