fmincon with simulink model
Mostrar comentarios más antiguos
Hello, I'm struggling with fmincon, as I'm still a beginner. I have a Simulink model that simulates the behavior of an electric vehicle and a track specification (maximum speed in different sections, total length).
The model takes as input the throttle and brake pedal pressures (steering is not relevant in this case) and outputs various vehicle dynamics parameters such as speed, jerk, acceleration, and SoC.
My goal is to determine the optimal pedal usage to complete the track while maximizing the remaining SoC, using the results obtained from the model.
I wrote a MATLAB script for this, but the throttle and brake pedal vectors never change—they always remain the same as the initial values u0 that I provide.
Does anyone know what might be causing this issue?
My cost function is:
function [cost, c, ceq] = myCostAndConstraints(u, timeVec, steeringTS, w1, w2, w3, w4, w5, total_distance)
N = length(u)/2;
throttleProfile = u(1:N);
brakeProfile = u(N+1:end);
myThrottleTs = timeseries(throttleProfile, timeVec);
myBrakeTs = timeseries(brakeProfile, timeVec);
in = Simulink.SimulationInput('optimization_offline_model');
in = in.setVariable('myThrottleTs', myThrottleTs);
in = in.setVariable('myBrakeTs', myBrakeTs);
in = in.setVariable('steeringWheelAngle_ts', steeringTS);
try
simOut = sim(in);
catch ME
warning("Simulation error: %s" + ME.message);
cost = 1e6;
c = 1e5;
ceq = [];
return;
end
try
timeSim = simOut.tout;
speedData = simOut.v_z.Data;
SoCData = simOut.SoC.Data;
jerkData = simOut.j_z.Data;
distanceData = simOut.distance.Data;
catch ME
warning("Error extracting signals: " + ME.message);
cost = 1e6;
c = 1e5;
ceq = [];
return;
end
SoC_final = SoCData(end);
time_elapsed = timeSim(end);
distance_final = distanceData(end);
penalty = 0;
if any(SoCData < 0.2)
penalty = penalty + w1 * (0.2 - min(SoCData(SoCData<0.2)));
end
vLimHard = 41.5;
violV = speedData - vLimHard;
violV(violV<0) = 0;
if any(violV > 0)
penalty = penalty + w5 * sum(violV);
end
avgJerk = mean(jerkData);
maxJerk = max(abs(jerkData));
penalty = penalty + w3*(max(0, 0.2 - avgJerk))^2 + w3*(max(0, avgJerk - 0.7))^2;
if avgJerk > 0.6
penalty = penalty + w3*(avgJerk - 0.6);
end
if maxJerk > 0.9
penalty = penalty + w3*(maxJerk - 0.9);
end
cost = - w1*SoC_final + w2*(time_elapsed) + w4 * abs(total_distance - distance_final) + penalty;
disp("Cost function =" + cost);
vLimVec = getVelocityLimit(distanceData, vLimHard);
c = speedData - vLimVec;
ceq = throttleProfile .* brakeProfile;
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Simulink en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!