Borrar filtros
Borrar filtros

can I use a compiled (.exe) file to generate my objective functions while running optimisation in MATLAB?

3 visualizaciones (últimos 30 días)
I have developed a Fortran code which simulates a thermodynamic cycle. I want to optimise the cycle for few objectives (like cost, efficiency etc.). My Fortran program reads design variables [X] from a file and writes the objectives values F[X] in a separate file. I can call the .exe file of my program in MATLAB and it communicates with the input and output files without problem. I want to see whether it is possible to call this .exe file to generate objective functions from [X] in each iteration of optimisation procedure? If yes, which types of optimisation solvers I may choose?

Respuesta aceptada

Andrew Schenk
Andrew Schenk el 17 de Jun. de 2015
Optimization functions like fminunc simply need a MATLAB function of the form y = f(x) to operate on. To interface with your external code, you should create a MATLAB function similar to the one below:
function y = myfun(x)
%write the x data
xh = fopen('x.txt', 'w');
fprintf(xh, '%f', x);
fclose(xh);
%run your program
system('yourProgram.exe');
%read the f data
fh = fopen('f.txt');
y = fscanf(fh, '%f');
fclose(fh);
Then use fminunc:
minunc(@myfun,x0);
  6 comentarios
Mohsen2015
Mohsen2015 el 22 de Jun. de 2015
Thanks
I used fminsearch instead and it worked with no problem either defining the function in MATLAB or calling a compiled Fortran .exe file. I also ran a two objective function optimisation and used "gamultiobj" (the example function given in MATLAB help). No problems and it works properly. Just two questions are still remaining:
1. why I can get correct answer with fminunc regardless of x0 value when define the function directly in MATLAB, but it has problems when calling .exe file? It only gives correct minimum when I choose x0 close enough.
2. Using .exe file increased the calculation time, especially in "gamultiobj". How can I reduce the time?
I'm going to ask this in a separate question.
Kurt Stewart
Kurt Stewart el 18 de Sept. de 2019
fminunc is a local solver - thus it gets stuck in local minima, that is why you would need to have the initial conditions very close to the solution to actually find it in a non-convex problem. gamultiobj uses a genetic algorithm. They just inherently take quite a long time. Try ParetoSearch and see how that works, probably faster than gamultiobj

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Fortran with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by