Ask Your Question

error using minimize_constrained

asked 2012-04-06 03:56:27 -0500

bennison gravatar image

When I use minimize_constrained(), I get the following error:

File "", line 1, in <module>

  File "/tmp/tmp81ftcX/", line 72, in <module>
    minimize_constrained(T, [_sage_const_1 ], nil)
  File "/sagenb/sage_install/", 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.)

edit retag flag offensive close merge delete


If you could post the *complete* code, that would be helpful. Currently `K` and `des` are undefined.

kcrisman gravatar imagekcrisman ( 2012-04-06 04:25:41 -0500 )edit

K 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.

bennison gravatar imagebennison ( 2012-04-06 05:25:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2012-04-06 04:30:51 -0500

kcrisman gravatar image

I 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.

sage: [u02-1]
[u02 - 1]
sage: isinstance(u02-1,tuple)
sage: isinstance(u02-1,list)
sage: function_type=type(lambda x,y: x+y)
sage: function_type
<type 'function'>
sage: isinstance(u02-1,function_type)

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?

edit flag offensive delete link more



Somebody 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.

bennison gravatar imagebennison ( 2012-04-06 05:29:50 -0500 )edit

Yes, 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).

kcrisman gravatar imagekcrisman ( 2012-04-06 06:25:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-04-06 03:56:27 -0500

Seen: 261 times

Last updated: Apr 06 '12