How to save 'display-iter' from fmincon to a .txt file?

15 views (last 30 days)
In the options of a fmincon function I set Display to iter and this is how the output loos like. I would like to save it to a .txt file in the very same form that is displayed on a screen. I googleded it and Someone mentioned that the similar problems might be done by using 'OutputFcn' but I have no idea how to do it as I am not sure how my output function should look like.
Thank you in advance!
No active inequalities. Elapsed time is 0.203401 seconds.
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 5 2496.66 0
1 13 2496.25 0 0.125 -14.5 26.9
2 20 2495.99 -0.1969 0.25 -2.45 21.8
3 26 2495.24 -0.2492 0.5 -2.74 29.9
4 33 2494.38 -0.3498 0.25 -7.68 11.1
5 38 2494.17 -0.2437 1 -11.2 10.8
6 43 2493.36 -0.283 1 -4.19 3.59
7 48 2492.84 -0.162 1 -1.31 9.38
8 53 2492.63 0 1 -2.28 4.2
9 58 2492.61 0 1 -1.28 0.21
10 63 2492.61 0 1 -0.0412 0.111
11 68 2492.57 0 1 -0.0386 2.94 Hessian modified
12 73 2492.52 0 1 -0.0279 5.33
13 78 2492.35 0 1 -0.0271 10.2
14 83 2492 0 1 -0.025 15.5
15 88 2491.35 0 1 -0.0244 18.5
16 93 2490.79 0 1 -0.0234 11.4
17 98 2490.64 0 1 -0.0642 3.4
18 103 2490.61 0 1 -0.00727 0.151
19 108 2490.61 0 1 -0.00048 0.00781

Accepted Answer

Les Beckham
Les Beckham on 7 Apr 2016
Getting back to the original question about capturing the output to a text file, I would simply use 'diary' instead of an OutputFcn. But that's just me; I like to keep it simple when I can. :)
  1 Comment
Stephen23
Stephen23 on 11 Apr 2016
Edited: Stephen23 on 11 Apr 2016
LuC's "Answer" moved here:
Thank you, using diary seems to be the easiest way. :)

Sign in to comment.

More Answers (2)

Brendan Hamm
Brendan Hamm on 24 Mar 2016
Without having tested this the basic idea you would define the function in the manner described here. You can see the expected syntax and the fields stored in the optimValues input to this function. I give an example of a few inputs:
function stop = outputFcn(x,optimValues,state,fileID)
fprintf(fileID,'%u,%u,%4.2f \r\n',optimValues.iteration,optimValues.funccount, optimValues.fval)
end
If you are not familiar with fprintf, please see the doc .
Also notice we have an extra input fileID to our output function, so we need to make an anonymous function which we will pass fileID to.
fileID = fopen('myLog.txt','a'); % 'a' will append to a file or create it if it doesn't exist.
f = @(x,optimValues,state) outputFcn(x,optimValues,state,fileID);
fileID is now embedded in the anonymous function's workspace (the workspace of f). Pass it to optimoptions and then pass that on to fmincon.
opt = optimoptions('fmincon','OutputFcn',f);
fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,opt); % I have not defined the parameters here.
  5 Comments
Brendan Hamm
Brendan Hamm on 1 Apr 2016
If you can please post the modified code you are using to run this and if possible the data you are using (or some made up data).
LuC
LuC on 1 Apr 2016
fid = fopen('myLog.txt','a+');
f = @(x,optimValues,state) myfun(x,optimValues,state,fid);
options = optimset ('fmincon') ;
options = optimset (options,...
'LargeScale','off',...
'Algorithm','active-set',...
'MaxFunEvals',2000,...
'MaxIter',2000,...
'Display','iter',...
'OutputFcn', f);
- - - - - -
tic
[Param,fval,exitflag,output] = fmincon (@Cost_Fcn,...
Param0,[],[],[],[],...
LB,UB,...
@Boundary_Fcn,...
options,...
Coeff,...
ListCoeffVariable,...
FitStrategy.CostRange,...
Xref,Yref,Timeref,Selected) ;
time = toc;
- - - - - - -
myfun(x,optimValues,state,fid);
fclose(fid);
- - - - - - -
function stop = myfun(x,optimValues,state,fid)
%fid = fopen( 'myLog.txt', 'a' );
fprintf('Iteration: \t, f(x): \t, Directional derivative: \t, FirstOrderOpt: \n');
fprintf(fid,'%u, %u, %4.2f, %4.2f \r\n',optimValues.iteration,optimValues.fval, optimValues.directionalderivative, optimValues.firstorderopt);
%fclose(fid);
x y
-2.0090 -35.0710
-2.0080 -31.2590
-2.0080 -30.8820
-2.0070 -30.4810
-2.0010 -21.4850
-1.9730 -7.1020
-1.9000 -0.6190
-1.7710 -8.8840
-1.6010 -26.5150
-1.4240 -39.9870
-1.2590 -38.5350
-1.1050 -29.2740
-0.9500 -28.0380
-0.7870 -35.6560
-0.6220 -41.3930
-0.4590 -42.4380
-0.3000 -42.0180
-0.1420 -41.3620
0.0190 -42.8060
0.1810 -46.8950
0.3420 -48.0330
0.5030 -44.2170
0.6630 -41.3610
0.8230 -40.8810
0.9840 -38.9310
1.1460 -36.4660
1.3060 -35.6040
1.4670 -33.6470
1.6280 -30.5850
1.7890 -29.7060
1.9490 -28.8190
2.1100 -25.1340
2.2710 -22.8480
2.4330 -23.7890
2.5940 -23.9550
2.7550 -23.5470
2.9150 -24.8020
3.0760 -26.0030
3.2370 -26.6650
3.3980 -28.1070
3.5590 -26.3200
3.7200 -19.9730
3.8800 -17.1940
4.0410 -21.8930
4.2020 -26.9210
4.3620 -28.9170
4.5230 -31.6800
4.6840 -35.0160
4.8450 -37.0100
5.0060 -39.2880

Sign in to comment.


Brendan Hamm
Brendan Hamm on 1 Apr 2016
Ok I see only 2 things wrong here, but there may be more as this obviously isn't ALL of the code.
1. I missed the fact that you need to set the output of your OutputFcn. Inside myfun just set
stop = false;
2. What are all of these extra inputs you are trying to pass to fmincon?
[Param,fval,exitflag,output] = fmincon (@Cost_Fcn,...
Param0,[],[],[],[],...
LB,UB,...
@Boundary_Fcn,...
options,... %%%%%<- This is the last input to the function
Coeff,... %%%%%<- This variable will trigger too many inputs.
ListCoeffVariable,...
FitStrategy.CostRange,...
Xref,Yref,Timeref,Selected) ;
You can't just keep passing more inputs to this function, it only accepts a finite number of inputs in a pre-specified order. This is likely your main cause for error.
3. I can't actually see it, but I imagine that your entire file is a function itself? If not you cannot define a function at the end of it. But this would be the source of your error.
4. This is not going to cause errors, but the way your outputFcn is written it will print your header line at every iteration. I would write that one line directly after opening the file, but not in the outputFcn. Worry about this after your problem actually runs though.
  6 Comments
Brendan Hamm
Brendan Hamm on 6 Apr 2016
It is absolutely impossible to debug the source of errors without knowing what is on Line: 40 Column: 24 where you use an unexpected operator.
This is a similar issue with all of your other errors and the changes you mention. Are you changing the definition of myFun or the assignment to the anonymous function handle? Is the error about unbalanced parenthesis on the line you show? Where are you defining the assignment in1 = FitStategy.CostRange, inside the function declaration or in the Fit_Sin_Function?
The issue is not with the outputFcn, you can see my example above works exactly as expected. Maybe you should consider using the coding approach I mention rather than the extra input arguments to fmincon.
LuC
LuC on 7 Apr 2016
The unexpected operator error is in FitStrategy.CostRange and thus I changed it to in = FitStrategy.CostRange.
Okay, I will try that approach. Thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!