Hi,
Im using amplpy and working on a call center scheduling problem where the input data contains shift start and end times and duration, all in the HH:MM format. I also want to be able to use comparative operators like <, > on these time values.
I would appreciate any advice on how to pass this data and use it in an AMPL model. I’m able to read the data into a datetime format in Python, but haven’t figured out how to pass it into AMPL.
Thanks for any pointers!
Atma
1 Like
Hi @Atma_Iyer,
You can do that by converting the strings to numbers as follows:
from amplpy import AMPL
import pandas as pd
ampl = AMPL()
ampl.eval("""
set EVENT;
param start{EVENT};
param end{EVENT};
""")
event_df = pd.DataFrame(
[
(1, "10:15", "10:30"),
(2, "11:45", "13:10"),
(3, "10:04", "10:15"),
(4, "12:05", "12:40"),
],
columns=["EVENT", "start", "end"],
).set_index("EVENT")
timestr_to_int = lambda v: int(v.replace(":", ""))
timestr_to_minutes = lambda v: int(v[:v.find(":")])*60 + int(v[v.find(":")+1:])
event_df["start"] = event_df["start"].apply(timestr_to_int)
event_df["end"] = event_df["end"].apply(timestr_to_int)
ampl.set_data(event_df, "EVENT")
ampl.eval("display start, end;")
This produces the following output:
: start end :=
1 1015 1030
2 1145 1310
3 1004 1015
4 1205 1240
;
You can choose between converting the strings directly to integers (timestr_to_int
) or to minutes (timestr_to_minutes
) in case you also want to calculate differences between times. If you had complete timestampts for the events you could use unix timestamps.
Hi Filipe,
Thanks so much for the great help. I have successfully used the timestr_to_minutes and the program runs well. We also implemented this model in Python/Pyomo and that is able to handle this data without the need for any conversion.
This may be a useful enhancement in a future AMPL release - supporting time data / timestamp values, especially for scheduling problems.
Once again, I really appreciate your responsiveness and tremendous help.
Regards,
Atma
1 Like
Hi @Atma_Iyer,
Thank your for your feedback! For performance reasons AMPL only allows comparisons <, <=, >, >= between numbers. Comparing strings can have substantial performance impacts on huge models (millions of variables and constraints). We are looking into a way to allow this, eventually by making a strcmp
function available by default initially which would allow writing conditions like strcmp(time1, time2) < 0
. I have just opened an issue for this feature request.
Please let us know of anything else you notice that may help making AMPL easier to use!
Thanks so much for the consideration and follow up. The performance consideration is huge - great point. Maybe a numeric representation like timestamp would be powerful. AMPL is incredibly efficient and a key differentiator in my mind, so I’m sure if and when something is done it will be great.
Ona separate note I was also able to use the symbolic parameter option recently and it is fantastic.
Thanks again
Atma
1 Like