001package org.cpsolver.instructor.test;
002
003import java.io.File;
004import java.io.IOException;
005import java.io.PrintWriter;
006import java.text.DecimalFormat;
007import java.util.Iterator;
008
009import org.cpsolver.coursett.model.TimeLocation;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012import org.cpsolver.ifs.util.ToolBox;
013import org.cpsolver.instructor.Test;
014import org.cpsolver.instructor.model.Instructor;
015import org.cpsolver.instructor.model.Preference;
016import org.cpsolver.instructor.model.Section;
017import org.cpsolver.instructor.model.TeachingAssignment;
018import org.cpsolver.instructor.model.TeachingRequest;
019
020/**
021 * General chemistry teaching assistants test. No soft constraints at the moment.
022 * 
023 * @version IFS 1.3 (Instructor Sectioning)<br>
024 *          Copyright (C) 2016 Tomas Muller<br>
025 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
027 * <br>
028 *          This library is free software; you can redistribute it and/or modify
029 *          it under the terms of the GNU Lesser General Public License as
030 *          published by the Free Software Foundation; either version 3 of the
031 *          License, or (at your option) any later version. <br>
032 * <br>
033 *          This library is distributed in the hope that it will be useful, but
034 *          WITHOUT ANY WARRANTY; without even the implied warranty of
035 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036 *          Lesser General Public License for more details. <br>
037 * <br>
038 *          You should have received a copy of the GNU Lesser General Public
039 *          License along with this library; if not see
040 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
041 */
042public class ChmTest extends Test {
043    public ChmTest(DataProperties properties) {
044        super(properties);
045    }
046    
047    @Override
048    protected void generateReports(File dir, Assignment<TeachingRequest, TeachingAssignment> assignment) throws IOException {
049        PrintWriter out = new PrintWriter(new File(dir, "solution-assignments.csv"));
050        out.println("Course,Section,Time,Room,Load,Student,Name,Not Available,Preference (Avoid),Max Load");
051        for (TeachingRequest request : variables()) {
052            out.print(request.getCourse().getCourseName());
053            String sect = "", time = "", room = "";
054            for (Iterator<Section> i = request.getSections().iterator(); i.hasNext(); ) {
055                Section section = i.next();
056                sect += section.getSectionName();
057                time += (section.getTime() == null ? "-" : section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true));
058                room += (section.getRoom() == null ? "-" : section.getRoom());
059                if (i.hasNext()) { sect += ", "; time += ", "; room += ", "; }
060            }
061            out.print(",\"" + sect + "\",\"" + time + "\",\"" + room + "\"");
062            out.print("," + new DecimalFormat("0.0").format(request.getLoad()));
063            TeachingAssignment ta = assignment.getValue(request);
064            if (ta != null) {
065                Instructor instructor = ta.getInstructor();
066                out.print(instructor.getExternalId());
067                out.print(",\"" + instructor.getName() + "\"");
068                out.print(",\"" + instructor.getAvailable() + "\"");
069                String timePref = "";
070                for (Preference<TimeLocation> p: instructor.getTimePreferences()) {
071                    if (!p.isProhibited()) {
072                        if (!timePref.isEmpty()) timePref += ", ";
073                        timePref += p.getTarget().getLongName(true).trim();
074                    }
075                }
076                out.print(",\"" + timePref + "\"");
077                out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
078            }
079            out.println();
080        }
081        out.flush();
082        out.close();
083
084        out = new PrintWriter(new File(dir, "solution-students.csv"));
085        out.println("Student,Name,Not Available,Preference (Avoid),Max Load,Assigned Load,Back-To-Back,Different Lecture,Overlap [h],1st Assignment,2nd Assignment, 3rd Assignment");
086        for (Instructor instructor: getInstructors()) {
087            out.print(instructor.getExternalId());
088            out.print(",\"" + instructor.getName() + "\"");
089            out.print(",\"" + instructor.getAvailable() + "\"");
090            String timePref = "";
091            for (Preference<TimeLocation> p: instructor.getTimePreferences()) {
092                if (!p.isProhibited()) {
093                    if (!timePref.isEmpty()) timePref += ", ";
094                    timePref += p.getTarget().getLongName(true).trim();
095                }
096            }
097            out.print(",\"" + timePref + "\"");
098            out.print("," + new DecimalFormat("0.0").format(instructor.getMaxLoad()));
099            Instructor.Context context = instructor.getContext(assignment);
100            out.print("," + new DecimalFormat("0.0").format(context.getLoad()));
101            out.print("," + (context.countBackToBackPercentage() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countBackToBackPercentage())));
102            out.print("," + (context.countDifferentLectures() == 0.0 ? "" : new DecimalFormat("0.0").format(100.0 * context.countDifferentLectures())));
103            int share = 0;
104            for (TeachingAssignment ta : context.getAssignments()) {
105                for (Preference<TimeLocation> p: instructor.getTimePreferences()) {
106                    if (!p.isProhibited())
107                        share += ta.variable().share(p.getTarget());
108                }
109            }
110            out.print("," + (share == 0 ? "" : new DecimalFormat("0.#").format(share / 12.0)));
111            for (TeachingAssignment ta : context.getAssignments()) {
112                String sect = "";
113                for (Iterator<Section> i = ta.variable().getSections().iterator(); i.hasNext(); ) {
114                    Section section = i.next();
115                    sect += section.getSectionName() + (section.getTime() == null ? "" : " " + section.getTime().getDayHeader() + " " + section.getTime().getStartTimeHeader(true));
116                    if (i.hasNext()) sect += ", ";
117                }
118                out.print(",\"" + ta.variable().getCourse() + " " + sect + "\"");
119            }
120            out.println();
121        }
122        out.flush();
123        out.close();
124    }
125    
126    public static void main(String[] args) throws Exception {
127        DataProperties config = new DataProperties();
128        config.load(ChmTest.class.getClass().getResourceAsStream("/org/cpsolver/instructor/test/chm.properties"));
129        config.putAll(System.getProperties());
130        ToolBox.configureLogging();
131        
132        new ChmTest(config).execute();
133    }
134}