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, 06 Apr 2012 13:25:44 +0200error using minimize_constrainedhttps://ask.sagemath.org/question/8861/error-using-minimize_constrained/When I use minimize_constrained(), I get the following error:
File "", line 1, in <module>
File "/tmp/tmp81ftcX/___code___.py", line 72, in <module>
minimize_constrained(T, [_sage_const_1 ], nil)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/numerical/optimize.py", line 415, in minimize_constrained
return vector(RDF,min)
UnboundLocalError: local variable 'min' referenced before assignment
It comes from the following piece of code:
K = matrix(K) # matrix of size (3*N+1)*(3*N+1)
des = vector(des) # vector of size 3*N+1
u = vector([var("u%02d" % k) for k in [0..3*N]]) # size 3*N+1
def LHS(X):
Y = 0.5*K*X*X - vector(des)*X
return Y
nul = [0 for j in range(0,3*N+1)]
minimize_constrained(LHS(u), [u02 -1], nil)
I don't understand why I keep getting the error for above function call (I know about global/local variables in python function calls, but definition of vector "u" makes it's elements global variables.)Fri, 06 Apr 2012 10:56:27 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/Comment by kcrisman for <p>When I use minimize_constrained(), I get the following error:</p>
<pre><code>File "", line 1, in <module>
File "/tmp/tmp81ftcX/___code___.py", line 72, in <module>
minimize_constrained(T, [_sage_const_1 ], nil)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/numerical/optimize.py", line 415, in minimize_constrained
return vector(RDF,min)
UnboundLocalError: local variable 'min' referenced before assignment
</code></pre>
<p>It comes from the following piece of code:</p>
<pre><code>K = matrix(K) # matrix of size (3*N+1)*(3*N+1)
des = vector(des) # vector of size 3*N+1
u = vector([var("u%02d" % k) for k in [0..3*N]]) # size 3*N+1
def LHS(X):
Y = 0.5*K*X*X - vector(des)*X
return Y
nul = [0 for j in range(0,3*N+1)]
minimize_constrained(LHS(u), [u02 -1], nil)
</code></pre>
<p>I don't understand why I keep getting the error for above function call (I know about global/local variables in python function calls, but definition of vector "u" makes it's elements global variables.)</p>
https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19981#post-id-19981If you could post the *complete* code, that would be helpful. Currently `K` and `des` are undefined. Fri, 06 Apr 2012 11:25:41 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19981#post-id-19981Comment by bennison for <p>When I use minimize_constrained(), I get the following error:</p>
<pre><code>File "", line 1, in <module>
File "/tmp/tmp81ftcX/___code___.py", line 72, in <module>
minimize_constrained(T, [_sage_const_1 ], nil)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/numerical/optimize.py", line 415, in minimize_constrained
return vector(RDF,min)
UnboundLocalError: local variable 'min' referenced before assignment
</code></pre>
<p>It comes from the following piece of code:</p>
<pre><code>K = matrix(K) # matrix of size (3*N+1)*(3*N+1)
des = vector(des) # vector of size 3*N+1
u = vector([var("u%02d" % k) for k in [0..3*N]]) # size 3*N+1
def LHS(X):
Y = 0.5*K*X*X - vector(des)*X
return Y
nul = [0 for j in range(0,3*N+1)]
minimize_constrained(LHS(u), [u02 -1], nil)
</code></pre>
<p>I don't understand why I keep getting the error for above function call (I know about global/local variables in python function calls, but definition of vector "u" makes it's elements global variables.)</p>
https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19978#post-id-19978K and des were not causing any problems (they are stiffness matrix and right hand side of finite elements method) since even with zero matrix and zero vector I would get the same error.Fri, 06 Apr 2012 12:25:21 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19978#post-id-19978Answer by kcrisman for <p>When I use minimize_constrained(), I get the following error:</p>
<pre><code>File "", line 1, in <module>
File "/tmp/tmp81ftcX/___code___.py", line 72, in <module>
minimize_constrained(T, [_sage_const_1 ], nil)
File "/sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/numerical/optimize.py", line 415, in minimize_constrained
return vector(RDF,min)
UnboundLocalError: local variable 'min' referenced before assignment
</code></pre>
<p>It comes from the following piece of code:</p>
<pre><code>K = matrix(K) # matrix of size (3*N+1)*(3*N+1)
des = vector(des) # vector of size 3*N+1
u = vector([var("u%02d" % k) for k in [0..3*N]]) # size 3*N+1
def LHS(X):
Y = 0.5*K*X*X - vector(des)*X
return Y
nul = [0 for j in range(0,3*N+1)]
minimize_constrained(LHS(u), [u02 -1], nil)
</code></pre>
<p>I don't understand why I keep getting the error for above function call (I know about global/local variables in python function calls, but definition of vector "u" makes it's elements global variables.)</p>
https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?answer=13429#post-id-13429I think I know what happened. I don't know if it should be considered user error or a bug.
Look at [the code in question](http://hg.sagemath.org/sage-main/file/c239be1054e0/sage/numerical/optimize.py#l398).
sage: [u02-1]
[u02 - 1]
sage: isinstance(u02-1,tuple)
False
sage: isinstance(u02-1,list)
False
sage: function_type=type(lambda x,y: x+y)
sage: function_type
<type 'function'>
sage: isinstance(u02-1,function_type)
False
And clearly it's not `None`, so the if/elif never actually gives `min` a value. Also notice that all the examples use lambda functions for their `cons` values.
Since `cons` "This should be either a function or list of functions that must be positive", perhaps just having a symbolic expression is not enough. After all, who knows what the variable is to be? But maybe we should have a better error check for this?Fri, 06 Apr 2012 11:30:51 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?answer=13429#post-id-13429Comment by bennison for <p>I think I know what happened. I don't know if it should be considered user error or a bug.</p>
<p>Look at <a href="http://hg.sagemath.org/sage-main/file/c239be1054e0/sage/numerical/optimize.py#l398">the code in question</a>.</p>
<pre><code>sage: [u02-1]
[u02 - 1]
sage: isinstance(u02-1,tuple)
False
sage: isinstance(u02-1,list)
False
sage: function_type=type(lambda x,y: x+y)
sage: function_type
<type 'function'>
sage: isinstance(u02-1,function_type)
False
</code></pre>
<p>And clearly it's not <code>None</code>, so the if/elif never actually gives <code>min</code> a value. Also notice that all the examples use lambda functions for their <code>cons</code> values.</p>
<p>Since <code>cons</code> "This should be either a function or list of functions that must be positive", perhaps just having a symbolic expression is not enough. After all, who knows what the variable is to be? But maybe we should have a better error check for this?</p>
https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19977#post-id-19977Somebody on irc suggested: "def constraint(u): return u[2] - 1" instead of "[u02-1]" and it worked. That way constraint is a function. Thx for your help.Fri, 06 Apr 2012 12:29:50 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19977#post-id-19977Comment by kcrisman for <p>I think I know what happened. I don't know if it should be considered user error or a bug.</p>
<p>Look at <a href="http://hg.sagemath.org/sage-main/file/c239be1054e0/sage/numerical/optimize.py#l398">the code in question</a>.</p>
<pre><code>sage: [u02-1]
[u02 - 1]
sage: isinstance(u02-1,tuple)
False
sage: isinstance(u02-1,list)
False
sage: function_type=type(lambda x,y: x+y)
sage: function_type
<type 'function'>
sage: isinstance(u02-1,function_type)
False
</code></pre>
<p>And clearly it's not <code>None</code>, so the if/elif never actually gives <code>min</code> a value. Also notice that all the examples use lambda functions for their <code>cons</code> values.</p>
<p>Since <code>cons</code> "This should be either a function or list of functions that must be positive", perhaps just having a symbolic expression is not enough. After all, who knows what the variable is to be? But maybe we should have a better error check for this?</p>
https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19975#post-id-19975Yes, that would solve your use case. What I'm wondering is whether we should be checking for other types of input (for instance, Sage functions instead of just Python ones).Fri, 06 Apr 2012 13:25:44 +0200https://ask.sagemath.org/question/8861/error-using-minimize_constrained/?comment=19975#post-id-19975