How to define multiple constraints for fmincon?

34 Ansichten (letzte 30 Tage)
Rachel Dawn
Rachel Dawn am 9 Feb. 2021
Beantwortet: Walter Roberson am 9 Feb. 2021
Hi, I'm trying to minimize the formula below. I've done most of the code except I can't quite figure out how to account for the constraints. I tried at the end of the code but I think this is for nonlinear constraints? And mine are linear. Also not sure how to account for the bounds.
Anyone know how I can do this? Thanks!
%FORMULA: SC= 3SA + 4SS + 6LA + 4LS
%BOUNDS:
%SA>= 100 locks
%SS>=100 locks
%LA>=100 locks
%LS>=100 locks
%CONSTRAINTS:
%SA + LA = 2000
%SS + LS = 1500
%LA + LS <= 1500
%SA + SS <= 2500
%All variables need to be positive & integers
%load guesses into array
x0=[SA SS LA LS];
%call solver to minimize objective function given the constraint
xopt= fmincon(@objective, x0, [],[],[],[],[],[],@constraint,[])
%retrieve optimized shipping cost
SC_Opt=ship_cost(xopt)
%double check constraints
%function to calculate FendPac total shipping cost
function ship_cost= ship_cost(x)
SA= x(1);
SS= x(2);
LA = x(3);
LS = x(4);
ship_cost=3*SA + 4*SS + 6*LA + 4*LS;
end
%function to calculate shipped locks to Austin
function to_atx = to_atx(x)
SA= x(1);
LA= x(2);
to_atx= LA + SA
end
%function to calculate shipped locks to San Antonio
function to_satx = to_satx(x)
SS= x(1);
LS= x(2);
to_satx= LS + SS
end
%function to calculate shipped locks from Los Angeles
function from_LA = from_LA(x)
LS= x(1);
LA= x(2);
from_LA= LS + LA
end
%function to calculate shipped locks from San Francisco
function from_SF = from_SF(x)
SS= x(1);
SA= x(2);
from_SF= SS + SA
end
%objective function for optimization
function obj=objective(x)
obj=ship_cost(x);
end
%CONSTRAINTS
function [c,ceq]= constraint1(x)
c(1)= 2000- to_atx(x);
c(2)= 1500- to_satx(x);
c(3)=from_LA(x) - 1500;
c(4)= from_SF(x) - 2500
ceq1=[];
end
  1 Kommentar
Walter Roberson
Walter Roberson am 9 Feb. 2021
function to_atx = to_atx(x)
SA= x(1);
LA= x(2);
to_atx= LA + SA
end
No, LA is x(3) not x(2) .

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 9 Feb. 2021
Aeq = [1 0 1 0; 0 1 0 1];
beq = [2000; 2000];
A = [0 0 1 1; 1 1 0 0];
b = [1500; 2500]
lb = [100, 100, 100, 100];
ub = [1900, 1900, 1900, 1900]; %min 100, sum = 2000, so max = 1900
xopt = fmincon(@objective, x0, A, b, Aeq, beq, lb, ub)

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by