ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 13 Dec 2019 10:00:47 +0100Problem with optimization and the minimize_constrained functionhttps://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/ Hello everyone. Im currently working on a project of mine with sagemath.
Basiaclly what im trying to optimize is a non-linear function under some constraints.
My function to maximize is:
N(x,y) = ((10- 2*x + y)/(1.25 + y)) * ((20 - 2*x + y)/(1.25 + y))
where the constants in this function are user inputs and x,y the variables to optimize under the following constraints:
(y - 0.003) + 2 * (x - 0.03) < 1.25,
x > 0,
y > 0
I tried defining the constraints as c1 to c3 and inputting them like this:
minimize_constrained(N_cells, [c_1,c_2,c_3],[0,0])
But unfortunately it gives me a Not Implemented Error and im stuck.Tue, 10 Dec 2019 13:16:28 +0100https://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/Answer by Juanjo for <p>Hello everyone. Im currently working on a project of mine with sagemath.
Basiaclly what im trying to optimize is a non-linear function under some constraints.</p>
<p>My function to maximize is:</p>
<p>N(x,y) = ((10- 2<em>x + y)/(1.25 + y)) * ((20 - 2</em>x + y)/(1.25 + y))</p>
<p>where the constants in this function are user inputs and x,y the variables to optimize under the following constraints:</p>
<p>(y - 0.003) + 2 * (x - 0.03) < 1.25,</p>
<p>x > 0,</p>
<p>y > 0</p>
<p>I tried defining the constraints as c1 to c3 and inputting them like this:
minimize_constrained(N_cells, [c_1,c_2,c_3],[0,0])
But unfortunately it gives me a Not Implemented Error and im stuck.</p>
https://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/?answer=49018#post-id-49018The following code works for me:
var("x,y")
N(x,y) = ((10-2*x+y)/(1.25 + y)) * ((20-2*x+y)/(1.25+y))
c1(x,y) = 1.25 - (y-0.003) - 2*(x-0.03)
c2(x,y) = x
c3(x,y) = y
minimize_constrained(N(x,y), [c1(x,y),c2(x,y),c3(x,y)], [0,0])
Alternatively:
N = lambda x: ((10-2*x[0]+x[1])/(1.25+x[1])) * ((20-2*x[0]+x[1])/(1.25+x[1]))
c1 = lambda x: 1.25 - (x[1]-0.003) - 2*(x[0]-0.03)
c2 = lambda x: x[0]
c3 = lambda x: x[1]
minimize_constrained(N, [c1,c2,c3], [0,0])Thu, 12 Dec 2019 19:04:10 +0100https://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/?answer=49018#post-id-49018Comment by Jens for <p>The following code works for me:</p>
<pre><code>var("x,y")
N(x,y) = ((10-2*x+y)/(1.25 + y)) * ((20-2*x+y)/(1.25+y))
c1(x,y) = 1.25 - (y-0.003) - 2*(x-0.03)
c2(x,y) = x
c3(x,y) = y
minimize_constrained(N(x,y), [c1(x,y),c2(x,y),c3(x,y)], [0,0])
</code></pre>
<p>Alternatively:</p>
<pre><code>N = lambda x: ((10-2*x[0]+x[1])/(1.25+x[1])) * ((20-2*x[0]+x[1])/(1.25+x[1]))
c1 = lambda x: 1.25 - (x[1]-0.003) - 2*(x[0]-0.03)
c2 = lambda x: x[0]
c3 = lambda x: x[1]
minimize_constrained(N, [c1,c2,c3], [0,0])
</code></pre>
https://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/?comment=49022#post-id-49022Thank you very much, it worked :)Fri, 13 Dec 2019 10:00:47 +0100https://ask.sagemath.org/question/48986/problem-with-optimization-and-the-minimize_constrained-function/?comment=49022#post-id-49022