root/PythonPackages/OpenOpt/openopt/examples/minlp_1.py

Revision 858, 1.4 kB (checked in by dmitrey, 1 year ago)

minor changes

Line  
1 """
2 Example of MINLP
3 It is recommended to read help(NLP) before
4 and /examples/nlp_1.py
5 """
6 from openopt import MINLP
7 from numpy import *
8 N = 150
9 K = 50
10
11 #objective function:
12 f = lambda x: ((x-5.45)**2).sum()
13
14 #optional: 1st derivatives
15 df = lambda x: 2*(x-5.45)
16
17 # start point
18 x0 = 8*cos(arange(N))
19
20 # assign prob:
21 # 1st arg - objective function
22 # 2nd arg - start point
23 # for more details see
24 # http://openopt.org/Assignment
25 p = MINLP(f, x0, df=df, maxIter = 1e3)
26
27 # optional: set some box constraints lb <= x <= ub
28 p.lb = [-6.5]*N
29 p.ub = [6.5]*N
30 # see help(NLP) for handling of other constraints:
31 # Ax<=b, Aeq x = beq, c(x) <= 0, h(x) = 0
32 # see also /examples/nlp_1.py
33
34 # required tolerance for smooth constraints, default 1e-6
35 p.contol = 1.1e-6
36
37 p.name = 'minlp_1'
38
39 # required field: nlpSolver - should be capable of handling box-bounds at least
40 #nlpSolver = 'ralg'
41 nlpSolver = 'ipopt'
42
43 # coords of discrete variables and sets of allowed values
44 p.discreteVars = {7:range(3, 10), 8:range(3, 10), 9:[2, 3.1, 9]}
45
46 # required tolerance for discrete variables, default 10^-5
47 p.discrtol = 1.1e-5
48
49 #optional: check derivatives, you could use p.checkdc(), p.checkdh() for constraints
50 #p.checkdf()
51
52 # optional: maxTime, maxCPUTime
53 # p.maxTime = 15
54 # p.maxCPUTime = 15
55
56 r = p.solve('branb', nlpSolver=nlpSolver, plot = False)
57 # optim point and value are r.xf and r.ff,
58 # see http://openopt.org/OOFrameworkDoc#Result_structure for more details
Note: See TracBrowser for help on using the browser.