From: Edgar Galvan on
Hi all,

I am trying to optimize a function
function y=myfun(X,C,K)
where X is a vector of variables and C and K are vectors of constants
so i have
[x,fval]=fmincon(@(X)myfun(X,C,K),A,b)


for debugging purposes, it would be very helpful if I could tell fmincon to keep certain elements of X constant during optimization. Is there a way to do this easily?

Thanks in advance
From: Matt J on
"Edgar Galvan" <dekuf409(a)aol.com> wrote in message <i02qe0$jr8$1(a)fred.mathworks.com>...

> for debugging purposes, it would be very helpful if I could tell fmincon to keep certain elements of X constant during optimization. Is there a way to do this easily?
=========


You could do something like the following:


Objective=@(X)myfun(X,C,K);

xFixed=blablabla;
subObjective=@(xVarying) objective([xVarying ; xFixed]);

[xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b)


Of course, in your actual situation, the concatenation
[xVarying ; xFixed] might have to be substituted with some more complicated interweaving of the two sets of variables.

I emphasize that this is only recommendable for debugging purposes! It is NOT efficient to do this in the implementation of an actual algorithm.
From: Edgar Galvan on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i02s68$g2h$1(a)fred.mathworks.com>...
> "Edgar Galvan" <dekuf409(a)aol.com> wrote in message <i02qe0$jr8$1(a)fred.mathworks.com>...
>
> > for debugging purposes, it would be very helpful if I could tell fmincon to keep certain elements of X constant during optimization. Is there a way to do this easily?
> =========
>
>
> You could do something like the following:
>
>
> Objective=@(X)myfun(X,C,K);
>
> xFixed=blablabla;
> subObjective=@(xVarying) objective([xVarying ; xFixed]);
>
> [xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b)
>
>
> Of course, in your actual situation, the concatenation
> [xVarying ; xFixed] might have to be substituted with some more complicated interweaving of the two sets of variables.
>
> I emphasize that this is only recommendable for debugging purposes! It is NOT efficient to do this in the implementation of an actual algorithm.

Hum, I can't seem to get it to work. It still varies all the elements in X. This is basically what what my code looks like (simplified).

Objective function:
function f=myfun(X)
f =2*X(1)^2-X(2);

Script:
A=[1 2;-4 0;0 -1]; b=[5 -7 -2];

Objective=@(X)myfun(X);
xFixed=1;
subObjective=@(xVarying)Objective([xVarying ; xFixed]);
xVaryingInitialGuess=1;

[xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b);

Did I do something dumb?

Thanks
From: Alan Weiss on
On 6/25/2010 3:30 PM, Edgar Galvan wrote:
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message
> <i02s68$g2h$1(a)fred.mathworks.com>...
>> "Edgar Galvan" <dekuf409(a)aol.com> wrote in message
>> <i02qe0$jr8$1(a)fred.mathworks.com>...
>>
>> > for debugging purposes, it would be very helpful if I could tell
>> fmincon to keep certain elements of X constant during optimization. Is
>> there a way to do this easily? =========
>>
>>
>> You could do something like the following:
>>
>>
>> Objective=@(X)myfun(X,C,K);
>>
>> xFixed=blablabla;
>> subObjective=@(xVarying) objective([xVarying ; xFixed]);
>>
>> [xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b)
>>
>>
>> Of course, in your actual situation, the concatenation
>> [xVarying ; xFixed] might have to be substituted with some more
>> complicated interweaving of the two sets of variables.
>>
>> I emphasize that this is only recommendable for debugging purposes! It
>> is NOT efficient to do this in the implementation of an actual algorithm.
>
> Hum, I can't seem to get it to work. It still varies all the elements in
> X. This is basically what what my code looks like (simplified).
> Objective function:
> function f=myfun(X)
> f =2*X(1)^2-X(2);
>
> Script:
> A=[1 2;-4 0;0 -1]; b=[5 -7 -2];
>
> Objective=@(X)myfun(X);
> xFixed=1;
> subObjective=@(xVarying)Objective([xVarying ; xFixed]);
> xVaryingInitialGuess=1;
>
> [xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b);
>
> Did I do something dumb?
>
> Thanks
Another approach:
Use the interior-point algorithm.
Set equal upper and lower bounds for the components you want fixed.
It's inefficient, but very easy.

Alan Weiss
MATLAB mathematical toolbox documentation
From: Matt J on
"Edgar Galvan" <dekuf409(a)aol.com> wrote in message <i0304n$64b$1(a)fred.mathworks.com>...

> Objective function:
> function f=myfun(X)
> f =2*X(1)^2-X(2);
>
> Script:
> A=[1 2;-4 0;0 -1]; b=[5 -7 -2];
>
> Objective=@(X)myfun(X);
> xFixed=1;
> subObjective=@(xVarying)Objective([xVarying ; xFixed]);
> xVaryingInitialGuess=1;
>
> [xVarying,fval]=fmincon(subObjective,[xVaryingInitialGuess;xFixed],A,b);
>
> Did I do something dumb?
=========

No, my mistake. I think Alan's approach is probably the easiest, but here's the necessary modification:

N=length(xVaryingInitialGuess);
Asub=A(:,1:N);
bsub=b-A(:,N+1:end)*xFixed;

[xVarying,fval]=fmincon(subObjective,xVaryingInitialGuess,Asub,bsub);