1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Sets options for CPLEX.
18 """
19
20 try:
21 from cplex import cplexoptimset
22 except ImportError:
23
24 pass
25
26 from pypower.util import feval
27
28
30 """Sets options for CPLEX.
31
32 Sets the values for the options dict normally passed to
33 C{cplexoptimset}.
34
35 Inputs are all optional, second argument must be either a string
36 (C{fname}) or a dict (C{ppopt}):
37
38 Output is an options dict to pass to C{cplexoptimset}.
39
40 Example:
41
42 If C{ppopt['CPLEX_OPT'] = 3}, then after setting the default CPLEX options,
43 CPLEX_OPTIONS will execute the following user-defined function
44 to allow option overrides::
45
46 opt = cplex_user_options_3(opt, ppopt)
47
48 The contents of cplex_user_options_3.py, could be something like::
49
50 def cplex_user_options_3(opt, ppopt):
51 opt = {}
52 opt['threads'] = 2
53 opt['simplex']['refactor'] = 1
54 opt['timelimit'] = 10000
55 return opt
56
57 For details on the available options, see the I{"Parameters Reference
58 Manual"} section of the CPLEX documentation at:
59 U{http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/}
60
61 @param overrides:
62 - dict containing values to override the defaults
63 - fname: name of user-supplied function called after default
64 options are set to modify them. Calling syntax is::
65
66 modified_opt = fname(default_opt)
67
68 @param ppopt: PYPOWER options vector, uses the following entries:
69 - OPF_VIOLATION - used to set opt.simplex.tolerances.feasibility
70 - VERBOSE - used to set opt.barrier.display,
71 opt.conflict.display, opt.mip.display, opt.sifting.display,
72 opt.simplex.display, opt.tune.display
73 - CPLEX_LPMETHOD - used to set opt.lpmethod
74 - CPLEX_QPMETHOD - used to set opt.qpmethod
75 - CPLEX_OPT - user option file, if ppopt['CPLEX_OPT'] is non-zero
76 non-zero it is appended to 'cplex_user_options_' to form
77 the name of a user-supplied function used as C{fname}
78 described above, except with calling syntax::
79
80 modified_opt = fname(default_opt, ppopt)
81
82 @see: C{cplexlp}, C{cplexqp}, L{ppoption}.
83
84 @author: Ray Zimmerman (PSERC Cornell)
85 @author: Richard Lincoln
86 """
87
88
89 verbose = 1
90 feastol = 1e-6
91 fname = ''
92
93
94 if ppopt != None:
95 if isinstance(ppopt, basestring):
96 fname = ppopt
97 have_ppopt = False
98 else:
99 have_ppopt = True
100
101 feastol = ppopt['OPF_VIOLATION'] / 5
102 verbose = ppopt['VERBOSE']
103 lpmethod = ppopt['CPLEX_LPMETHOD']
104 qpmethod = ppopt['CPLEX_QPMETHOD']
105 if ppopt['CPLEX_OPT']:
106 fname = 'cplex_user_options_#d' % ppopt['CPLEX_OPT']
107 else:
108 have_ppopt = False
109
110
111 opt = cplexoptimset('cplex')
112 opt['simplex']['tolerances']['feasibility'] = feastol
113
114
115 vrb = max([0, verbose - 1])
116 opt['barrier']['display'] = vrb
117 opt['conflict']['display'] = vrb
118 opt['mip']['display'] = vrb
119 opt['sifting']['display'] = vrb
120 opt['simplex']['display'] = vrb
121 opt['tune']['display'] = vrb
122
123
124 if have_ppopt:
125 opt['lpmethod'] = ppopt['CPLEX_LPMETHOD']
126 opt['qpmethod'] = ppopt['CPLEX_QPMETHOD']
127
128
129
130
131
132 if len(fname) > 0:
133 if have_ppopt:
134 opt = feval(fname, opt, ppopt)
135 else:
136 opt = feval(fname, opt)
137
138
139
140 if overrides is not None:
141 names = overrides.keys()
142 for k in range(len(names)):
143 if isinstance(overrides[names[k]], dict):
144 names2 = overrides[names[k]].keys()
145 for k2 in range(len(names2)):
146 if isinstance(overrides[names[k]][names2[k2]], dict):
147 names3 = overrides[names[k]][names2[k2]].keys()
148 for k3 in range(len(names3)):
149 opt[names[k]][names2[k2]][names3[k3]] = overrides[names[k]][names2[k2]][names3[k3]]
150 else:
151 opt[names[k]][names2[k2]] = overrides[names[k]][names2[k2]]
152 else:
153 opt[names[k]] = overrides[names[k]]
154
155 return opt
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320