How to check if my linear constraints and bounds are infeasible & reduce execution time?
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm making a code where it creates an optimized engine usage schedule based on engine's reliability using genetic algorithm solver. The following is the code that I've made:
tic
clear
% Create arrays to store total Hour & daily result
totalHourWeekday = zeros(5,13);
result = zeros(5,13);
% Calculate the maximum engine by adding all of the engine's status
engineStatus = ones(1,13);
maxEngine = sum(engineStatus);
% Initialize optimization problem
rel = zeros(1,13);
decision = optimproblem("ObjectiveSense","max");
engine = optimvar('engine',13,'Type','integer','LowerBound',0,'UpperBound',1);
% Define days
for day = 1:5
% calculate maximum duration
maxDuration = 24*max(day);
% Initialize demand
for demand = 1:5
disp(['Day: ',num2str(day),' Demand: ',num2str(demand)])
switch demand
case 1
hourDemand = 3;
engineDemand = 9;
% Calculate reliability based on the sum between the hour
% demand & previous hour
if day == 1
timeCalc = hourDemand + totalHourWeekday(demand,:);
else
timeCalc = hourDemand + result(day-1,:);
end
rel = exp(-(4.44*10^-5*timeCalc));
relSys = 1-((1-engine(1)*rel(1,1))*(1-engine(2)*rel(1,2))*(1-engine(3)*rel(1,3))*(1-engine(4)*rel(1,4))*(1-engine(5)*rel(1,5))*(1-engine(6)*rel(1,6))*(1-engine(7)*rel(1,7))*(1-engine(8)*rel(1,8))*(1-engine(9)*rel(1,9))*(1-engine(10)*rel(1,10))*(1-engine(11)*rel(1,11))*(1-engine(12)*rel(1,12))*(1-engine(13)*rel(1,13)));
% Set reliability as objective function
decision.Objective = relSys;
% Initialize constraint #1: engines should be equal to the
% demand
engineMaintOrNot = sum(engine*engineStatus);
decision.Constraints.engineDemandConstraint = engineMaintOrNot == engineDemand;
% Initialize constraint #2: engine's hour should be less
% than max duration
if day == 1
decision.Constraints.durationConstraint = engine*totalHourWeekday(demand,:) <= maxDuration;
else
decision.Constraints.durationConstraint = engine*result(day-1,:) <= maxDuration;
end
% Solve optimization problem
sol = solve(decision);
% Add the hour demand to the chosen engines
for i = 1:13
if sol.engine(i) == 1
totalHourWeekday(demand,i) = totalHourWeekday(demand,i) + hourDemand;
end
end
case 2
hourDemand = 2;
engineDemand = 6;
timeCalc = hourDemand + totalHourWeekday(demand-1,:);
rel = exp(-(4.44*10^-5*timeCalc));
relSys = 1-((1-engine(1)*rel(1,1))*(1-engine(2)*rel(1,2))*(1-engine(3)*rel(1,3))*(1-engine(4)*rel(1,4))*(1-engine(5)*rel(1,5))*(1-engine(6)*rel(1,6))*(1-engine(7)*rel(1,7))*(1-engine(8)*rel(1,8))*(1-engine(9)*rel(1,9))*(1-engine(10)*rel(1,10))*(1-engine(11)*rel(1,11))*(1-engine(12)*rel(1,12))*(1-engine(13)*rel(1,13)));
decision.Objective = relSys;
engineMaintOrNot = sum(engine*engineStatus);
decision.Constraints.engineDemandConstraint = engineMaintOrNot == engineDemand;
decision.Constraints.durationConstraint = engine*totalHourWeekday(demand-1,:) <= maxDuration;
sol = solve(decision);
for i = 1:13
if sol.engine(i) == 1
totalHourWeekday(demand,i) = totalHourWeekday(demand,i) + hourDemand;
end
end
case 3
hourDemand = 5;
engineDemand = 10;
timeCalc = hourDemand + totalHourWeekday(demand-1,:);
rel = exp(-(4.44*10^-5*timeCalc));
relSys = 1-((1-engine(1)*rel(1,1))*(1-engine(2)*rel(1,2))*(1-engine(3)*rel(1,3))*(1-engine(4)*rel(1,4))*(1-engine(5)*rel(1,5))*(1-engine(6)*rel(1,6))*(1-engine(7)*rel(1,7))*(1-engine(8)*rel(1,8))*(1-engine(9)*rel(1,9))*(1-engine(10)*rel(1,10))*(1-engine(11)*rel(1,11))*(1-engine(12)*rel(1,12))*(1-engine(13)*rel(1,13)));
decision.Objective = relSys;
engineMaintOrNot = sum(engine*engineStatus);
decision.Constraints.engineDemandConstraint = engineMaintOrNot == engineDemand;
decision.Constraints.durationConstraint = engine*totalHourWeekday(demand-1,:) <= maxDuration;
sol = solve(decision);
for i = 1:13
if sol.engine(i) == 1
totalHourWeekday(demand,i) = totalHourWeekday(demand,i) + hourDemand;
end
end
case 4
hourDemand = 8;
engineDemand = maxEngine-1;
timeCalc = hourDemand + totalHourWeekday(demand-1,:);
rel = exp(-(4.44*10^-5*timeCalc));
relSys = 1-((1-engine(1)*rel(1,1))*(1-engine(2)*rel(1,2))*(1-engine(3)*rel(1,3))*(1-engine(4)*rel(1,4))*(1-engine(5)*rel(1,5))*(1-engine(6)*rel(1,6))*(1-engine(7)*rel(1,7))*(1-engine(8)*rel(1,8))*(1-engine(9)*rel(1,9))*(1-engine(10)*rel(1,10))*(1-engine(11)*rel(1,11))*(1-engine(12)*rel(1,12))*(1-engine(13)*rel(1,13)));
decision.Objective = relSys;
engineMaintOrNot = sum(engine*engineStatus);
decision.Constraints.engineDemandConstraint = engineMaintOrNot == engineDemand;
decision.Constraints.durationConstraint = engine*totalHourWeekday(demand-1,:) <= maxDuration;
sol = solve(decision);
for i = 1:13
if sol.engine(i) == 1
totalHourWeekday(demand,i) = totalHourWeekday(demand,i) + hourDemand;
end
end
case 5
% Special case
for i = 1:13
if engineStatus(1,i) == 1
totalHourWeekday(demand,i) = totalHourWeekday(demand,i) + 6;
end
end
end
% Add the total hour from all hour demands within the day
if day == 1
result(day,:) = sum(totalHourWeekday);
else
result(day,:) = result(day-1,:) + sum(totalHourWeekday);
end
end
end
The code ran well until it reached 4th day. When calculating 4th day's 1st demand, it gave me the following error:
I guessed that, since it gave the infeasible linear constraints and bounds, an error was detected on line 57. That's why I am wondering how to solve this problem or how to check for linear constraints and bounds infeasibility?
Furthermore, is there any way to reduce the code's execution time?
Thank you in advance
4 个评论
Torsten
2024-6-14
I don't know the background of your problem, but usually - if you make schedules for a certain time span - you cannot do this successively (day by day), but for the complete period because decisions taken at day i influence decisions on days before and after day i.
采纳的回答
Nipun
2024-6-17
Hi Kahari,
I understand that you are receiving an out of bounds error when indexing in a data structure in MATLAB and intend to know the root cause of this error.
You can create a filter to investigate the infeasibilities of the linear programming constraints in MATLAB. For more information on creating such filters, refer to the following MathWorks documentation: https://www.mathworks.com/help/optim/ug/investigate-linear-infeasibilities.html
Hope this helps.
Regards,
Nipun
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!