# Model sigmoid function as convex optimisation with logical constraint

Hello,

I was wondering if I could get support regarding a model I am doing. My research work lead me to the need to implement a fitting function into my model in CVX, however this fitting function is not convex as it is a sigmoid. I have tried dividing the function into two sections based on my decision variable Di but this does not seem to be working and I am getting the following error: Constraints may not appear in if/then statements.

My problem is quite big so I will just show a fragment of the problem:

param=[9.76584915402396,27.9904155442713,0.252459657607282,-39.8453332837601];

cvx_begin
variable Di(N,T)
expression Mk(N,T)
expression M(N,T)
expression fsigm(1,T)

for t=1:24
if Di(1,t)<=(param(3))
fsigm = -(param(1)+(param(2)-param(1))./(1+10.^((param(3)-Di(1,t))*param(4))));
M(t)=-(fsigm);
else
fsigm = param(1)+(param(2)-param(1))./(1+10.^((param(3)-Di(1,t))*param(4)));
M(t)=fsigm;
end
end
Mk=ones(300,24).*M;

The goal is to maximize Mk.

Should I switch to YALMIP to be able to still use MATLAB?.

Thanks a lot!

If your problem is not convex, then CVX is not applicable.

1 Like

In CVX, you would need to manually do the Big M modeling in order to handle logic constraints. YALMIP has automated tools for that, such as `implies`, as well as ability to handle non-convex models.

Edit: I just looked more carefully at your model. The two possibilities for `fsgm` appear to be negatives of each other; therefore, unless `param(1)+(param(2)-param(1) == 0`, one of these will be convex and the other concave. With the `param` value you show, that is not the case. Therefore, even with Big M modeling, this would still be a non-convex problem.

1 Like

Thank you!. Sorry actually the minus sign in the first equation is a mistake. I thought I could divide the decreasing sigmoid function in the inflection point to program this as two separate functions.

I am now unclear on what your model is:

Here is a simplified example of what you can do, which is similar to your problem.

``````cvx_begin
variable x
maximize(-6*inv_pos(10^(3+5*x)))
-3 <=x <= 4
cvx_end
``````

or basically the same thing

``````cvx_begin
variable x
minimize(6*inv_pos(10^(3+5*x)))
-3 <=x <= 4
cvx_end``````

Thank you so much for answers. Basically I would like to model a decreasing sigmoid function. My original problem is:

``````param=[9.76584915402396,27.9904155442713,0.252459657607282,-39.8453332837601];

cvx_begin
variable Di(N,T)
expression Mk(N,T)
expression M(N,T)
expression fsigm(1,T)

param=[9.76584915402396,27.9904155442713,0.252459657607282,-39.8453332837601];

fsigm = param(1)+(param(2)-param(1))./(1+10.^((param(3)-Di(1,:))*param(4)))
M=fsigm;
Mk=ones(300,24).*M;
``````

The goal is to maximize Mk.

The error I received is: Illegal operation: {real constant} + {log-concave}

I didn’t read the scope of the parentheses carefully enough. That first additive term is problematic. CVX will accept

``````cvx_begin
variable x
maximize((param(2)-param(1))*inv_pos(1+10^(param(3)-x*param(4))))
cvx_end
``````

The first step is to prove (for any logic branch of your constraints), that your problem is convex. I’m rather dubious yours is.

1 Like

Thank you so much!. I have tried with:

fsigm = param(1)+(param(2)-param(1)).*inv_pos(1+10.^((param(3)-Di(1,:))*param(4)))

I still have the same error though: Illegal operation: {real constant} + {log-concave}.

Thanks anyway!.

As I noted. That’s why I omitted the additive term when showing you what CVX would accept.

1 Like

Hi Mark,

Thank you, I omitted the additive term. I get the same error but in a constant constraint ahaha story of my life: Illegal operation: {real constant} + {log-concave} / Error in OptimConcave (line 138) / DCi = 0.042*(BatCi/5000)+((1-Efi^2)/Efi).*Mk;%NXT.

I think I can’t omit the additive term as the optimisation will give me values that do not make sense for what I would like in my optimisation problem.

Thanks again!.