Optimization in which the function varies with its own solution
1 view (last 30 days)
Show older comments
In this case I dont want a code being made but rather an idea to approach the "optimization" problem that I am about to explain.
I will first attempt to explain the simple problem, easy to solve with linprog: I have a car which can only be powered by fuel cells, batteries or both. For a certain drive (i.e. 2 hours) we are considering 2 travel phases: the high power (at the beginning until it reaches a desidered speed) and the high energy phase (after the desired speed is reach and it should be kept constant) and I want to be able to optimize the amount (multipliers) of batteries and the amount of fuel cells necessary for the trip with regard to their total weight. The function to be optimized is the total weight of batteries+fuel cells+hydrogen for phase 1 and for phase 2: (Fuel cell weight*x1+ Fuel cell weight*x2+ Battery weight*x3+Battery weight*x4+H2 reference weight*x5).
where:
x1= multiplier of fuel cells for first phase
x2=multiplier of fuel cells for second phase
x3=multiplier of batteries for first phase
x4= multiplier of batteries for second phase
x5=multiplier for H2 .
Important for understanding: there is one type of Battery stack with a specific power and a specific weight and only one fuel cell stack with a specific power and a specific weight. These will be scalated to reach the total power needed during each phase. Likewise theres only one H2 reference weight, which will be multiplied with respect to the total power the fuel cells will need to provide.
Now, I wrote a series of constraints such as:
-the power of the batteries used + the power of the fuel cells used should be equal to the power needed for phase 1: P_fuelcell*x1+P_batt*x3=1500kW
-the power of the batteries used + the power of the fuel cells used should be equal to the power needed for phase 2: P_fuelcell*x2+P_batt*x4=500kW
-The total amount of hydrogen needed is equal to a formula derived from the total power of fuel cells for the phases 1 and 2 above, something like this: (Fuel cell power in phase 1 + Fuel cell power in phase 2) *constant / efficiency.
- the total weight shouldn´t exceed a certain value (limiting the function f)
With the above, Matlab found the optimal solution: it gave me the vector [x1,x2,x3,x4,x5].
But now the alteration with which I need help:
now some of the constraints and the function change/vary depending on the results themselves... For example:
The function will change with regard to x1 and x2: if x1 is bigger than x2, the the function will change to: Fuel cell weight*x1+ zero (0) *x2+ Battery weight*x3+Battery weight*x4+H2 reference weight*x5. and if x2 is bigger than x1 it will be: zero(0)*x1+ Fuel cell weight*x2+ Battery weight*x3+Battery weight*x4+H2 reference weight*x5.
Also the efficiency used for the hydrogen calculation changes with regard to the total amount of power the fuel cells are chosen to delivered (Power of fuel cell stack *(x1+x2)).
I know I will not be able to define this problem as a simple linprog anymore, nor a simple optimization, so it would be of great help if someone could provide some guidence on how I could approach this problem... The whole problem is of course much bigger that the stated above but I tried to simplify it as much as possible... Thanks in advance!
0 Comments
Answers (1)
Daniel Vieira
on 2 Mar 2020
I'm not sure I fully understood it because to me it still seems simple. Instead of writing:
objFun=@(X) Fuel_cell_weight*x(1)+ Fuel_cell_weight*x(2)+ Battery_weight*x(3)+Battery_weight*x(4)+H2_reference_weight*x(5);
Write:
function f=myFun(x)
% your whole complicated function with all if's and else's
end
objFun=@myFun;
And if you think linprog is no longer appropriate for this case, use fmincon. It's slower, but should do the job, and you can even specify additional nonlinear constraints.
2 Comments
Daniel Vieira
on 23 Mar 2020
sorry for taking long...
for difficult constraints you can use the non-linear constraints of fmincon. write a function the same way suggested for the objective function:
function [c,ceq]=myConstraints(x)
% write inequality constraint c, as complicated as needed: fmincon attempts to satisfy c<=0
% write equality constraint ceq, as complicated as needed: fmincon attempts to satisfy ceq==0
end
then pass this to the non-linear constraint argument of fmincon as a function handle:
nlcons=@myConstraints
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!