3

To find x that maximises (c + x) / 2 subject to 0 <= x <= c <= 1 is mathematically trivial: the solution is x = c, whatever the value of c.

How does one express that in Python?

  • I can solve equations with inequality constraints e.g. using scipy.optimize.linprog, but I have difficulty to understand how I can formulate the problem in such a way that the answer will be x = c when c has no fixed numeric value.
  • Perhaps a symbolic toolbox such as sympy is suited for the task, but, again, how do I formulate this?

The above is a simple example equation. I have many of such equations like 1 - (x + c) / 2 and so on. They are all linear. There are also equations with more unknown values such as c1 and c2. I can formulate them easily, but the question is the same every time: How do I let Python solve (maximise/minimise) such equations?


Try 1, scipy.optimize.linprog:

from scipy.optimize import linprog
f = [-.5, -.5, 0]  # columns: c, x, 1
A_ub = [[0,-1,0], [-1,1,0], [1,0,-1]]
b_ub = [0, 0, 0]
bounds = ((0, 1), (0, 1), (1, 1))
res = linprog(f, A_ub, b_ub, bounds=bounds)

How does the value of res.x (which is [1., 1., 1.]) imply x = c?

Try 2, sympy

from sympy import symbols, solve, Max
x, c = symbols('x,c')
exp = (c + x) / 2
res = solve([Max(exp), 0 <= x, x <= c, c <= 1], x)

The answer is: (0 <= x) & (c <= 1) & (x <= c) & (-oo < c) & (-oo < x) & (x < oo) & Eq(x, -c) which raises more questions than it answers.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Paul
  • 766
  • 9
  • 28
  • maybe you`ll find something helpful in the answers to [this question](https://stackoverflow.com/questions/10499941/how-can-i-solve-equations-in-python). Note that i'm not suggesting that this is a duplicate, just pointing it out – Flob Mar 04 '19 at 11:44
  • Wrt the question itself, note this expression is trivial because it's linear. In this case, if you want to maximize the value within some bounds you can always pick the value at both bounds and take the maximum. However, non-linear expressions become much more complicated, and the ability to solve it depends very much on the specific expression. – jdehesa Mar 04 '19 at 11:50
  • @jdehesa It would be really nice if you could post a simple piece of demo code as answer. In all my cases the equations are linear but I still have no clue about a principled way of querying for the solutions, because the bound `x = c` has no numerical solution. – Paul Mar 04 '19 at 12:10
  • About your attempt with SymPy, `Max` is just the maximum of the passed arguments, which here is just `exp` so it is the same as `exp`. `solve` will try to solve the given equations and inequations; in this case it assumes `Max(exp)` (which is `exp`) should be `0`, and then there are the other inequations. The result just tells you when all of those are true (the important bit at the end, `Eq(x, -c)`). – jdehesa Mar 04 '19 at 12:14
  • @jdehesa, thanks that helps a little bit. You are right `Eq(x, -c)` is the answer, but I don't understand it. `solve(exp, x)` would ask for `x` such that `exp` equals 0, but I want to maximize it instead. (and what do the silly `(-oo < c) & (-oo < x) & (x < oo)` mean??? we already specified the bounds to be 0 and 1) – Paul Mar 04 '19 at 12:22
  • 1
    @Paul Since the question has been put on hold, here is what I meant about "picking the bound with the highest value" in SymPy https://pastebin.com/bbQQUbDX – jdehesa Mar 04 '19 at 12:30

1 Answers1

0

Considering x and c are both integers. For any given c, the solution will be:

d = {x: (c+x)/2 for x in range(0,c+1)}   
max(d, key=d.get)
Alejandro Blasco
  • 1,295
  • 2
  • 20
  • 24
  • The point @TrebuchetMS was trying to make is that neither `x` nor `c` is an integer. – Paul Mar 04 '19 at 12:06
  • @Paul Ok, now I undestand what you ask. I gave you a numerical solution. But I don't think Python is good for analytical solutions, which is actually what you're asking. – Alejandro Blasco Mar 04 '19 at 12:20