0

I have to get the value (in a range of them) that gives the maximum of a function, using z3py. For example: I have a function (foo(x,y) = 2*x + 1 + y), and I want to find the value of [x,y] (using the constraint lo <= foo(x,y) <= hi) such that foo(x,y) is maximum. In other words I have to solve a maximum problem within a range, is that possible?

I have wrote this simple script in python:

    s = Solver()
    # hi is given by the user
    # expr is foo(x)
    s.add(expr <= hi)
    s.add(expr >= lo)
    s.add(X >= 0)
    s.add(Y >= 0)

    if  s.check() == sat:
        print('_upper_bound: got model %s' % s.model())

If I use only a variable (e.g., foo(x) = 2*x + 1), the given model is correct and the value of foo(x) is the highest. If I use more than one variable (e.g., foo(x,y) = 2*x+y) the found result is the minimum.

All the points in the range are in the domain of the function.

EDIT: Apparently, if I remove the two constraints about X and Y, I get the maximum value.

badnack
  • 737
  • 1
  • 11
  • 20

1 Answers1

2

using scipy, you can minimize (you maximize -f if you minimize f)

scipy.optimize.minimize(fun, x0, args=(), method='BFGS', jac=None, 
    hess=None, hessp=None, bounds=None, constraints=(), tol=None, 
    callback=None, options=None)

as you see, you have a bounds parameter,

bounds : sequence, optional Bounds for variables (only for L-BFGS-B, TNC, COBYLA and SLSQP). (min, max) pairs for each element in x, defining the bounds on that parameter. Use None for one of min or max when there is no bound in that direction.

More detail on the optimization routines to be found here.

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • Your solution is really good, but unfortunately I have just discovered that I have to work with z3py, since I have to deal with BitVectors. – badnack Sep 26 '13 at 01:37