fmincon not obeying nonlcon constraint
Show older comments
Afternoon all,
I'm working with fmincon from the Optimization toolbox, but having some issues with nonlinear constraints. Code follows:
x0 = [0,0];
A = [2 1; 5 2; 0 1; -1 0];
b = [100 140 35 -2];
[breakdown, profit] = fmincon(@ofun, x0, A, b, [], [], [], [], @nlcon);
profit = abs(profit)
breakdown
% Returns true if nonlinear constraint is obeyed
SanityCheck = timetaken([4 3 breakdown(1)]) + timetaken([8 6 breakdown(2)])
SanityCheck - 70 <= 0
function t = timetaken(a)
t0 = 0:1:50;
tlog = (a(2) - a(1))./(1+exp(0.3*(t0-10))) + a(1);
t = sum(tlog([1:a(3)]));
end
function f = ofun(x) % Objective Function
profit = [350 550];
wage = [20];
f = (timetaken([4 3 x(1)])*wage(1) - profit(1)*x(1)) + (timetaken([8 6 x(2)])*wage(1) - profit(2)*x(2));
end
% Worker can only do 70 hours.
function [c, ceq] = nlcon(x)
c(1) = (timetaken([4 3 x(1)])) + (timetaken([8 6 x(2)])) -70;
c(2) = x(1)^2 + x(2) - 25;
c(3) = fsquare(x(1)) + x(2) - 30;
ceq = [];
end
function sq = fsquare(input)
sq = input^2;
end
c(2) and c(3) are debugging constraints I put in to check that the nonlcon function itself was working and that it's ok to call other functions within nonlcon - and they are respected - but the output blows past c(1) as if it isn't there.
I'm sure it's something simple, but whatever it is I'm missing it badly!
Any insight gratefully received!
3 Comments
Bruno Luong
on 19 Nov 2020
Edited: Bruno Luong
on 19 Nov 2020
Your implementation
t = sum(tlog([1:a(3)]))
Make the non-linear constraint and cost function discontinuous, i.e., NOT differentiable as required. It should matter.
GIGO
Alex Griffiths
on 20 Nov 2020
Bruno Luong
on 20 Nov 2020
You might change this discrete sum term to a contuous integral if it makes sens to the problem you want to solve.
Answers (0)
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!