How to save 'display-iter' from fmincon to a .txt file?
15 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Les Beckham
2016-4-7
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 个评论
更多回答(2 个)
Brendan Hamm
2016-3-24
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
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 个评论
Brendan Hamm
2016-4-1
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).
Brendan Hamm
2016-4-1
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 个评论
Brendan Hamm
2016-4-6
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!