fmin_slsqp constraint problem

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

fmin_slsqp constraint problem

Neal Becker
I have a ineq constraint:

    ## constrain poles to be inside unit circle
    def c(coef, len_z, len_p, dz, dp):
        p = compose ((coef/opt.scale)[len_z:-1], dp)
        return np.abs(p) - 1

So this will return a 1D array where each value should satisfy the constraint.  
fmin_slsqp will not accept this directly:

    e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z, len_p,
dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
full_output=True)

Traceback (most recent call last):
  File "./optimize_pll5.3.2.py", line 519, in <module>
    run_line (sys.argv)
  File "./optimize_pll5.3.2.py", line 498, in run_line
    e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z, len_p,
dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
full_output=True)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line 334,
in fmin_slsqp
    a_ieq[i] = ieqcons_prime[i](x)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line
176, in function_wrapper
    return function(x, *args)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line
398, in approx_fprime
    grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
ValueError: setting an array element with a sequence.

Any ideas on this?

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Oleksandr Huziy
What is your obj_fnc, I know it is naive, bu still, is it possible that it returns a list?

Cheers
--
Oleksandr (Sasha) Huziy

2012/10/26 Neal Becker <[hidden email]>
I have a ineq constraint:

    ## constrain poles to be inside unit circle
    def c(coef, len_z, len_p, dz, dp):
        p = compose ((coef/opt.scale)[len_z:-1], dp)
        return np.abs(p) - 1

So this will return a 1D array where each value should satisfy the constraint.
fmin_slsqp will not accept this directly:

    e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z, len_p,
dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
full_output=True)

Traceback (most recent call last):
  File "./optimize_pll5.3.2.py", line 519, in <module>
    run_line (sys.argv)
  File "./optimize_pll5.3.2.py", line 498, in run_line
    e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z, len_p,
dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
full_output=True)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line 334,
in fmin_slsqp
    a_ieq[i] = ieqcons_prime[i](x)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line
176, in function_wrapper
    return function(x, *args)
  File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py", line
398, in approx_fprime
    grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
ValueError: setting an array element with a sequence.

Any ideas on this?

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Neal Becker
The obj_fnc is much too complicated to include here, but does return a single
value.  I think the problem is ieqcons returns an array, while fmin_slsqp
expects a single value.

Oleksandr Huziy wrote:

> What is your obj_fnc, I know it is naive, bu still, is it possible that it
> returns a list?
>
> Cheers
> --
> Oleksandr (Sasha) Huziy
>
> 2012/10/26 Neal Becker <[hidden email]>
>
>> I have a ineq constraint:
>>
>>     ## constrain poles to be inside unit circle
>>     def c(coef, len_z, len_p, dz, dp):
>>         p = compose ((coef/opt.scale)[len_z:-1], dp)
>>         return np.abs(p) - 1
>>
>> So this will return a 1D array where each value should satisfy the
>> constraint.
>> fmin_slsqp will not accept this directly:
>>
>>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z,
>> len_p,
>> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
>> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
>> full_output=True)
>>
>> Traceback (most recent call last):
>>   File "./optimize_pll5.3.2.py", line 519, in <module>
>>     run_line (sys.argv)
>>   File "./optimize_pll5.3.2.py", line 498, in run_line
>>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef, len_z,
>> len_p,
>> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz, dp),
>> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz, dp)],
>> full_output=True)
>>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line
>> 334,
>> in fmin_slsqp
>>     a_ieq[i] = ieqcons_prime[i](x)
>>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py",
>> line
>> 176, in function_wrapper
>>     return function(x, *args)
>>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/optimize.py",
>> line
>> 398, in approx_fprime
>>     grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
>> ValueError: setting an array element with a sequence.
>>
>> Any ideas on this?
>>
>> _______________________________________________
>> SciPy-User mailing list
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Moore, Eric (NIH/NIDDK) [F]
> -----Original Message-----
> From: Neal Becker [mailto:[hidden email]]
> Sent: Friday, October 26, 2012 11:08 AM
> To: [hidden email]
> Subject: Re: [SciPy-User] fmin_slsqp constraint problem
>
> The obj_fnc is much too complicated to include here, but does return a
> single
> value.  I think the problem is ieqcons returns an array, while
> fmin_slsqp
> expects a single value.
>
> Oleksandr Huziy wrote:
>
> > What is your obj_fnc, I know it is naive, bu still, is it possible
> that it
> > returns a list?
> >
> > Cheers
> > --
> > Oleksandr (Sasha) Huziy
> >
> > 2012/10/26 Neal Becker <[hidden email]>
> >
> >> I have a ineq constraint:
> >>
> >>     ## constrain poles to be inside unit circle
> >>     def c(coef, len_z, len_p, dz, dp):
> >>         p = compose ((coef/opt.scale)[len_z:-1], dp)
> >>         return np.abs(p) - 1
> >>
> >> So this will return a 1D array where each value should satisfy the
> >> constraint.
> >> fmin_slsqp will not accept this directly:
> >>
> >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
> len_z,
> >> len_p,
> >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
> dp),
> >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
> dp)],
> >> full_output=True)
> >>
> >> Traceback (most recent call last):
> >>   File "./optimize_pll5.3.2.py", line 519, in <module>
> >>     run_line (sys.argv)
> >>   File "./optimize_pll5.3.2.py", line 498, in run_line
> >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
> len_z,
> >> len_p,
> >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
> dp),
> >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
> dp)],
> >> full_output=True)
> >>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py",
> line
> >> 334,
> >> in fmin_slsqp
> >>     a_ieq[i] = ieqcons_prime[i](x)
> >>   File "/usr/lib64/python2.7/site-
> packages/scipy/optimize/optimize.py",
> >> line
> >> 176, in function_wrapper
> >>     return function(x, *args)
> >>   File "/usr/lib64/python2.7/site-
> packages/scipy/optimize/optimize.py",
> >> line
> >> 398, in approx_fprime
> >>     grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
> >> ValueError: setting an array element with a sequence.
> >>
> >> Any ideas on this?
> >>

It looks like the difference between ieqcons and f_ieqcons is returning an array or an scalar. I've not used fmin_slsqp, this is based solely on the documentation.

Eric
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Oleksandr Huziy
You can modify your constraint


  ## constrain poles to be inside unit circle
    def c(coef, len_z, len_p, dz, dp):
        p = compose ((coef/opt.scale)[len_z:-1], dp)
        return np.min( np.abs(p) - 1)

Cheers
--
Oleksandr (Sasha) Huziy

2012/10/26 Moore, Eric (NIH/NIDDK) [F] <[hidden email]>
> -----Original Message-----
> From: Neal Becker [mailto:[hidden email]]
> Sent: Friday, October 26, 2012 11:08 AM
> To: [hidden email]
> Subject: Re: [SciPy-User] fmin_slsqp constraint problem
>
> The obj_fnc is much too complicated to include here, but does return a
> single
> value.  I think the problem is ieqcons returns an array, while
> fmin_slsqp
> expects a single value.
>
> Oleksandr Huziy wrote:
>
> > What is your obj_fnc, I know it is naive, bu still, is it possible
> that it
> > returns a list?
> >
> > Cheers
> > --
> > Oleksandr (Sasha) Huziy
> >
> > 2012/10/26 Neal Becker <[hidden email]>
> >
> >> I have a ineq constraint:
> >>
> >>     ## constrain poles to be inside unit circle
> >>     def c(coef, len_z, len_p, dz, dp):
> >>         p = compose ((coef/opt.scale)[len_z:-1], dp)
> >>         return np.abs(p) - 1
> >>
> >> So this will return a 1D array where each value should satisfy the
> >> constraint.
> >> fmin_slsqp will not accept this directly:
> >>
> >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
> len_z,
> >> len_p,
> >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
> dp),
> >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
> dp)],
> >> full_output=True)
> >>
> >> Traceback (most recent call last):
> >>   File "./optimize_pll5.3.2.py", line 519, in <module>
> >>     run_line (sys.argv)
> >>   File "./optimize_pll5.3.2.py", line 498, in run_line
> >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
> len_z,
> >> len_p,
> >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
> dp),
> >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
> dp)],
> >> full_output=True)
> >>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py",
> line
> >> 334,
> >> in fmin_slsqp
> >>     a_ieq[i] = ieqcons_prime[i](x)
> >>   File "/usr/lib64/python2.7/site-
> packages/scipy/optimize/optimize.py",
> >> line
> >> 176, in function_wrapper
> >>     return function(x, *args)
> >>   File "/usr/lib64/python2.7/site-
> packages/scipy/optimize/optimize.py",
> >> line
> >> 398, in approx_fprime
> >>     grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
> >> ValueError: setting an array element with a sequence.
> >>
> >> Any ideas on this?
> >>

It looks like the difference between ieqcons and f_ieqcons is returning an array or an scalar. I've not used fmin_slsqp, this is based solely on the documentation.

Eric
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Neal Becker
Do you think applying np.min here will produce the same sort of convergence
behaviour?

Oleksandr Huziy wrote:

> You can modify your constraint
>
>
>   ## constrain poles to be inside unit circle
>     def c(coef, len_z, len_p, dz, dp):
>         p = compose ((coef/opt.scale)[len_z:-1], dp)
>         return np.min( np.abs(p) - 1)
>
> Cheers
> --
> Oleksandr (Sasha) Huziy
>
> 2012/10/26 Moore, Eric (NIH/NIDDK) [F] <[hidden email]>
>
>> > -----Original Message-----
>> > From: Neal Becker [mailto:[hidden email]]
>> > Sent: Friday, October 26, 2012 11:08 AM
>> > To: [hidden email]
>> > Subject: Re: [SciPy-User] fmin_slsqp constraint problem
>> >
>> > The obj_fnc is much too complicated to include here, but does return a
>> > single
>> > value.  I think the problem is ieqcons returns an array, while
>> > fmin_slsqp
>> > expects a single value.
>> >
>> > Oleksandr Huziy wrote:
>> >
>> > > What is your obj_fnc, I know it is naive, bu still, is it possible
>> > that it
>> > > returns a list?
>> > >
>> > > Cheers
>> > > --
>> > > Oleksandr (Sasha) Huziy
>> > >
>> > > 2012/10/26 Neal Becker <[hidden email]>
>> > >
>> > >> I have a ineq constraint:
>> > >>
>> > >>     ## constrain poles to be inside unit circle
>> > >>     def c(coef, len_z, len_p, dz, dp):
>> > >>         p = compose ((coef/opt.scale)[len_z:-1], dp)
>> > >>         return np.abs(p) - 1
>> > >>
>> > >> So this will return a 1D array where each value should satisfy the
>> > >> constraint.
>> > >> fmin_slsqp will not accept this directly:
>> > >>
>> > >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
>> > len_z,
>> > >> len_p,
>> > >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
>> > dp),
>> > >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
>> > dp)],
>> > >> full_output=True)
>> > >>
>> > >> Traceback (most recent call last):
>> > >>   File "./optimize_pll5.3.2.py", line 519, in <module>
>> > >>     run_line (sys.argv)
>> > >>   File "./optimize_pll5.3.2.py", line 498, in run_line
>> > >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
>> > len_z,
>> > >> len_p,
>> > >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
>> > dp),
>> > >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
>> > dp)],
>> > >> full_output=True)
>> > >>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py",
>> > line
>> > >> 334,
>> > >> in fmin_slsqp
>> > >>     a_ieq[i] = ieqcons_prime[i](x)
>> > >>   File "/usr/lib64/python2.7/site-
>> > packages/scipy/optimize/optimize.py",
>> > >> line
>> > >> 176, in function_wrapper
>> > >>     return function(x, *args)
>> > >>   File "/usr/lib64/python2.7/site-
>> > packages/scipy/optimize/optimize.py",
>> > >> line
>> > >> 398, in approx_fprime
>> > >>     grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
>> > >> ValueError: setting an array element with a sequence.
>> > >>
>> > >> Any ideas on this?
>> > >>
>>
>> It looks like the difference between ieqcons and f_ieqcons is returning an
>> array or an scalar. I've not used fmin_slsqp, this is based solely on the
>> documentation.
>>
>> Eric
>> _______________________________________________
>> SciPy-User mailing list
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: fmin_slsqp constraint problem

Oleksandr Huziy
I am not an expert in this function but the constraints are equivalent, so I would expect it. Actually it should be the same if the implementation is consistent, since we are not changing constraints, but expressing them in a different way.

But you could test it. By comparing this method and by creating n constraints which act on each component of dp.
smth like this

ieqcons=[lambda coef, len_z, len_p,
dz, dp: -c(coef, len_z, len_p, dz, dp, comp_num) for comp_num in range(len_p)]

and modify c respectively

    ## constrain poles to be inside unit circle
    def c(coef, len_z, len_p, dz, dp, comp_num):
        p = compose ((coef/opt.scale)[len_z:-1], dp)
        return (np.abs(p) - 1)[comp_num]


Cheers

--
Oleksandr (Sasha) Huziy

2012/10/26 Neal Becker <[hidden email]>
Do you think applying np.min here will produce the same sort of convergence
behaviour?

Oleksandr Huziy wrote:

> You can modify your constraint
>
>
>   ## constrain poles to be inside unit circle
>     def c(coef, len_z, len_p, dz, dp):
>         p = compose ((coef/opt.scale)[len_z:-1], dp)
>         return np.min( np.abs(p) - 1)
>
> Cheers
> --
> Oleksandr (Sasha) Huziy
>
> 2012/10/26 Moore, Eric (NIH/NIDDK) [F] <[hidden email]>
>
>> > -----Original Message-----
>> > From: Neal Becker [mailto:[hidden email]]
>> > Sent: Friday, October 26, 2012 11:08 AM
>> > To: [hidden email]
>> > Subject: Re: [SciPy-User] fmin_slsqp constraint problem
>> >
>> > The obj_fnc is much too complicated to include here, but does return a
>> > single
>> > value.  I think the problem is ieqcons returns an array, while
>> > fmin_slsqp
>> > expects a single value.
>> >
>> > Oleksandr Huziy wrote:
>> >
>> > > What is your obj_fnc, I know it is naive, bu still, is it possible
>> > that it
>> > > returns a list?
>> > >
>> > > Cheers
>> > > --
>> > > Oleksandr (Sasha) Huziy
>> > >
>> > > 2012/10/26 Neal Becker <[hidden email]>
>> > >
>> > >> I have a ineq constraint:
>> > >>
>> > >>     ## constrain poles to be inside unit circle
>> > >>     def c(coef, len_z, len_p, dz, dp):
>> > >>         p = compose ((coef/opt.scale)[len_z:-1], dp)
>> > >>         return np.abs(p) - 1
>> > >>
>> > >> So this will return a 1D array where each value should satisfy the
>> > >> constraint.
>> > >> fmin_slsqp will not accept this directly:
>> > >>
>> > >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
>> > len_z,
>> > >> len_p,
>> > >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
>> > dp),
>> > >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
>> > dp)],
>> > >> full_output=True)
>> > >>
>> > >> Traceback (most recent call last):
>> > >>   File "./optimize_pll5.3.2.py", line 519, in <module>
>> > >>     run_line (sys.argv)
>> > >>   File "./optimize_pll5.3.2.py", line 498, in run_line
>> > >>     e = fmin_slsqp (obj_fnc, coef*opt.scale, ieqcons=[lambda coef,
>> > len_z,
>> > >> len_p,
>> > >> dz, dp: -c(coef, len_z, len_p, dz, dp)], args=(len(lz), len(lp), dz,
>> > dp),
>> > >> eqcons=[lambda coef, len_z, len_p, dz, dp: h(coef, len_z, len_p, dz,
>> > dp)],
>> > >> full_output=True)
>> > >>   File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py",
>> > line
>> > >> 334,
>> > >> in fmin_slsqp
>> > >>     a_ieq[i] = ieqcons_prime[i](x)
>> > >>   File "/usr/lib64/python2.7/site-
>> > packages/scipy/optimize/optimize.py",
>> > >> line
>> > >> 176, in function_wrapper
>> > >>     return function(x, *args)
>> > >>   File "/usr/lib64/python2.7/site-
>> > packages/scipy/optimize/optimize.py",
>> > >> line
>> > >> 398, in approx_fprime
>> > >>     grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
>> > >> ValueError: setting an array element with a sequence.
>> > >>
>> > >> Any ideas on this?
>> > >>
>>
>> It looks like the difference between ieqcons and f_ieqcons is returning an
>> array or an scalar. I've not used fmin_slsqp, this is based solely on the
>> documentation.
>>
>> Eric
>> _______________________________________________
>> SciPy-User mailing list
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user