The objectives of this assignment are to formulate a common optimization problem as a non-linear system of equations and to use MATLAB to solve the system.
A one-day career fair is being organized for Wyoming High School. The
objective is to bring speakers in from various organizations to speak
about the nuances of particular careers such as Mechanical
Engineering, Politics, Acting, and so on. A significant portion of
the high school population has indicated interest in attending the
fair. Each interested student has been asked to list
preferred speakers (
is a parameter - the actual number of
preferences may vary from year to year). The goal is to create
schedules for the speakers and students so that the total number of
missed preferences is minimized. However, there are some realistic
restrictions we have to worry about. These are:
Schedule the speakers so as to minimize the maximum number of rooms
used in a period. Assume all data is input via file with the
following format (the names of items follows those given in Section 3):
Speakers will be identified by the order in which their required number of speeches is given on the 2nd line of the file. Comments are shown only to help clarify the meaning of lines. It will not be necessary to worry about file comments - just assume there are none. The following is a sample input file showing 6 speakers for 5 periods with number of speeches set at 2,3,4,3,2,3 for speakers 1-6, respectively.% The number of speakers
% The number of speeches required of each speaker
% The number of speaking periods
6 2 3 4 3 2 3 5Sample output should look like this for 4 rooms:
Printing speaker schedule
3 5 1 0
3 1 4 2
3 4 2 6
2 3 0 6
5 4 0 6
where columns represent rooms and rows represent periods. A 0 entry
at row Complete this part before continuing to part II.
Now, find the minimum room capacity which supports a schedule using
the minimum number of rooms in which all student preferences are
satisfied. Expect input from a file with the following format:
Speakers will be identified as in Part I. Students will be identified by the order in which their preference lines appear after the 4th line of the file. As in Part I, just assume there are no comments in the file.% The number of speakers
% The number of speeches required of each speaker
% The number of speaking periods
% The number of students, the number of preferences
% The remaining lines give student preferences, one
% line per student, where
is the number of the
... % speaker that is thepreference of student
.
% End of file at
student
The following is a sample input file showing 6 speakers for 5 periods with number of speeches set at 2,3,4,3,2,3 for speakers 1-6, respectively, and 15 students with 3 preferences each. For example, student 1 prefers to listen to speakers 1, 2 and 4 and student 15 prefers to listen to speakers 2, 3, and 5.
6 2 3 4 3 2 3 5 15 3 1 2 4 2 3 5 3 5 6 1 3 4 2 5 6 3 5 6 2 4 5 1 4 5 1 2 5 1 3 5 1 4 5 2 5 6 1 3 5 2 5 6 2 3 5Sample output appears below where the user has selected 4 rooms of capacity 7 to hold the conference (a schedule with fewer rooms is not possible). Two schedules are shown: one for speakers, which looks like and has a similar meaning to the schedule of Part I, and one for students. In both schedules columns represent rooms and rows represent periods. In the student schedule each room/period entry contains a list of students. Thus, students 5,9,12,14 are scheduled for the lecture given by speaker 2 in room 1 during period 5. Observe that some speakers have no students during some periods.
Printing speaker schedule
5 2 0 3
5 2 6 3
1 0 4 6
1 0 4 3
2 6 4 3
Printing student schedule
[3,5,6,8,12,14,15] [1,2,7,9,11] [] [4,10,13]
[2,7,8,9,10,11,13] [1,5,12,14,15] [] [3,4,6]
[1,4,8,9,10,11,13] [] [2,7,15] [3,5,6,12,14]
[1,4,8,9,10,11,13] [] [5,7,12,14] [2,3,6,15]
[5,9,12,14] [] [1,4,7,8,11] [2,3,6,10,13,15]
Consider part I first. Let's define some important variables.
Suppose there are
speakers. The identities of the speakers will
simply be given by the set of numbers
. Let
denote the number of periods speaker
is supposed to speak in
during the day. We will use
to represent the number of periods
encompassing the career fair during the day and
to denote the
maximum number of rooms that will be used in any one period. For
convenience the rooms will be numbered
and
periods will be numbered
. This way we can
reference them very easily in any program we write.
Now let's write mathematical expressions representing the restrictions. Consider the restriction (6.) that there must be at most one speaker for each room in a single period. Suppose, each time we schedule a speaker to a room and period, we hand that speaker a red marker stating room and period. Then, to see if the restriction is satisfied, we just need to count the red markers for each room and period. If the number is 2 or greater for one room/period combination, the restriction is not satisfied. Otherwise it is.
How do we hand out red markers in a computer program? By inventing a
variable that takes the value 1 if a particular speaker
is
scheduled for a particular room in a particular period and 0 otherwise
(corresponding to no marker being handed out to speaker
for that
room and period). Here is a description of the set of variables we
create:
So, how do the new variables get used? Well, first we have to
establish mathematical constraints (equations or inequalities) that
force each of these variables to have value 0 or 1. This can be done
in several different ways. For example, for all
,
,
, create
the following equations:
Now we can satisfy restriction 6. with the following set of
inequalities:
Equations (1) and inequalities (2) are called a
quadratic system. This system has quite a number of unknowns, namely
for each speaker, room, period combination. It is easy to
see this system is satisfied (all equations and inequalities are
satisfied) if
for all
which means do not
schedule any speakers! We will add more constraints to the system.
When all the constraints corresponding to all relevant restrictions
are added in, we will get a meaningful solution.
Let's concentrate now on restriction 7. (every speaker
gives at most one speech in a period). Similar to inequalities
(2), we add constraints:
Now consider restriction 8. (a set number of speeches for
each speaker). This restriction can be satisfied by adding the
following constraints to the system:
At this point we have all the constraints needed to solve part I. The quadratic system (1)-(4) can be solved directly in MATLAB using quadprog. The only question is how to represent the system. This is treated in the Coding section below.
To solve part II we need to define more variables and add constraints
involving them. Let the number of students be
. Let
denote
the capacity of each room. Indentify students by number
as we did for the speakers. The following variables
can be created for students, just like they were created for speakers:
Restriction 9. may be represented in a manner similar to
inequality (3). That is,
Restriction 10 requires each student to attend a speech in every
period. This can be represented in a manner similar to inequality
(4) except that
can be used instead of
. That is,
Restriction 11 requires all rooms to have the same capacity
.
Thus,
to insure that room
is not
overbooked in period
(we do not count the speaker).
Finally, consider satisfying all student preferences. Represent
preferences as a set of student/speaker pairs
![]() |
(7) |
This section is divided into the following parts: reading from file,
choosing an optimization tool for solving Part I, dealing with linear
constraints, dealing with non-linear constraints, choosing an
optimization tool for solving Part II.
File reading is briefly mentioned since this has been done in previous
labs. The important points are: use an input line to get a file
name from a user. Get a file identifier using fopen. Read
single numbers using fscanf(fid,'%d',1);. Read a vector of, say,
4 numbers using fscanf(fid,'%d',4);.
Arrays are a convenient tool for constraint representation and that is
what MATLAB expects to see. By convention, array columns represent
variables and array rows represent constants of constraints. A system
of linear constraints can be represented as a simple inequality
involving an array product. For example, the inequality of
Figure 1 represents the set of constraints
![]() |
MATLAB has builtin functions which take as input the constant array data of constraints and return values to variables which satisfy the constraints. For example, the system of inequalities and equations shown in Figures 1-3 can be solved using the MATLAB code shown in Figure 4. The solution returned is the transpose of [0 1 1 0]. Observe, the x values are either 0 or 1 only because the constraints of Figures 1-2 are satisfied: the function quadprog does not actually solve the equation of Figure 2, instead it finds values for x that minimize x'*H*x + f'*x, and the minimum occurs at 0 if the constraints of Figures 1-2 are satisfied. Hence, in finding the minimum, quadprog winds up solving the equation of Figure 3.
We have represented the variables of the problems with three
subscripts:
for speaker, room, and period. But we said MATLAB
likes to see the variables in array columns. So we need a way to
translate
to a column index for both the speakers and the
students. Let's start with an example. Suppose there are 2 speakers,
3 students, 4 periods, and 2 rooms. One natural translation is shown
in the following table (Idx means column index and Var
means variable to associate with that column):
| Idx | Var | Idx | Var | Idx | Var | Idx | Var |
| 1 | 11 | 21 | 31 | ||||
| 2 | 12 | 22 | 32 | ||||
| 3 | 13 | 23 | 33 | ||||
| 4 | 14 | 24 | 34 | ||||
| 5 | 15 | 25 | 35 | ||||
| 6 | 16 | 26 | 36 | ||||
| 7 | 17 | 27 | 37 | ||||
| 8 | 18 | 28 | 38 | ||||
| 9 | 19 | 29 | 39 | ||||
| 10 | 20 | 30 | 40 |
| floor((idx-1)/(nprds*nrms))+1 | ||
| floor(mod((idx-1), nprds*nrms)/nprds)+1 | ||
| mod((idx-1), nprds)+1 | ||
| floor((idx-nspks*nrms*nprds-1)/(nprds*nrms))+1 | ||
| floor(mod((idx-nspks*nrms*nprds-1), nprds*nrms)/nprds)+1 | ||
| mod((idx-nspks*nrms*nprds-1), nprds)+1 |
Life would be made a lot simpler if we could build a function, say
S, to do the translation from
to column index and use
it, for example, like this: A(row,S(i,j,k))=1; to place a 1 in
the correct row and column according to the speaker, room, and period.
But if S is to be a function, then some mechanism must be used
to tell it the values of nspks, nstud, nrms, and
nprds. Normally, these would have to be passed as arguments to
the function. But we do not want to do this because it is ugly. So
we make use of the following lines in the calling function:
setappdata(0,'nprds',nprds); setappdata(0,'nrms',nrms); setappdata(0,'nspks',nspks); setappdata(0,'nstud',nstud);which are invoked after values for the named variables have been set. These lines copy the values to a global workspace that is visible to all functions. To get the values from the global workspace the called function uses the following lines:
nstud = getappdata(0,'nstud'); nprds = getappdata(0,'nprds'); nspks = getappdata(0,'nspks'); nrms = getappdata(0,'nrms');
Consider restriction 6. (at most one speaker can be assigned to a room
in a period) which is implemented as the following set of nrms*nprds inequalities:
con_num = 1;
for j=1:nrms
for k=1:nprds
for i=1:nspks; A(con_num, S(i,j,k)) = 1; end
con_num = con_num+1;
end
end
Code for another set of constraints will follow this but without
restting con_num to 1!
The following shows code that implements S as used in
implementing the above constraints.
function index = S(i,j,k);
nprds = getappdata(0,'nprds');
nrms = getappdata(0,'nrms');
index = k + (j-1)*nprds + (i-1)*nprds*nrms;
end
Code for implementing a corresponding P (as in
options = optimset('MaxIter',100000);
x = zeros(1,n);
x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x,options);
To use the options field, we are forced to provide a starting value
for x. This is the reason for the line x = zeros(1,n);.
Submit m files solving the two problems of Section 2
on or before June 8 using blackboard. See the course webpage at