Supplied objective function must return a scalar value in fmincon

Hey guys please help me I am getting this error in my optimization problem
Below is the Data File
global SL V p r k m n C Z s
SL = 0.75;
V = 94100;
p = [0.07,0.18,0.2,0.3];
r = [55,47,45,49];
k = [33,28,29,30];
m = 4;
n= 4;
C = [78,69,70,73;
64,68,56,59;
34,39,42,41;
52,47,48,45];
Z =[250 250 250 250;
320 320 320 320;
440 440 440 440;
350 350 350 350];
s = [110,95,99,100;
110,95,99,100;
110,95,99,100;
110,95,99,100];
bguess = randi(4,4);
bguess = (bguess<2);
Q0 = [zeros(4,4) bguess];
% call the solver
xopt = fmincon(@EP, Q0,[],[],[],[],[],[]);
Below is the Objective Function File
function G = EP(Q,b)
global s r k C m n
rng;
y = rand(4);
b = randi(4,4);
b = (b<2);
q = 0;
for i = 1:m
for j = 1:n
q = q + y(i,j)*Q(i,j);
end
end
x = sym('x',[1,4]); %x as symbolic variable
f1 = 0;
for i = 1:m
for j = i:n
f1 = f1 + s(i,j)*b(i,j)*x(j);
end
end
f2 = 0;
for i = 1:m
for j = 1:n
f2 = f2+C(i,j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f3 = 0;
f3a = 0;
for i = 1:m
for j = 1:n
f3a = f3a + y(i,j)*Q(i,j)-x(j);
end
end
for i = 1:m
for j = 1:n
f3 = f3 + b(i,j)*r(j)*f3a;
end
end
f4 = 0;
for i = 1:m
for j = 1:n
f4 = f4+ s(j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f5 = 0;
f5a = 0;
for i = 1:m
for j = 1:n
f5a = f5a + x(j) -y(i,j)*Q(i,j);
end
end
for i = 1:m
for j = 1:n
f5 = f5 + b(i,j)*k(j)*f5a;
end
end
F1 = int(((f1 - f2 + f3).*normpdf(x, 400, 100)),0,q);
F2 = int(((f4 - f2 - f5).*normpdf(x, 400, 100)),q,Inf);
G = -(F1 +F2);
end
This is the error I am getting. Please help me.

9 Comments

Hey Walter
I tried as per your advice.
Please have a look at the pic below the code.
This is the data file.
I am not understanding why it is not showing results.
global SL V p r k m n C Z s
SL = 0.75;
V = 94100;
p = [0.07,0.18,0.2,0.3];
r = [55,47,45,49];
k = [33,28,29,30];
m = 4;
n= 4;
C = [78,69,70,73;
64,68,56,59;
34,39,42,41;
52,47,48,45];
Z =[250 250 250 250;
320 320 320 320;
440 440 440 440;
350 350 350 350];
s = [110,95,99,100;
110,95,99,100;
110,95,99,100;
110,95,99,100];
bguess = randi(4,4);
bguess = (bguess<2);
Q0 = [zeros(4,4) bguess];
% call the solver
xopt = fmincon(@EP, Q0,[],[],[],[],[],[]);
This is the command window showing
What I did is using double in objective function.
Below is the Objective function
function G = EP(Q,b)
global s r k C m n
rng;
y = rand(4);
b = randi(4,4);
b = (b<2);
q = 0;
for i = 1:m
for j = 1:n
q = q + y(i,j)*Q(i,j);
end
end
syms x; %x as symbolic variable
f1 = 0;
for i = 1:m
for j = i:n
f1 = f1 + s(i,j)*b(i,j)*x;
end
end
f2 = 0;
for i = 1:m
for j = 1:n
f2 = f2+C(i,j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f3 = 0;
f3a = 0;
for i = 1:m
for j = 1:n
f3a = f3a + y(i,j)*Q(i,j)-x;
end
end
for i = 1:m
for j = 1:n
f3 = f3 + b(i,j)*r(j)*f3a;
end
end
f4 = 0;
for i = 1:m
for j = 1:n
f4 = f4+ s(j)*b(i,j)*y(i,j)*Q(i,j);
end
end
f5 = 0;
f5a = 0;
for i = 1:m
for j = 1:n
f5a = f5a + x -y(i,j)*Q(i,j);
end
end
for i = 1:m
for j = 1:n
f5 = f5 + b(i,j)*k(j)*f5a;
end
end
F1 = int(((f1 - f2 + f3).*normpdf(x, 400, 100)),0,q);
F2 = int(((f4 - f2 - f5).*normpdf(x, 400, 100)),q,Inf);
G = double(-(F1 +F2));
end
You do not ask it to display results. You run the optimization but do not display anything.
"Local minimum possible" is normal. fmincon() is not a global optimizer, and it cannot do symbolic analysis of the formulas it is working with. It cannot prove that the point it found is a global minima so it does not make the claim that the output is definitely the minima.
Hi walter thanks for previous hints.
I have got one more error when I am now putting constraints in it.
Below is the constrints file
function [c,ceq] = Constraints(Q,b)
global SL V C
%Limit of Integration
Q = zeros(4,4);
q = 0;
rng;
y = rand(4);
b = randi(4,4);
b = (b<2);
for i1 = 1:4
for j1 = 1:4
q = q + y(i1,j1)*Q(i1,j1);
end
end
c1 = 0;
for i1 = 1:4
for j1 = 1:4
c1 = c1 +C(i1,j1)*b(i1,j1)*y(i1,j1)*Q(i1,j1);
end
end
x = sym('x',[1,4]); %x as symbolic variable
c2 = double(int(normpdf(x, 400, 100),q,Inf));
c3 = zeros(4,1);
for j1 = 1:4
for l = 1:4
for i1 = 1:4
c3(l) = c3(l) + b(i1,j1);
end
end
end
c(1) = c1 - V;
c(2) = c2 - SL;
ceq = c3 - 1;
end
I have tried everything but I am not getting a way. Please help.
x = sym('x',[1,4]); %x as symbolic variable
x is length 4
c2 = double(int(normpdf(x, 400, 100),q,Inf));
x is length 4, so normpdf(x, 400, 100) is a vector of length 4. You integrate an expression involving the vector of length 4, from q to Inf, where q is 0, You do not specify a variable of integration, so it does symvar() and picks the first one, which will be x1. So you integrate the expression of length 4 with respect to x1. As the expression involves x1, x2, x3, x4, the result is going to be of length 4 involving x2, x3, x4 (the x1 vanishes because it is a definite integration with respect to x1.) Because the result involves unresolved variables x2, x3, x4, there is no way to convert the result into a numeric scalar by using double() .
function [c,ceq] = Constraints(Q,b)
How are you invoking that as part of your fmincon() call? You are declaring it with two parameters, so normally you would have to use an anonymous function to pass in whatever b is. However your code does not use b, so it does not care that you are not passing anything in for b. The readers trying to make sense of your code care, though.
Q = zeros(4,4);
You are ignoring the current value of Q that is passed in, so you will always return the same value for the constraint function. It is not worth having a constraint function in such a case: either the constraint can never be satisfied and you are wasting time making the fmincon() call, or else the constraint is always satisfied and you wasting your time processing the constraint.
Hi Walter
I have this optimization problem.
I have to maximize EP.
I am unable to proceed further.
Can you tell me a way I can proceed.
If you could give me any hint or a way I can start once again.
"(reminder to myself)" indicates that I need to return to this question and give you a reply when I have more time. I have been working on so many questions recently that the ones I still need to do more work on, are scrolling off the first three pages of the lists of my activity.

Sign in to comment.

Answers (0)

Asked:

on 17 Jul 2020

Commented:

on 23 Jul 2020

Community Treasure Hunt

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

Start Hunting!