-2

I am using Z3 to solve an optimization problem. the objective is to maximize the value of a variable, call it X, X is the summation of:

X = x1+x2+x3+x4+...+xi

each term form x1 to xi represents a non-linear equation. So, I can't use the optimization APIs. Instead, I first get a value for X and begin a loop. in each iteration, I add another constraint to get X greater than the previous generated X value.

I noticed that the first value is the maximum value and in each time the program enters the loop, I wait for a long long time to get another greater value but it never generates new values. I changed the values of the input and this happens in each time.

is that a coincidence? or Is the Z3 designed such that it generates the max. values for such formulas?

Rehab11
  • 483
  • 2
  • 7
  • 16
  • I would suggest to use a real nonlinear solver. What you are doing looks to me like a fishy hack. – Erwin Kalvelagen May 09 '18 at 05:18
  • Isn't z3 a real nonlinear solver? What I am doing is a workaround to maximize the value cause Z3 doesn't support nonlinear optimization. – Rehab11 May 09 '18 at 13:24

1 Answers1

1

Z3 doesn't really do non-linear optimization: Depending on the heuristics it uses, it may or may not give you an answer. (Most likely it'll either say unknown or run forever.) The hack you're implementing is likely the best you can get if you have truly non-linear constraints and you're not getting any mileage from z3 out-of-the-box. Another option would be to use strategies/tactics to guide the solver, but that is not for the faint of the heart and is not guaranteed to work.

See here for the original optimization z3 paper, which clearly states it is for the linear fragment: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/nbjorner-nuz.pdf

For a good read on strategies in z3, see: http://www.cs.tau.ac.il/~msagiv/courses/asv/z3py/strategies-examples.htm

alias
  • 28,120
  • 2
  • 23
  • 40
  • I understand that Z3 doesn't do nonlinear optimization. so I tried this workaround. what I don't understand is that Z3 gives me only one solution in the first iteration of the loop, then it never returns from the "solver.check()" function in the following iteration although there are multiple satisfiable solution. (I am using the C++ APIs). I believe that the problem is in the "sum()" expression but can't figure out what is the problem. – Rehab11 May 15 '18 at 00:55
  • If you are using the solver in incremental mode at each iteration, that could be the culprit. In that mode, Z3 uses a "less-powerful" engine. So, each time you go around the loop, start the solver from fresh and add all the formulas. See this answer for details: https://stackoverflow.com/a/50298033/936310 – alias May 15 '18 at 03:38
  • I tried that but unfortunately the problem still exists. It does not exist when i try to maximize other expression than the summation expression. – Rehab11 May 15 '18 at 18:44