File: sameday.c

package info (click to toggle)
tablix2 0.3.5-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 9,920 kB
  • ctags: 3,351
  • sloc: ansic: 24,593; xml: 13,161; sh: 10,409; makefile: 852; perl: 564; yacc: 289; sed: 16
file content (589 lines) | stat: -rw-r--r-- 14,408 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* TABLIX, PGA general timetable solver                              */
/* Copyright (C) 2002-2004 Tomaz Solc                                      */

/* This program is free software; you can redistribute it and/or modify    */
/* it under the terms of the GNU General Public License as published by    */
/* the Free Software Foundation; either version 2 of the License, or       */
/* (at your option) any later version.                                     */

/* This program is distributed in the hope that it will be useful,         */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of          */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
/* GNU General Public License for more details.                            */

/* You should have received a copy of the GNU General Public License       */
/* along with this program; if not, write to the Free Software             */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

/* $Id: sameday.c,v 1.8 2007-06-06 18:16:14 avian Exp $ */

/** @module
 *
 * @author Tomaz Solc 
 * @author-email tomaz.solc@tablix.org
 *
 * @credits
 * Ideas taken from a patch for Tablix 0.0.3 by 
 * Jaume Obrador <obrador@espaiweb.net>
 *
 * @brief 
 * Adds a weight if a class or a teacher (as specified by the "resourcetype"
 * module option) has the same subject more than N 
 * times in a day. 
 *
 * Default for N is 1. Default value can be changed with the module option 
 * "default". Default value for N can also be overridden for individual 
 * teachers, classes and events with various restrictions.
 *
 * @ingroup School scheduling
 */

/** @option resourcetype
 *
 * Use this option to specify one or more constant resource types. Specified
 * resource types will have their timetables checked by this module.
 *
 * Use option
 * 
 * <option name="resourcetype">class</option>
 *
 * to get the same behaviour as this module had before Tablix version 0.3.1.
 *
 */

/** @option default
 *
 * Use this option to set the default number of equal events classes or 
 * teachers can have in day.
 *
 */

/** @resource-restriction ignore-sameday
 *
 * <resourcetype type="class">
 * 	<resource name="example-class">
 * 		<restriction type="ignore-sameday"/>
 * 	</resource>
 * </resourcetype>
 *
 * Set this restriction to a class or a teacher that you don't want to be 
 * checked for multiple occurrences of the same subject in a day. 
 *
 * This module then ignores classes that have this restriction, however this
 * setting can be overridden for individual events if "set-sameday" restriction 
 * is used on an event for this class or teacher.
 */

/** @resource-restriction set-sameday
 *
 * <resourcetype type="class">
 * 	<resource name="example-class">
 * 		<restriction type="set-sameday">2</restriction>
 * 	</resource>
 * </resourcetype>
 *
 * With this restriction you can set a maximum number of equal events a class
 * or a teacher can have in a day. This setting can be overridden for 
 * individual events if "set-sameday" event restriction is used on an event 
 * for this class or teacher.
 *
 */

/** @tuple-restriction set-sameday
 *
 * <event name="example" repeats="5">
 * 		...
 * 		<restriction type="set-sameday">2</restriction>
 * </resourcetype>
 *
 * With this restriction you can set a maximum number of events of type type
 * that a class or a teacher can have in a day. 
 *
 * This retriction overrides the default setting and settings made by 
 * any resource restrictions.
 */

/** @tuple-restriction ignore-sameday
 *
 * Set this restriction to all events that you do not want to be checked for
 * multiple occurences per day.
 *
 * This retriction overrides the default setting and settings made by 
 * any resource restrictions.
 */

/** @tuple-restriction set-sameday-blocksize
 *
 * With this restriction you can tell this module that the current event 
 * will be scheduled in blocks of this number of equal consecutive events. 
 * 
 * Module will then consider a block of events as a single event when checking 
 * for multiple occurences per day.
 *
 * Default block size is 1.
 */

/** @tuple-restriction consecutive 
 *
 * This is an alias for "ignore-sameday" restriction. It is defined for 
 * convenience when this module is used together with consecutive.so module.
 */

/** @tuple-restriction periods-per-block
 *
 * This is an alias for "set-sameday-blocksize" restriction. It is defined for 
 * convenience when this module is used together with consecutive.so module.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "module.h"

struct eventgroup_t {
	/* Tuple ID of the first event in this group. */
	int tupleid;

	/* Number of defined events in this group. (used only in precalc() for
	 * a sanity check). */
	double num;

	/* Maximum number of events in this group that can be 
	 * scheduled per day. */
	int max_perday;
};

/* Array of all event groups. */
static int groupnum;
static struct eventgroup_t *group;

/* Additional data for each event. */
struct eventdata_t {
	/* Pointer to the group this event belongs to. */
	struct eventgroup_t *group;

	/* Size of the block this event is part of. */
	int blocksize;
};

static struct eventdata_t *events;

struct eventlist_t {
	/* Pointer to the event group structure for this event group. */
	struct eventgroup_t *group;

	/* Number of events in this group scheduled in the current day for the
	 * current class or teacher. */
	int num;
};

/* Used by the fitness function. Lists all events scheduled for a class or 
 * teacher in a day. */
static struct eventlist_t *eventlist;
static int eventlistnum;

/* Used by the precalc function. Lists all constant resource types we are
 * checking against. */
static int *restype_check;

/* Constants */
static int periods, days;

int resource_ignore_sameday(char *restriction, char *content, resource *res)
{
	int tupleid;
	int resid, typeid;

	resid=res->resid;
	typeid=res->restype->typeid;

	if(res->restype->var) {
		error(_("'%s' restriction valid only for constant "
			 		"resource types"), restriction);
		return(-1);
	}

	/* Go through all events and override the default for all events
	 * that use this resource. */
	for(tupleid=0;tupleid<dat_tuplenum;tupleid++) {
		if(dat_tuplemap[tupleid].resid[typeid]==resid) {
			events[tupleid].group->max_perday=periods;
		}
	}

	return 0;
}

int resource_set_sameday(char *restriction, char *content, resource *res)
{
	int tupleid;
	int resid, typeid;
	int max_perday,c;

	resid=res->resid;
	typeid=res->restype->typeid;

	if(res->restype->var) {
		error(_("'%s' restriction valid only for constant "
			 		"resource types"), restriction);
		return(-1);
	}

        c=sscanf(content, "%d", &max_perday);
        if(c!=1||max_perday<=0||max_perday>periods) {
                error(_("Invalid number of periods"));
                return(-1);
        }

	/* Go through all events and override the default for all events
	 * that use this resource. */
	for(tupleid=0;tupleid<dat_tuplenum;tupleid++) {
		if(dat_tuplemap[tupleid].resid[typeid]==resid) {
			events[tupleid].group->max_perday=max_perday;
		}
	}

	return 0;
}

int event_ignore_sameday(char *restriction, char *content, tupleinfo *tuple)
{
	int tupleid;

	tupleid=tuple->tupleid;

	events[tupleid].group->max_perday=INT_MAX;

	return 0;
}

int event_set_sameday(char *restriction, char *content, tupleinfo *tuple)
{
	int tupleid;
	int max_perday,c;

	tupleid=tuple->tupleid;

        c=sscanf(content, "%d", &max_perday);
        if(c!=1||max_perday<=0||max_perday>periods) {
                error(_("Invalid number of periods"));
                return(-1);
        }

	events[tupleid].group->max_perday=max_perday;

	return 0;
}

int event_set_blocksize(char *restriction, char *content, tupleinfo *tuple)
{
	int tupleid;
	int blocksize,c;

	tupleid=tuple->tupleid;

        c=sscanf(content, "%d", &blocksize);
        if(c!=1||blocksize<=0||blocksize>periods) {
                error(_("Invalid number of periods"));
                return(-1);
        }

	events[tupleid].blocksize=blocksize;

	return 0;
}

/* This fitness function expects one timetable extension. It will check if any 
 * event group has more than the specified number of events per day for the
 * constant resource for which the extension was made. */
int module_fitness(chromo **c, ext **e, slist **s)
{
	int tupleid, time;

	int resid, day, period, n;

	int block;

	int max_perday;

	int connum;
	int sum;

	struct eventgroup_t *curgroup;

	ext *cext=e[0];

	/* Number of defined constant resources (classes, teachers, ...) */
	connum=cext->connum;

	/* Total number of weights added */
	sum=0;

	for(resid=0;resid<connum;resid++) {

		time=0;
		for(day=0;day<days;day++) {
			curgroup=NULL;
			block=0;

			/* Construct the list of events for this day */
			eventlistnum=0;
			for(period=0;period<periods;period++) {

				tupleid=cext->tupleid[time][resid];

				if(tupleid==-1) {
					/* This time slot is empty, continue
					 * with the next. */
					curgroup=NULL;
					time++;
					continue;
				}

				if(events[tupleid].group==curgroup) {
					block++;
					if(block<=events[tupleid].blocksize) {
						time++;
						continue;
					}
				} 
				block=1;

				/* Event group of event in the current 
				 * timeslot. */
				curgroup=events[tupleid].group;

				for(n=0;n<eventlistnum;n++) {
					if(eventlist[n].group==curgroup) {
						/* We have already seen an
						 * event in this group on 
						 * this day */

						eventlist[n].num++;
						break;
					}
				}
				
				if(n==eventlistnum) {
					/* This is the first time we see
					 * an event in this group on this
					 * day. */
					eventlist[n].group=curgroup;
					eventlist[n].num=1;

					eventlistnum++;
				}

				time++;
			}

			/* Now check the list if any group has more events
			 * in this day than it should. */
			for(n=0;n<eventlistnum;n++) {

				max_perday=eventlist[n].group->max_perday;
				if(eventlist[n].num>max_perday) {
					sum=sum+eventlist[n].num-max_perday;
				}
			}
		}
	}
	return(sum);
}

/* Initialize event groups. Returns -1 on error and 0 on success. 
 * max_perday is the default setting for the maximum number of events
 * in a group per day. */
static int init_group(int max_perday)
{
	int n,tupleid;

	/* We can't have more event groups than we have defined events */
	group=malloc(sizeof(*group)*dat_tuplenum);
	groupnum=0;

	events=malloc(sizeof(*events)*dat_tuplenum);

	if(group==NULL||events==NULL) return -1;
	
	for(tupleid=0;tupleid<dat_tuplenum;tupleid++) {

		for(n=0;n<groupnum;n++) {
			if(tuple_compare(tupleid, group[n].tupleid)) {
				/* This tuple is part of a group we already 
				 * know. */

				events[tupleid].group=&group[n];

				break;
			}
		}

		if(n==groupnum) {
			/* This tuple is part of a new group. */

			group[n].tupleid=tupleid;
			group[n].num=0;
			group[n].max_perday=max_perday;

			events[tupleid].group=&group[n];

			groupnum++;
		}

		events[tupleid].blocksize=1;
	}

	return 0;
}

int module_precalc(moduleoption *opt)
{
	int typeid,resid,c,n,tupleid;
	int result;

	int resnum;
	int limit;

	result=0;

	/* Go trough all constant resource types we are checking */
	for(typeid=0;typeid<dat_typenum;typeid++) if(restype_check[typeid]) {
		resnum=dat_restype[typeid].resnum;

		for(resid=0;resid<resnum;resid++) {

			/* Reset the number of defined events */
			for(n=0;n<groupnum;n++) group[n].num=0;

			for(tupleid=0;tupleid<dat_tuplenum;tupleid++) {

				c=res_get_conflict(&dat_restype[typeid],
					resid, 
					dat_tuplemap[tupleid].resid[typeid]);

				if(c) {
					events[tupleid].group->num+=1.0/events[tupleid].blocksize;
				}
			}

			for(n=0;n<groupnum;n++) {
				debug("sameday group %d (%s): %f blocks", 
					n, dat_tuplemap[group[n].tupleid].name,
					group[n].num);

				/* Warning: Can overflow here if 
				 * group[n].max_perday is large */
				limit=days*group[n].max_perday;

				if(group[n].num>limit) {
					error(_("Constant resource '%s' "
						"(type '%s') has %.1f blocks of"
						" '%s' events defined "
						"and maximum %d blocks per "
						"day, however only %d days "
						"are defined"), 
					dat_restype[typeid].res[resid].name,
					dat_restype[typeid].type,
					group[n].num,
					dat_tuplemap[group[n].tupleid].name,
					group[n].max_perday,
					days);

					result=-1;
				}
			}
		}
	}

	return(result);
}

int module_init(moduleoption *opt)
{
	int c;
	int max_perday;

	fitnessfunc *fitness;
	moduleoption *result;

	char *type;
	char fitnessname[256];

	c=res_get_matrix(restype_find("time"), &days, &periods);
	if(c) {
		error(_("Resource type 'time' is not a matrix"));
		return -1;
	}

	eventlist=malloc(sizeof(*eventlist)*periods);
	restype_check=malloc(sizeof(*restype_check)*dat_typenum);
	if(eventlist==NULL||restype_check==NULL) {
		error(_("Can't allocate memory"));
		return -1;
	}

	for(c=0;c<dat_typenum;c++) {
		restype_check[c]=0;
	}

	max_perday=option_int(opt, "default");

	/* Default for backward compatibility */
	if(max_perday==INT_MIN) max_perday=1;

	if(init_group(max_perday)) {
		error(_("Can't allocate memory"));
		return -1;
	}

	precalc_new(module_precalc);

	handler_res_new(NULL, "ignore-sameday", resource_ignore_sameday);
	handler_tup_new("ignore-sameday", event_ignore_sameday);

	handler_res_new(NULL, "set-sameday", resource_set_sameday);
	handler_tup_new("set-sameday", event_set_sameday);

	handler_tup_new("consecutive", event_ignore_sameday);
	handler_tup_new("periods-per-block", event_set_blocksize);

	handler_tup_new("set-sameday-blocksize", event_set_blocksize);

	result=option_find(opt, "resourcetype");
	if(result==NULL) {
		error(_("Module '%s' has been loaded, but not used"), 
								"sameday.so");
		error(_("To obtain the same functionality as in "
			"version 0.3.0, add the following module options"));

		error("<option name=\"resourcetype\">class</option>");
	}

	while(result!=NULL) {
		type=result->content_s;

		snprintf(fitnessname, 256, "sameday-%s", type);

		fitness=fitness_new(fitnessname,
				option_int(opt, "weight"),
				option_int(opt, "mandatory"),
				module_fitness);
		
		if(fitness==NULL) return -1;

		c=fitness_request_ext(fitness, type, "time");
		if(c) return -1;

		restype_check[restype_findid(type)]=1;
		
		result=option_find(result->next, "resourcetype");
	}

	return 0;
}