close Warning: Can't synchronize with repository "(default)" (/var/svn/mms does not appear to be a Subversion repository.). Look in the Trac log for more information.

Ticket #487: functions.tol

File functions.tol, 11.6 KB (added by lmperez, 14 years ago)
Line 
1//////////////////////////////////////////////////////////////////////////////
2// File:    functions.tol
3// Purpose: It defines functions to create useful general input variables:
4// summer, Easter, Christmas & festives
5//////////////////////////////////////////////////////////////////////////////
6
7//////////////////////////////////////////////////////////////////////////////
8// Structures
9//////////////////////////////////////////////////////////////////////////////
10
11Struct InputDef(
12  Polyn Omega,
13  Serie X
14);
15
16//////////////////////////////////////////////////////////////////////////////
17// Constants
18//////////////////////////////////////////////////////////////////////////////
19
20Set Standard.Variables = Empty;
21
22//////////////////////////////////////////////////////////////////////////////
23// Functions
24//////////////////////////////////////////////////////////////////////////////
25
26//////////////////////////////////////////////////////////////////////////////
27TimeSet Festive.National(Text counrty)
28//////////////////////////////////////////////////////////////////////////////
29{
30  Set s.Brazil = [[ "Brasil", "brasil", "BRASIL", "Brazil","brazil", "BRAZIL",
31    "Bra", "bra", "BRA"
32  ]];
33  Set s.Spain = [[ "España", "españa", "ESPAÑA", "Spain","spain", "SPAIN",
34    "Spa","spa", "SPA"
35  ]];
36
37  Set s.Countries = s.Brazil + s.Spain;
38
39  TimeSet If(Not(counrty <: s.Countries),
40  {
41    WriteLn("[Festive.National] There are not definited national festives "+
42      "of: "+country,"W");
43    TimeSet W
44  },
45  {
46    TimeSet Festive.National.Brazil(Real void)
47    {
48      Set festives = [[
49        TimeSet DiaDoTravalho        = D(1)*M(5),
50        TimeSet RevolConst           = D(9)*M(7),
51        TimeSet IndepBras            = D(7)*M(9),
52        TimeSet NossaSenApar         = D(12)*M(10),
53        TimeSet ServidorPublico      = D(28)*M(10),
54        TimeSet Finados              = D(2)*M(11),
55        TimeSet ProclamacaoRepublica = D(15)*M(11),
56        TimeSet Tiradentes           = D(21)*M(4)
57      ]];
58 
59      TimeSet BinGroup("+",festives)
60    };
61 
62    TimeSet Festive.National.Spain(Real void)
63    {
64      Set festives = [[
65        TimeSet Constitucion   = D(6)*M(12),
66        TimeSet DiaDelPilar    = D(12)*M(10),
67        TimeSet DiaDelTrabajo  = D(1)*M(5),
68        TimeSet Inmaculada     = D(8)*M(12),
69        TimeSet TodosLosSantos = D(1)*M(11),
70        TimeSet VirgenAgosto   = D(15)*M(8)
71      ]];
72 
73      TimeSet BinGroup("+",festives)
74    };
75
76    TimeSet Case(
77      counrty <: s.Brazil, Festive.National.Brazil(?),
78      counrty <: s.Spain,  Festive.National.Spain(?),
79      True,                TimeSet W
80    )
81  })
82};
83//////////////////////////////////////////////////////////////////////////////
84PutDescription("It builds the timeset of national festives by country.",
85Festive.National);
86//////////////////////////////////////////////////////////////////////////////
87
88
89//////////////////////////////////////////////////////////////////////////////
90Set Build.Festives.Struct(TimeSet festives, Real referenceDay)
91//////////////////////////////////////////////////////////////////////////////
92{
93  Real    Yini   = 1981;
94  Real    Yfin   = Year(Today)+3;
95  TimeSet limit  = BinGroup("+",For(Yini,Yfin,TimeSet (Real j){ Y(j) }));
96  TimeSet limFes = festives*limit;
97
98  // It gives the relative frequency coefficient between festives by day
99  Real CoefNor(Real k)
100  {
101    TimeSet tms.k = limFes*WD(k);
102    TimeSet tms.R = limFes*WD(referenceDay);
103
104    Real _k   = SumS(CalInd(tms.k,Diario));
105    Real _ref = SumS(CalInd(tms.R,Diario));
106
107    Real If(_ref, _k/_ref,
108    {
109      Text WriteLn("[Build.Festives.Struct][CoefNor]: You have selected a not"
110      +" valid reference day. There are not festives in WD("+
111      IntText(referenceDay)+") within your sample","W");
112      Real Stop
113    })
114  };
115
116  // It creates a generic input for the festives
117  Set festives.Nationals = [[
118    Set InputDef(
119      Polyn 1,
120      Serie Fes.National = SubSer(CalInd(festives,Diario),DefFirst,DefLast)
121    )
122  ]];
123
124  // It creates the inputs fitters for the festive inputs
125  Set festives.Fitters = For(1,7,Set (Real k)
126  {
127    Set If(EQ(k,referenceDay), Empty,
128    {
129      Text name = "Fes.Fit.WD"+IntText(k);
130      Serie fes.k = CalInd(festives*WD(k),Diario);
131      Serie fes.r = CalInd(festives*WD(referenceDay),Diario)*CoefNor(k);
132      Serie fes.f = SubSer(fes.k - fes.r,DefFirst,DefLast);
133
134      Set InputDef(Polyn 1,Serie PutName(name,fes.f))
135    })
136  }) - [[ Empty ]];
137
138  // Set of satellites
139  Set Satellites = [[
140    Set WD.1 = [[ Real -1 ]],
141    Set WD.2 = [[ Real -1 ]],
142    Set WD.3 = Empty,
143    Set WD.4 = [[ 1,2,3 ]],
144    Set WD.5 = [[ 1,2 ]],
145    Set WD.6 = [[ Real -1 ]],
146    Set WD.7 = [[ 1 ]]
147  ]];
148
149  // It creates the inputs satellites for the festive inputs
150  Set festives.Satellites = BinGroup("<<",For(1,7,Set (Real j)
151  {
152    Set If(Not(Card(Satellites[j])), Empty,
153      Set EvalSet(Satellites[j],Set (Real k)
154      {
155        Text    succ  = If(GT(k,0),IntText(k),"_"+IntText(SqRt(k^2)));
156        Text    name  = "Fes.Sat.Succ"+succ+".Fes."+Name(Satellites[j]);
157        TimeSet tms   = festives*WD(j);
158        Serie   sat.k = SubSer(
159          CalInd(Succ(tms,k,Diario),Diario),
160          DefFirst,
161          DefLast
162        );
163
164        Set InputDef(Polyn 1,Serie PutName(name,sat.k))
165      })
166    )
167  }) - [[ Empty ]]);
168
169  Set struct = festives.Nationals << festives.Fitters << festives.Satellites;
170
171  Set EvalSet(struct,Real (Set idef)
172  {
173    Set Standard.Variables := Standard.Variables + [[ Serie idef->X ]]
174  });
175
176  Set struct
177};
178//////////////////////////////////////////////////////////////////////////////
179PutDescription("It builds an InputDef structure for festives in three steps:
1801-> A generic input for all festives.
1812-> A set of fitters per week day.
1823-> A set of satellites around festives.
183",
184Build.Festives.Struct);
185//////////////////////////////////////////////////////////////////////////////
186
187
188//////////////////////////////////////////////////////////////////////////////
189Set Build.KeyDays.Series(TimeSet day, Text label)
190//////////////////////////////////////////////////////////////////////////////
191{
192  Set For(1,7,Serie (Real k)
193  {
194    Serie input = CalInd(day*WD(k),Diario);
195
196    Text label2 = If(label == "",
197      "Holiday.key.WD"+IntText(k),
198      label+".WD"+IntText(k)
199    );
200
201    Serie PutName(label2,input)
202  })
203};
204//////////////////////////////////////////////////////////////////////////////
205PutDescription("",
206Build.KeyDays.Series);
207//////////////////////////////////////////////////////////////////////////////
208
209
210//////////////////////////////////////////////////////////////////////////////
211Set Build.Holiday.KeyDays(TimeSet iniSat)
212//////////////////////////////////////////////////////////////////////////////
213{
214  TimeSet summer       = M(6)+M(7)+M(8)+M(9);
215  TimeSet Christmas    = M(11)+M(12)+M(1);
216  Real check.summer    = SumS(CalInd(iniSat*summer,Diario));
217  Real check.Christmas = SumS(CalInd(iniSat*Christmas,Diario));
218
219  TimeSet tmsSat_00 = Range(iniSat, 0, 6, Diario)*WD(6);
220  TimeSet tmsMon_00 = Succ(tmsSat_00, 2, Diario);
221
222  Serie holiday.IndSem_00_WD5  = F:CalInd(tmsSat_00, Diario);
223  Serie holiday.IndSem_00_WD6  =   CalInd(tmsSat_00, Diario);
224  Serie holiday.IndSem_00_WD7  = B:CalInd(tmsSat_00, Diario);
225  Serie holiday.IndSem_00_Rest = (1+B+B^2+B^3):CalInd(tmsMon_00, Diario);
226
227  Set key.Days = Case(
228    check.summer,
229    {
230      Text label = "Summer.key.";
231
232      Set Inp.15Jun = Build.KeyDays.Series(D(15)*M(6), label+"15Jun");
233      Set Inp.01Jul = Build.KeyDays.Series(D(1)*M(7),  label+"01Jul");
234      Set Inp.15Jul = Build.KeyDays.Series(D(15)*M(7), label+"15Jul");
235      Set Inp.01Aug = Build.KeyDays.Series(D(1)*M(8),  label+"01Aug");
236      Set Inp.15Aug = Build.KeyDays.Series(D(15)*M(8), label+"15Aug");
237      Set Inp.01Sep = Build.KeyDays.Series(D(1)*M(9),  label+"01Sep");
238      Set Inp.15Sep = Build.KeyDays.Series(D(15)*M(9), label+"15Sep");
239   
240      Set Inp.15Jun << Inp.01Jul << Inp.15Jul << Inp.01Aug << Inp.15Aug <<
241        Inp.01Sep << Inp.15Sep
242    },
243    check.Christmas,
244    {
245      Text label = "Christmas.key.";
246
247      Set Inp.24Dec = Build.KeyDays.Series(D(24)*M(12), label+"24Dec");
248      Set Inp.25Dec = Build.KeyDays.Series(D(25)*M(12), label+"25Dec");
249      Set Inp.31Dec = Build.KeyDays.Series(D(31)*M(12), label+"31Dec");
250      Set Inp.01Jan = Build.KeyDays.Series(D(1)*M(1),   label+"01Jan");
251   
252      Set Inp.24Dec << Inp.25Dec << Inp.31Dec << Inp.01Jan
253    },
254    True, Empty
255  );
256
257  Set key.Days << [[
258    holiday.IndSem_00_WD5,
259    holiday.IndSem_00_WD6,
260    holiday.IndSem_00_WD7,
261    holiday.IndSem_00_Rest
262  ]]
263};
264//////////////////////////////////////////////////////////////////////////////
265PutDescription("It builds the set of series of key days in holidays, and the
266weekend days arround",
267Build.Holiday.KeyDays);
268//////////////////////////////////////////////////////////////////////////////
269
270
271//////////////////////////////////////////////////////////////////////////////
272Set Build.Weekly.HolidayStruct(TimeSet iniSat, Real numSem)
273//////////////////////////////////////////////////////////////////////////////
274{
275  TimeSet summer       = M(6)+M(7)+M(8)+M(9);
276  TimeSet Christmas    = M(11)+M(12)+M(1);
277  Real check.summer    = SumS(CalInd(iniSat*summer,Diario));
278  Real check.Christmas = SumS(CalInd(iniSat*Christmas,Diario));
279
280  Text label = Case(
281    check.summer,   "Summer.IndWeek.",
282    check.Christmas,"Christmas.IndWeek.",
283    True,           "Holidays.IndWeek."
284  );
285
286  Set sKeyDays = Build.Holiday.KeyDays(iniSat);
287
288  Set sIndWeeks = For(1,numSem,Set (Real n)
289  {
290    TimeSet rng.Sat = Range(iniSat, 0, 6, Diario)*WD(6);
291    TimeSet tmsSatN = Succ(rng.Sat, n, WD(6));
292    TimeSet tmsMonN = Succ(Succ(rng.Sat,2,Diario),n,WD(1));
293
294    Serie srWD5  = F:CalInd(tmsSatN, Diario);
295    Serie srWD6  =   CalInd(tmsSatN, Diario);
296    Serie srWD7  = B:CalInd(tmsSatN, Diario);
297    Serie srRest = (1+B+B^2+B^3):CalInd(tmsMonN, Diario);
298
299    Text nameWD5  = label+FormatReal(n, "%02.0lf")+"_WD5";
300    Text nameWD6  = label+FormatReal(n, "%02.0lf")+"_WD6";
301    Text nameWD7  = label+FormatReal(n, "%02.0lf")+"_WD7";
302    Text nameRest = label+FormatReal(n, "%02.0lf")+"_Rest";
303
304    Set If(LT(n, numSem),
305      [[
306        Serie PutName(nameWD5,srWD5),
307        Serie PutName(nameWD6,srWD6),
308        Serie PutName(nameWD7,srWD7),
309        Serie PutName(nameRest,srRest)
310      ]],
311      [[
312        Serie PutName(nameWD5,srWD5),
313        Serie PutName(nameWD6,srWD6),
314        Serie PutName(nameWD7,srWD7)
315      ]]
316    )
317  });
318
319  Set result = BinGroup("<<",sIndWeeks) << sKeyDays;
320
321  Set sHolidayStruct = EvalSet(result,Serie (Serie sr)
322  {
323    Text name = Identify(sr);
324    Serie sr2 = SubSer(sr,DefFirst,DefLast);
325
326    Serie PutName(name,sr2)
327  });
328
329  Set struct = EvalSet(sHolidayStruct,Set (Serie s)
330  {
331    Set InputDef(Polyn 1, Serie s)
332  });
333
334  Set EvalSet(struct,Real (Set idef)
335  {
336    Set Standard.Variables := Standard.Variables + [[ Serie idef->X ]]
337  });
338
339  Set struct
340};
341//////////////////////////////////////////////////////////////////////////////
342PutDescription("Devuelve la estructura de escalones de variables por semana,
343en Sol y NotSol, definidos de manera que todos los inputs toman valor todos
344los años.",
345Build.Weekly.HolidayStruct);
346//////////////////////////////////////////////////////////////////////////////
347
348
349//////////////////////////////////////////////////////////////////////////////
350// FIN
351//////////////////////////////////////////////////////////////////////////////