fgoalattain Optimization problem - objective function variables do not change

3 views (last 30 days)
I have written the following code to optimize variables of a function in Matlab. The function executes a sql statement in a database and runs a few codes within database by calling a wrapper function. The results of the query are what I want them to attain the goal values eventually (or get as close as possibly can):
%% Connection to databse using a JDBC driver url
instance = '***';
username = '***';
password = '***';
driver = 'postgreSQL';
databaseurl = '****';
db_conn = database(instance,username,password,'Vendor',driver,'Server',... databaseurl);
%% Setting optimization Objectives
tp = 100;
fn = 0;
fp = 0;
goal = [tp, fn, fp];
%% Setting weight vector
weight = [1, -1, -1];
%% Initializing parameters
models = 1;
models_str = mat2str(models);
fcst_pars0 = [0.1, 0.1];
agg = 0;
agg_str = num2str(agg);
tc = 6;
tc_str = mat2str(tc);
date = '''2015-01-19''';
%% Setting bounds
lb = zeros(size(fcst_pars0));
ub = ones(size(fcst_pars0));
%% Setting display parameters
options = optimoptions('fgoalattain','Display','iter-detailed',... 'DiffMinChange',0.5,'TolFun',1e-15,'TolX',1e-15);
%% Calling optimization solver
[fcst_pars,~,attainfactor] = ... fgoalattain(@(fcst_pars) tampfun( models_str, fcst_pars, agg_str, ... tc_str, date, db_conn ),fcst_pars0,goal,weight,[],[],[],[],lb,ub, ... [],options);
--------------------------------------------------------------------------------------
function kpi = tampfun( models_str, fcst_pars, agg_str, tc_str, date, db_conn )
%% Conversion of function inputs into strings
pvalue_str = num2str(fcst_pars(1));
rsquared_str = num2str(fcst_pars(2));
%% Execution of SQL statement
sqlquery = strcat('SELECT * FROM rtest(array[',models_str,'],',... pvalue_str,',',rsquared_str,',',agg_str,',array[',tc_str,'],','false,0,0,',date,')');
curs = exec(db_conn,sqlquery);
%% Getting the results of the query
curs = fetch(curs);
kpi = [curs.Data{1}, curs.Data{2}, curs.Data{3}];
close(curs);
end
I am expecting the fcst_pars values to be changes from one iteration to another until an optimum solution is achieved. But the fcst_pars values do not change at a new iteration or during function evaluations within each iteration.
Any comments on how I can get this working properly?

Answers (1)

Alan Weiss
Alan Weiss on 8 Apr 2015
It is hard to read your code. Please tag appropriate portions of your question with the {} Code button.
But, if I understand you correctly, you are expecting to do a multiobjective optimization where your control variables (the ones that fgoalattain can change, namely fcst_pars, are integers, or are rounded to integers. If I am correct, then you cannot use fgoalattain, because it works only with continuous variables (it is a derivative-based solver). I see that you set DiffMinChange to 0.5, a very large value, perhaps because you wish to use a derivative-based method even with a discrete problem. Sorry, this is not going to work.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Amir Shamdani
Amir Shamdani on 8 Apr 2015
Thanks for your answer Alan!
Sorry that my code is not readable! Do you think I should remove all the comments? If you think it helps, let me know and I'll remove them all!
The control variables are real and not integer. As part of the code I define initial values of the control variables as follows:
fcst_pars0 = [0.1, 0.1];
They can change between 0 and 1 which was again defined as upper and lower bounds for the control variable as follows:
lb = zeros(size(fcst_pars0));
ub = ones(size(fcst_pars0));
Therefore, the control variable fcst_pars is a continuous variable.
When I saw that the control variables are not changing as part of the optimisation, I decided to change the value of DiffMinChange to a larger value to force the solver to take larger steps.
Alan Weiss
Alan Weiss on 8 Apr 2015
No, you should tag appropriate portions of your question with the {} Code button, not remove comments. Remove blank code lines as appropriate once you have tagged the lines to look like code.
If the control variables are supposed to be between 0 and 1, then having DiffMinChange set to 0.5 may cause the gradient estimation step to be right on the boundary immediately. Are you sure that you want to make it that large? And what is DiffMaxChange set to?
Your TolFun and TolX options are not very sensible. See the documentation. If you don't have a good reason for changing them, then you would probably do better to leave them at their default values.
But this does not address your main problem of why your solver is not proceeding. I really don't know. Maybe you can step through the solution process using the debugger. Or just give different values to your control variables and see if the objective function changes.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!