Confronting dates in a constrain

I am implementing an optimisation problem on matlab and one of the constraints imposes an inequality of the type: date(x) * decision variable> date(y) + duration
Obviously I cannot multiply a date by a number so how could I solve the problem?
Thanks in advance for the help!

Answers (1)

Perhaps
decision_variable * (date(x) > date(y) + duration)
leading to
date(y) - date(x) + duration
multiplied by something. But the something is not necessarily the decision variable: it depends on what the intent is when the decision variable is false, whether that is intended to cause the constraint to pass or to fail.

5 Comments

Thanks for the suggestion!
In the meantime I have solved it in another way that seems to work well. I discovered the existence of "datenum" to convert the string date into a double and managed to solve the problems in the code...I hope...
Eric Sofen
Eric Sofen on 27 Feb 2024
Edited: Eric Sofen on 28 Feb 2024
Notice that:
  1. datenum is marked "not recommended" in the documentation. It is an old function with various shortcomings and has been replaced with datetime. We discourage users from using datenum in new code unless there's a particularly compelling reason (and then we want to hear about why datetime doesn't work for that particular situation, so we can improve it).
  2. datenum represents the number of days since the year 0 CE. Does multiplying that time period by decision_variable really make sense for solving your problem? Because of this, Walter's suggested approach seems more fruitful.
datenum() represents the number of days since year 0 Common Era
datenum('jan 1 1970')
ans = 719529
datestr(0)
ans = '00-Jan-0000'
@Eric Sofen This is the code related to the constrain I was talking about:
for n=1:Nr
for k=1:Nr
if (k ~= n)
for v=1:Vr
temp=f(n)+hours(eps(n,k)/mu);
constr_combined_time(n,k,v)=datenum(q(k))*y(n,k,v)>=datenum(temp);
end
end
end
end
the general meaning is that q(k) is the starting date of the job k multiplied by the decision variable which is zero if the job is not part of the optimal solution or it is equal to 1 if the job is executed because it is part of the optimal solution. Basically this date must be later than the finishing date of the job n to which job k is combined to (+ a certain duration).
I'm a beginner using Matlab but I tried implementing Walter's solution and It didn't work in this situation so I'm still using datenum which seems to work well in this particular case.
@Walter Roberson, whoops! Of course, you're right about the epoch for datenum. I went back and edited my post.

Sign in to comment.

Products

Release

R2023b

Asked:

on 26 Feb 2024

Commented:

on 28 Feb 2024

Community Treasure Hunt

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

Start Hunting!