CONOPT
Loading...
Searching...
No Matches
tutorial.py
Go to the documentation of this file.
7
8import sys
9import os
10
11import pyconopt
12
13sys.path.append('../common/')
14import std
15
17 def __init__(self):
18 self.Al = 0.16
19 self.Ak = 2.0
20 self.Ainp = 0.16
21 self.Rho = 1.0
22 self.K = 4.0
23 super().__init__()
24
25 def buildModel(self):
26 """
27 adding the variables and constraints to the model
28 @ingroup PYTHON1THREAD_TUTORIAL
29 """
30 # adding the variables to the model
31 self.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
32 self.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
33 self.addVariable(0.0, pyconopt.CONOPT_INF)
34 self.addVariable(0.0, pyconopt.CONOPT_INF)
35
36 # adding the constraints to the model
37 self.addConstraint(pyconopt.ConstraintType_Free, -0.1, [0, 1, 2, 3],
38 [-1, -1, 0, 0], [0, 0, 1, 1])
39 self.addConstraint(pyconopt.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
40 [1, 1, 0])
41 self.addConstraint(pyconopt.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
42 [0, 0])
43
44 # setting the objective constraint
45 self.setObjectiveElement(pyconopt.ObjectiveElement_Constraint, 0)
46
47 # setting the optimisation direction
48 self.setOptimizationSense(pyconopt.Sense_Maximize)
49
50
51 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
52 """
53 @ingroup PYTHON1THREAD_TUTORIAL
54 """
55 L = x[0]
56 Inp = x[1]
57 Out = x[2]
58 P = x[3]
59
60 g = 0
61 if rowno == 0:
62 g = P * Out
63 elif rowno == 1:
64 hold1 = (self.Al*pow(L,(-self.Rho)) + self.Ak*pow(self.K,(-self.Rho)) + self.Ainp*pow(Inp,(-self.Rho)))
65 hold2 = pow(hold1,( -1./self.Rho ))
66
67 g = hold2
68
69 return g
70
71 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
72 """
73 @ingroup PYTHON1THREAD_TUTORIAL
74 """
75 L = x[0]
76 Inp = x[1]
77 Out = x[2]
78 P = x[3]
79
80 jac = []
81 if rowno == 0:
82 jac.append(P)
83 jac.append(Out)
84 elif rowno == 1:
85 hold1 = (self.Al*pow(L,(-self.Rho)) + self.Ak*pow(self.K,(-self.Rho)) + self.Ainp*pow(Inp,(-self.Rho)))
86 hold2 = pow(hold1,( -1./self.Rho ))
87 hold3 = hold2 / hold1
88
89 jac.append(hold3 * self.Al * pow(L ,(-self.Rho-1.)))
90 jac.append(hold3 * self.Ainp * pow(Inp,(-self.Rho-1.)))
91
92 return jac
93
94if __name__ == "__main__":
95 name = os.path.basename(__file__)[:-3]
96
97 conopt = pyconopt.Conopt(name)
98 model = TutModelData()
99 msghdlr = std.TutMessageHandler(name)
100
101 model.buildModel()
102
103 conopt.loadModel(model)
104 conopt.setMessageHandler(msghdlr)
105
106 # getting the license variables
107 license_int_1 = os.environ.get('LICENSE_INT_1', None)
108 license_int_2 = os.environ.get('LICENSE_INT_2', None)
109 license_int_3 = os.environ.get('LICENSE_INT_3', None)
110 license_text = os.environ.get('LICENSE_TEXT', None)
111 if license_int_1 is not None and license_int_2 is not None \
112 and license_int_3 is not None and license_text is not None:
113 conopt.setLicense(int(license_int_1), int(license_int_2),
114 int(license_int_3), license_text)
115
116 coi_error = conopt.solve()
117
118 retcode = std.checkSolve(conopt, 0.572943, coi_error)
119
120 sys.exit(retcode)
setOptimizationSense(self, sense)
sets the optimisation direction.
Definition pyconopt.py:2119
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
Definition pyconopt.py:2111
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition pyconopt.py:2052
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition pyconopt.py:2011
The Conopt class.
Definition pyconopt.py:1416
A class that can be extended to build and solve a model using Conopt.
Definition pyconopt.py:2391
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutorial.py:51
buildModel(self)
adding the variables and constraints to the model
Definition tutorial.py:25
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition tutorial.py:71