CONOPT
Loading...
Searching...
No Matches
qp1.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import conopt as co
12
13sys.path.append('../common/')
14import std
15
16
17class QPModelData(co.ModelData):
18 def __init__(self):
19 self.NN = 1000
20 self.NQ = self.NN * 2 - 1
21 # only the lower triangle of the Q matrix is stored, since it is
22 # symmetric. The diagonal has the value 10. While the first off diagonal
23 # has the value 0.1. Note, the indices for the lower diagonal is (i + 1,
24 # i)
25 self.target = [10] * self.NN
26 self.Qdiag = [1] * self.NN
27 self.Qlowerdiag = [0.1] * (self.NN - 1)
28
29 super().__init__()
30
31 def buildModel(self):
32 """
33 adding the variables and constraints to the model
34 @ingroup PYTHON1THREAD_QP1
35 """
36 # adding the variables to the model
37 for i in range(self.NN):
38 self.addVariable(0.0, co.Conopt.Infinity)
39
40 # adding the constraints to the model
41 # the first constraint is the quadratic objective
42 varidx = list(range(self.NN))
43 zeros = [0] * self.NN
44 ones = [1] * self.NN
45 self.addConstraint(co.ConstraintType_Free, 0.0, varidx, zeros, ones)
46
47 # the second constraint is the summation constraint: sum(x) = 1
48 self.addConstraint(co.ConstraintType_Eq, 1.0, varidx, ones, zeros)
49
50 # setting the objective constraint
51 self.setObjectiveElement(co.ObjectiveElement_Constraint, 0)
52
53 # setting the optimisation direction
54 self.setOptimizationSense(co.Sense_Minimize)
55
56 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
57 """
58 @copydoc conopt.ModelData.evaluateNonlinearTerm
59 @ingroup PYTHON1THREAD_QP1
60 """
61 # only the objective function is nonlinear, so this function will only be
62 # called when rowno == 0
63 g = 0
64 if rowno == 0:
65 g += sum(
66 [
67 (x[i] - self.target[i]) * q * (x[i] - self.target[i])
68 for i, q in enumerate(self.Qdiag)
69 ]
70 )
71 g += 2 * sum(
72 [
73 (x[i + 1] - self.target[i + 1]) * q * (x[i] - self.target[i])
74 for i, q in enumerate(self.Qlowerdiag)
75 ]
76 )
77
78 return g / 2
79
80 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
81 """
82 @copydoc conopt.ModelData.evaluateNonlinearJacobian
83 @ingroup PYTHON1THREAD_QP1
84 """
85 jac = [0] * self.NN
86 if rowno == 0:
87 for i in range(self.NN):
88 jac[i] += self.Qdiag[i] * (x[i] - self.target[i])
89 if i < self.NN - 1:
90 jac[i + 1] += self.Qlowerdiag[i] * (x[i] - self.target[i])
91 jac[i] += self.Qlowerdiag[i] * (x[i + 1] - self.target[i + 1])
92
93 return jac
94
95
96if __name__ == '__main__':
97 name = os.path.basename(__file__)[:-3]
98
99 conopt = co.Conopt(name)
100 model = QPModelData()
101 msghdlr = std.TutMessageHandler(name)
102
103 model.buildModel()
104
105 conopt.loadModel(model)
106 conopt.setMessageHandler(msghdlr)
107
108 # getting the license variables
109 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
110 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
111 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
112 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
113 if (
114 license_int_1 is not None
115 and license_int_2 is not None
116 and license_int_3 is not None
117 and license_text is not None
118 ):
119 conopt.setLicense(
120 int(license_int_1),
121 int(license_int_2),
122 int(license_int_3),
123 license_text,
124 )
125
126 coi_error = conopt.solve()
127
128 retcode = std.checkSolve(conopt, 59978.0, coi_error, 0.001)
129
130 sys.exit(retcode)
list target
Definition qp1.py:25
list Qdiag
Definition qp1.py:26
list Qlowerdiag
Definition qp1.py:27
__init__(self)
Definition qp1.py:18
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition qp1.py:80
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp1.py:56
buildModel(self)
adding the variables and constraints to the model
Definition qp1.py:31