fmincon not obeying nonlcon constraint

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

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
Ahh... ok. I missed that it needs to be a continuous function. Slightly surprising that this didn't produce an error or warning. Thank you!
You might change this discrete sum term to a contuous integral if it makes sens to the problem you want to solve.

Sign in to comment.

Answers (0)

Products

Release

R2020b

Asked:

on 19 Nov 2020

Commented:

on 20 Nov 2020

Community Treasure Hunt

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

Start Hunting!