Using Calendars in AIMMS
The word “programming” in Mathematical Programming refers to creating a plan — typically one executed over a real-time period. This makes real-time references an essential ingredient of most decision support applications. Calendars are the AIMMS mechanism for working with real time. This article covers:
How calendars are constructed flexibly
How calendars relate to date/time fields in database tables
How the current time is mapped to elements in a calendar
How calendar information can be used to construct meaningful subsets, such as all weekend days
Construction of Calendars
A calendar is an AIMMS set — a finite collection of elements.
The elements of a calendar are called timeslots, and each timeslot represents a period of equal length.
To define a calendar, you need to specify the timeslot length, the begin date, and the end date.
The length is expressed using a unit from the quantity SI_Time_Duration.
The format used to present timeslots to users should follow their conventions.
As a running example:
1Section Calendars {
2 Quantity SI_Time_Duration {
3 BaseUnit: s;
4 Conversions: {
5 century->s : #-># * 3153600000,
6 day ->s : #-># * 86400,
7 hour ->s : #-># * 3600,
8 minute ->s : #-># * 60,
9 month ->s : #-># * 2628000,
10 year ->s : #-># * 31536000
11 }
12 Comment: "Expresses the value for the duration of periods."" }
13 Parameter p_def_yearNumber {
14 InitialData: 2026;
15 }
16 StringParameter sp_calBeginDate {
17 Definition: FormatString("%i-01-01", p_def_yearNumber);
18 }
19 StringParameter sp_calEndDate {
20 Definition: FormatString("%i-12-31", p_def_yearNumber);
21 }
22 Calendar cal_daysInYear {
23 Index: i_day;
24 Parameter: ep_day;
25 Unit: day;
26 BeginDate: sp_calBeginDate;
27 EndDate: sp_calEndDate;
28 TimeslotFormat: "%c%y-%sm-%sd";
29 }
30 DeclarationSection Auxiliar_Sets {
31 Set s_weekendDays {
32 SubsetOf: cal_daysInYear;
33 Definition: {
34 { i_day |
35 TimeslotCharacteristic( i_day, 'weekday' ) = 6 or
36 TimeslotCharacteristic( i_day, 'weekday' ) = 7 }
37 }
38 }
39 }
40}
Some remarks on the declaration above:
Lines 2–11: The quantity
SI_Time_Durationis required to define the timeslot length. In this example, only thedayconversion is used.Lines 13–15: The example covers all days in a given year, so
p_def_yearNumberis the only input needed.Lines 16–18:
sp_calBeginDatedefines the first day of the specified year usingFormatString.Lines 19–21:
sp_calEndDatedefines the last day of the year in the same way.Lines 23–30: The calendar itself, using the ISO date format
"%c%y-%sm-%sd"(e.g.2026-01-15). You can choose any format, as long as all timeslots remain unique.Lines 31–39: The subset
s_weekendDaysis declared inside aDeclarationSectionwithin the same section, grouping auxiliary sets alongside the calendar.
Relating Calendars to Date/Time Columns in Databases
A key feature of calendars in AIMMS is the natural mapping to date/time columns in a database. In this example, the data source is a SQLite database. Consider the following table:
with its design view:
Using the AIMMS mapping wizard, the database columns can be mapped to AIMMS identifiers:
Reading the data and displaying it in the WebUI produces:
Using Current Time
AIMMS provides two functions to obtain the current time: CurrentToString and CurrentToTimeSlot.
CurrentToString returns a string, while CurrentToTimeSlot returns a timeslot element. Both are useful in this context.
Initializing the Current Year
CurrentToString returns the current date/time formatted according to its argument.
The current year can be initialized with the following statement, placed in MainInitialization:
p_def_yearNumber := val( CurrentToString("%c%y") );
Further information about CurrentToString can be found in the AIMMS Function Reference.
Obtaining the Current Day as a Calendar Element
CurrentToTimeSlot returns the timeslot corresponding to the current moment. In MainExecution:
ep_day := CurrentToTimeSlot(Calendar : cal_daysInYear );
Further information about CurrentToTimeSlot can be found in the AIMMS Function Reference.
Creating Subsets Based on Timeslot Characteristics
The subset s_weekendDays is a subset of cal_daysInYear containing all weekend days.
In AIMMS, Saturday is day 6 and Sunday is day 7 of the week. The subset is defined as follows:
Set s_weekendDays {
SubsetOf: cal_daysInYear;
Definition: {
{ i_day |
TimeslotCharacteristic( i_day, 'weekday' ) = 6 or
TimeslotCharacteristic( i_day, 'weekday' ) = 7 }
}
}