File: firstlastequal.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 (163 lines) | stat: -rw-r--r-- 4,246 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
/* TABLIX, PGA highschool timetable generator                              */
/* Copyright (C) 2002-2007 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: firstlastequal.c,v 1.1 2007-05-27 17:58:40 avian Exp $ */

/** @module
 *
 * @author Tomaz Solc 
 * @author-email tomaz.solc@tablix.org
 *
 * @brief This module counts the number of occupied first and last time slots in
 * teacher's (or any other constant resource type) timetables. The number of 
 * weights added is equal to the difference between the number of occupied 
 * first time slots and the number of occupied last time slots.
 *
 * For example if a teacher has two events scheduled on the first time slot in 
 * a day then it must also have two events scheduled on the last time slot. 
 * (E.g. A teacher teaches first period for Monday and Tuesday, and the last 
 * period for Wednesday, Tuesday, Friday. So that he/she can go home earlier 
 * on Monday/Tuesday and come in later on Wednesday, Tuesday, Friday.)
 *
 * The following example will make this module check teacher's timetable: 
 *
 * <module name="firstlastequal" weight="60" mandatory="yes">
 * 	<option name="resourcetype">teacher</option>
 * </module>
 *
 * @ingroup School scheduling
 */

/** @option resourcetype
 *
 * Use this option to specify a constant resource type for which this module
 * will be in effect.
 *
 * You can use multiple resourcetype options for one module.
 *
 */

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

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

#include "module.h"

/* Dimensions of the time matrix */
static int periods, days;

int fitness(chromo **c, ext **e, slist **s)
{
	int sum;

	ext *timext;

	int connum, con_resid;
	int day;

	/* number of non-free first and last time slots in a week */
	int first_num, last_num;

	int first_resid, last_resid;

	timext=e[0];

	connum=timext->connum;

	sum=0;

        for(con_resid=0;con_resid<connum;con_resid++) {
		first_num=0;
		last_num=0;
                for(day=0;day<days;day++) {
			/* resid of the first time slot of the day */
			first_resid=day*periods;
			/* resid of the last time slot */
			last_resid=day*periods+periods-1;

			if(timext->tupleid[first_resid][con_resid]!=-1) {
				first_num++;
			}

			if(timext->tupleid[last_resid][con_resid]!=-1) {
				last_num++;
			}
                }

		sum=sum+abs(first_num-last_num);
        }

	return(sum);
}

int module_init(moduleoption *opt)
{
	fitnessfunc *f;
	moduleoption *result;

	char *type;

	char fitnessname[256];
	int n;

	resourcetype *time;

	/* Find dimensions of the time matrix */

	time=restype_find("time");
	if(time==NULL) {
		error(_("Resource type '%s' not found"), "time");
		return -1;
	}

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

	/* Register fitness functions */

	result=option_find(opt, "resourcetype");
	if(result==NULL) {
		error(_("module '%s' has been loaded, but not used"), 
							"firstlastequal.so");
	}

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

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

		f=fitness_new(fitnessname,
			option_int(opt, "weight"),
			option_int(opt, "mandatory"),
			fitness);
		
		if(f==NULL) return -1;
		
		n=fitness_request_ext(f, type, "time");
		if(n) return -1;

		result=option_find(result->next, "resourcetype");
	}

	return(0);
}