Using Symbolic function to execute Optimization in MATLAB

Hello all I ran into a symbolic viarible optimization problem in MATLAB. I tried to define array containing with symbolic element, and then apply a certain function and I would like to optimize this function which contain symbolic variables. Error notes show: "Function 'ge' is not implemented for MuPAD symbolic objects" which i assume meaning MATLAB can not run optimization on symbolic object functions. is there anyone know how to fix it? pls.....
This is the code:
clear all
global N ITERATION_STEP BETA a1 a2 a3
syms a1 a2 a3
N=3;
ITERATION_STEP=3;
% discount factor
BETA=0.95;
prob = [0.662,0.547,0.769]; %prob = rand(1,n);
disp('prob');
disp(prob);
states = zeros(1,N);
actions = [a1, a2, a3];
%actions(1,:)=20; initial transmitted power is 20dB
inst_reward = zeros(1,N);
future_reward = zeros(1,N);
for i = 1:N
imm_reward = InstReward(prob,actions);
end
disp('imm_reward');
disp(imm_reward);
% at every moment, there are two possible rewards, name them reward1 and
% reward 2 which reward1 denotes the reward when no state change happens,
% while reward2 denotes the reward when state changes.
for j = 1:2
for i = 1:2
alter_prob = 1 - prob;
reward1 = InstReward(prob, actions);
reward2 = InstReward(alter_prob, actions);
total = BETA.^(j)*(prob.*reward1 + alter_prob.*reward2);
future_reward = imm_reward + sum(total);
end
end
obj_function = @(actions)(sum(future_reward));
% optimization algorithm to find the value of actions to get the min reward
% [X,FVAL,EXITFLAG,OUTPUT] = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
%options = optimset('Display','off');
[X,FVAL,EXITFLAG,OUTPUT] = fmincon(obj_function,[0,0],[],[],[],[],0,[]);
But I got error note as: Error using sym>notimplemented (line 2682) Function 'ge' is not implemented for MuPAD symbolic objects.
Error in sym/ge (line 823) notimplemented('ge');
Error in nlconst (line 663) elseif f >=0
Error in fmincon (line 794) [X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in main (line 42) [X,FVAL,EXITFLAG,OUTPUT] = fmincon(obj_function,[0,0],[],[],[],[],0,[]);

 Akzeptierte Antwort

Matt J
Matt J am 8 Okt. 2013
Bearbeitet: Matt J am 8 Okt. 2013

0 Stimmen

No, fmincon (and other Optimization Toolbox solvers) do not do symbolic optimization.
You can use intermediate symbolic computations within your objective function and constraints to help generate output, but all direct input and output arguments to fmincon must be non-symbolic.

Weitere Antworten (3)

Walter Roberson
Walter Roberson am 8 Okt. 2013

0 Stimmen

You can use matlabFunction() to generate an anonymous function from a symbolic expression, and then optimize that function.

1 Kommentar

thx a lot. Care to give me an example? coz i am not sure how to use matlabFunction() to pass the function.....

Melden Sie sich an, um zu kommentieren.

Alan Weiss
Alan Weiss am 9 Okt. 2013

0 Stimmen

There are examples in the documentation showing how to optimize using matlabFunction. Here is one, and here is another.
Alan Weiss
MATLAB mathematical toolbox documentation
viva
viva am 9 Okt. 2013
Bearbeitet: viva am 9 Okt. 2013
hi all. i revised the code as followed using matlabFunction, but there are still mistakes on the last line:
clear all
global N ITERATION_STEP BETA a1 a2 a3
syms a1 a2 a3
N=3;
ITERATION_STEP=3;
% discount factor
BETA=0.95;
prob = [0.662;0.547;0.769]; %prob = rand(1,n);
disp('prob');
disp(prob);
states = zeros(N,1);
actions = [a1; a2; a3];
%actions(1,:)=20; initial transmitted power is 20dB
inst_reward = zeros(N,1);
future_reward = zeros(N,1);
for i = 1:N
imm_reward = InstReward(prob,actions);
end
disp('imm_reward');
disp(imm_reward);
% at every moment, there are two possible rewards, name them reward1 and
% reward 2 which reward1 denotes the reward when no state change happens,
% while reward2 denotes the reward when state changes.
for j = 1:2
for i = 1:2
alter_prob = 1 - prob;
reward1 = InstReward(prob, actions);
reward2 = InstReward(alter_prob, actions);
total = BETA.^(j)*(prob.*reward1 + alter_prob.*reward2);
future_reward = imm_reward + sum(total);
f = sum(future_reward);
obj_function = matlabFunction(f,'vars',{actions});
% optimization algorithm to find the value of actions to get the min reward
% [X,FVAL,EXITFLAG,OUTPUT] = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
% options = optimset('Display','off');
[X,FVAL,EXITFLAG,OUTPUT] = fmincon(@obj_function,[0,0],[],[],[],[],0,[]);
end
end
Error: File: main.m Line: 52 Column: 45 "obj_function" was previously used as a variable, conflicting with its use here as the name of a function or command.

3 Kommentare

Omit the '@',
fmincon(obj_function,...
viva
viva am 9 Okt. 2013
Bearbeitet: viva am 9 Okt. 2013
yes, u r right, now i am struggling on this part:
objfun = matlabFunction(f,'vars',{actions});
constraint = a1+a2+a3-50;
confun = matlabFunction(constraint);
% optimization algorithm to find the value of actions to get the min reward
% [X,FVAL,EXITFLAG,OUTPUT] = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
options = optimset('Display','off');
[X,FVAL,EXITFLAG,OUTPUT] = fmincon(objfun,[0,0,0],[],[],[],[],[],[],confun,options);
and there are error note said: Index exceeds matrix dimensions. Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Dear Matt, do you know what the problem is here? thx for your help.
Matt J
Matt J am 9 Okt. 2013
Bearbeitet: Matt J am 9 Okt. 2013
Test your objfun to see if it can accept a length(x)=3 input

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 8 Okt. 2013

Kommentiert:

am 9 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by