Error in fmincon : how to write "fun" when x is an array?

Hi,
I'm using the fmincon function to solve a NLP(Nonlinear problem).The x of the problem is an array(6*7). So I'm a little confused about how to write myfun(the objective function) and the nonlcon(the nonlinear constraints).
Many thanks in advance for your help!
Best wishes!
I think I should post my error first:
myfun is a scalar function of x(an array,6*7), with several other parameters(such as L,G,G1,N0) which are all defined and assigned in test.m. To calculate the objective function's value, I need to calculate some variables first, such as SINR,C and I ,using variable x and the assigned parameters. And to calculate f, I used for-loops.( Question 1 :Can I use for-loops to express the objective function, rather than define the function with a equation as usual? )
The variables SINR,C and I are also needed in the nonlcon. So I defined them as global variables in myfun.m. What confused me is that when I write mycon(the nonlcon) I find out I don't need the original variable x ,I only need the intermediate variables SINR,C and I. Is is permitted to write mycon without variable x? (That's the Question 2)
And the result of mycon: [c ceq],c is an array(2*7),ceq=[].
Question 3 : Is it right to write like this?
f=@(x)myfun(x,L,G,G1,N0);
nonlcon = mycon(L);
x = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon);
And here are myfun.m and mycon.m for reference.
%=======================myfun.m===============================
function f = myfun1(x,L,G,G1,N0)
f=0;
global SINR;
SINR=zeros(5,7);
global I;
I=zeros(5,7);
global C;
C=zeros(1,7);
% -------------------to calculate I---------------------------
for k=1:5
for n=1:7
for m=1:7
t=x(k,n)*x(k,m)*x(6,m)*G1(n,m);
I(k,n)=I(k,n)+t;
end
end
end
% -------------------to calculate SINR--------------------------
for k=1:5
for n=1:7
SINR(k,n)=x(k,n)*x(6,n)*G(n,n)/(I(k,n)+N0);
end
end
% ---------to calculate C and the objective function's value f----
for n=1:7
for k=1:5
t=x(k,n)*log2(1+SINR(k,n));
C(1,n)=t+C(1,n);
end
t2=L(2,n)*C(1,n);
f=f+t2;
end
%============================mycon.m=================================
function [ c,ceq ] = mycon1(L)
c=zeros(2,7);
global I;
global C;
for n=1:7
for k=1:5
c(2,n)=c(2,n)+I(k,n);
end
c(1,n)=L(3,n)*(tran(L(2,n))*C(1,n)+tran(L(2,L(1,n)))*C(1,L(1,n)));
c(2,n)=c(2,n)-3;
end
ceq=[];

 Accepted Answer

1) Yes, It is fine to use any amount of logic and loops to calculate the output of your objective function.
2) No, it is not permitted to write your nonlinear constraint function without reference to x. It is not required that the nonlinear constraint function use x in the calculation. On the other hand, nonlinear constraints that do not depend upon x are either always satisfied or else never satisfiable, and either way there is no point including them.
3) No. Your nonlinear constraint position must either be empty or a function handle.
That said:
The real answer to #2 is that you are doing things incorrectly.
fmincon does not completely define the order in which the objective function and nonlinear constraint function are called relative to each other. During an early phase of gradient estimation, fmincon calls the objective function several times without calling the nonlinear constraint function. During the main series, fmincon could skip calling the nonlinear constraint function if it found that the objective function indicated that a particular position was worse, so the two are not certain to be called as pairs. During the time that fmincon is attempting to find a valid position, it could potentially call the nonlinear constraint function a number of times in order to find a position worth evaluating the objective function at. The next objective function call might be at a different location that fmincon projects to be in-bounds.
Thus, the nonlinear constraint function cannot count on fmincon having just called the objective function for that location, and the objective function cannot count on the nonlinear constraint function being called on that location either before or after the objective function.
This leads us to the proposition that neither the objective function nor the nonlinear constraint function should attempt to communicate computation information through global variables. The functions must assume that they are being called independently of the other.
To deal with this, do not use global variables. Instead, create a function that, given x, calculates those variables, and call that function from both the objective function and the nonlinear constraint function.
If you have R2017a or later, you could memoize() the common function.
You should also use vectorization to improve the performance. For example look at
for k=1:5
for n=1:7
for m=1:7
t=x(k,n)*x(k,m)*x(6,m)*G1(n,m);
I(k,n)=I(k,n)+t;
end
end
end
You could create a vector of t values for the m values and add the sum of the vector to I(k,n):
for k=1:5
for n=1:7
for m=1:7
t(m)=x(k,n)*x(k,m)*x(6,m)*G1(n,m);
end
I(k,n) = I(k,n) + sum(t);
end
end
The x(k,n) and is a common multiplier independent of m, so you can move that out:
for k=1:5
for n=1:7
for m=1:7
t(m)=x(k,m)*x(6,m)*G1(n,m);
end
I(k,n) = I(k,n) + x(k,n) .* sum(t);
end
end
and you can vectorize the "for m" loop:
for k=1:5
for n=1:7
m=1:7
t(m)=x(k,m).*x(6,m).*G1(n,m);
I(k,n) = I(k,n) + x(k,n) .* sum(t);
end
end
but m is just the dimension of x, and we will assume it is the dimension of G1 as well, so we can abbreviate:
for k=1:5
for n=1:7
t = x(k,:) .* x(6,:) .* G1(n,:);
I(k,n) = I(k,n) + x(k,n) .* sum(t);
end
end
x(6,:) is independent, so you can move that out too:
for k=1:5
for n=1:7
t = x(k,:) .* G1(n,:);
I(k,n) = I(k,n) + x(k,n) .* sum(t .* x(6,:));
end
end
and you can reduce that further. It would not surprise me if you can remove at least one more layer of loop, possibly even both layers of loop, by using some matrix multiplications.

3 Comments

Thank you very much for not only answering my questions but also showing me the simplification step by step.
You explained the reason why the objective function and the constraint function shouldn't be relative. Your explanation is detailed and easy to understand. I really appreciate it.
As you recommended, I create another function to calculate [I,SINR,C] and call that function in 'myfun' and 'mycon'. But I got some new errors which I don't know how to fix it.
This is the function I created :
function [I,SINR,C]=calculateM(x,G,G1,N0)
...
And this is the myfun.m
%
function f = myfun(x,G,G1,N0,L)
f=0;
[I,SINR,C]=calculateM(x,G,G1,N0);
for n=1:7
f=f+L(2,n)*C(1,n);
end
end
In the test.m, after the definition of the variables:
%
y = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon);
The error:
%
>> retest
Not enough input arguments.
Error in myfun (line 4)
[I,SINR,C]=calculateM(x,G,G1,N0);
Error in fmincon (line 534)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in retest (line 23)
y = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon);
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
I don't know why there is the error "Not enough input arguments". I think it may caused by fmincon because it seems like the fmincon function will check the objective function at first without using the initial values of these needed variables. Is that right? Or for some other reasons? And how can I fix it?
Thanks again and really hope to get your reply.
Best wishes.
You should continue to parameterize myfun like you did before. Before you had
f=@(x)myfun(x,L,G,G1,N0);
and you should use the same thing now:
f = @(x)myfun(x,L,G,G1,N0);
cf = @(x) mycon(x,L,G,G1,N0);
y = fmincon(f, x0, A, b, Aeq, beq, lb, ub, cf);
Thank you so much. The problem solved.
Best wishes.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 1 May 2017
Edited: Stephen23 on 1 May 2017
Most importantly noncon needs to be a function (of x, which does not need to be used inside the function). Personally I do not trust globals (search this forum to know why), so I rewrote your code using nested functions, which are more robust and much easier to debug.
Note that the loops most likely could be vectorized. I also did not run your code (you did not supply tran, nor did I check the algorithm.

1 Comment

Thanks a lot! I run your rewrite-test.m and it worked. You are right that the global variables are not trust-able. :) However, it converged to an infeasible point. I think the reason why it converged to an infeasible point is complicated.
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance but constraints are not
satisfied to within the default value of the constraint tolerance.
Best wishes!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!