File: freeperiod.c

package info (click to toggle)
tablix2 0.3.5-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,024 kB
  • sloc: ansic: 24,593; xml: 13,161; sh: 10,409; makefile: 800; perl: 564; yacc: 289; sed: 16
file content (267 lines) | stat: -rw-r--r-- 6,261 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
/* TABLIX, PGA general timetable solver                              */
/* Copyright (C) 2002-2005 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: freeperiod.c,v 1.4 2006-06-15 17:47:28 avian Exp $ */

/** @module
 *
 * @author Nick Robinson 
 * @author-email npr@bottlehall.co.uk
 *
 * @brief Specifies that some time slots should never be used by any lessons.
 * 
 * There is no fitness function, so the "weight" and "mandatory" module 
 * options are ignored (restrictions set by this module are always mandatory).
 *
 * @ingroup School scheduling
 */

/** @option free-period
 *
 * <option name="free-period">day period</option>
 *
 * This option specifies that the time slot at "day period" should never 
 * be used to schedule lessons.
 */

/** @resource-restriction free-period
 *
 * <restriction type="free-period">day period</restriction>
 *
 * This option specifies that the time slot at "day period" should never 
 * be used to schedule lessons for a teacher.
 */

/** @resource-restriction free-day
 *
 * <restriction type="free-day">day</restriction>
 *
 * This option specifies that no slots on "day" should ever 
 * be used to schedule lessons for a teacher.
 */

/** @resource-restriction day-off
 *
 * <restriction type="day-off">day</restriction>
 *
 * Synonym for 'free-day'
 *
 */

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

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

#include "module.h"

static int exclnum, *excl;

struct tlist
{
	int *frees;
	int freenum;
	int tid;
	struct tlist *next; 
} *texcl;

static int days;
static int periods;

/** @brief Removes some values from a domain.
 *
 * This function removes all values from @a dom domain that are in the
 * @a val list.
 *
 * @param dom Pointer to the domain struct
 * @param val Array of values
 * @param valnum Number of values in the array */
void domain_del(domain *dom, int *val, int valnum)
{
	int n,m;
	assert(dom!=NULL);
	assert(val!=NULL);
	assert(valnum>=0);

	for(n=0;n<dom->valnum;n++)
		for(m=0;m<valnum;m++)
			if(dom->val[n]==val[m])
			{
				dom->val[n]=-1;
				break;
			}

	for(n=0;n<dom->valnum;n++)
		while(n<dom->valnum&&dom->val[n]==-1)
		{
			for(m=n+1;m<dom->valnum;m++)
				dom->val[m-1]=dom->val[m];
			dom->valnum--;
		}
}

int find_excl( int slot )
{
	int i;
	for ( i = 0; i<exclnum && excl[i] != slot; i++ );

	return ( i != exclnum );
}

struct tlist *find_texcl( int tid )
{
	struct tlist *tmp = texcl;
	while ( tmp && ( tmp->tid != tid ) ) tmp = tmp->next;

	return tmp;
}

void addfreeperiod( resource *res1, int d, int p )
{
	struct tlist *this_teacher;
	
	if ( ( this_teacher = find_texcl( res1->resid ) ) )
	{
		//found the teacher so add the free slot to the list
		this_teacher->frees = realloc( this_teacher->frees, ++this_teacher->freenum*sizeof(int) );
		this_teacher->frees[this_teacher->freenum-1] = d*periods+p;
	}
	else
	{
		//not found the teacher so add to the list and create first free slot
		struct tlist *nxt = texcl;
		texcl = (struct tlist*)malloc( sizeof( struct tlist ) );
		texcl->frees = (int *)malloc( sizeof(int) );
		texcl->freenum = 1;
		texcl->frees[texcl->freenum-1] = d*periods+p;
		texcl->tid = res1->resid;
		texcl->next = nxt;
	}
}

int getfreeperiod(char *restriction, char *cont, resource *res1)
{
	int cd, d, p;
	cd=sscanf(cont, "%d %d", &d, &p);
	if(cd!=2||d<0||p<0||d>=days||p>=periods)
	{
		error(_("invalid day or period in 'free-period' restriction"));
		return 1;
	}

	addfreeperiod( res1, d, p );
	
	return 0;
}

int getfreeday(char *restriction, char *cont, resource *res1)
{
	int cd, d,p;
	cd=sscanf(cont, "%d", &d);
	if(cd!=1||d<0||d>=days)
	{
		error(_("invalid day in 'free-day' restriction"));
		return 1;
	}

	for ( p = 0; p < periods; p++ )
		addfreeperiod( res1, d, p );
	
	return 0;
}

int module_precalc(moduleoption *opt) 
{
	int i;
	int timeid, teachid;
	tupleinfo *tup;
	domain *dom;
	struct tlist *tnext;

	if ( exclnum == 0 && texcl == NULL )
	{
		info(_("module '%s' has been loaded, but not used"), "freeperiod.so");
		return 0;
	}
	
	timeid = restype_find( "time" )->typeid; 
	teachid = restype_find( "teacher" )->typeid;
	tup = dat_tuplemap;
	for ( i = 0; i < dat_tuplenum; i++ )
	{
		struct tlist *tt;
		dom = tup[i].dom[timeid];
		if(excl!=NULL) {
			domain_del( dom, excl, exclnum );
		}
		if ( ( tt = find_texcl( dat_tuplemap[i].resid[teachid] ) ) )
			domain_del( dom, tt->frees, tt->freenum );
	}

	if(excl!=NULL) {
		free(excl);
	}
	
	while(texcl!=NULL) {
		tnext=texcl->next;
		free( texcl->frees );
		free(texcl);
		texcl=tnext;
	}
	
	return(0);
}

int module_init(moduleoption *opt) 
{
	int d,p;
	moduleoption *result;

	precalc_new(module_precalc);

	texcl = NULL;
	excl = NULL;
	exclnum = 0;
	if(res_get_matrix(restype_find("time"), &days, &periods)) {
		error(_("Resource type 'time' is not a matrix"));
		return -1;
	}
	
	result=option_find(opt, "free-period");
	while (result!=NULL)
	{
		int cd = 0;
		cd=sscanf(result->content_s, "%d %d", &d, &p);
		if(cd!=2||d<0||p<0||d>=days||p>=periods)
		{
			error(_("invalid day or period in 'free-period' option"));
			return 1;
		}
		excl = realloc( excl, ++exclnum*sizeof(int) );
		excl[exclnum-1] = d*periods+p;
		
		result=option_find(result->next, "free-period");
	}

	handler_res_new("teacher", "free-day", getfreeday);
	handler_res_new("teacher", "day-off", getfreeday);
	handler_res_new("teacher", "free-period", getfreeperiod);
	
	return( 0 );
}