001package org.cpsolver.studentsct;
002
003import java.io.File;
004
005import org.cpsolver.ifs.assignment.Assignment;
006import org.cpsolver.ifs.assignment.DefaultSingleAssignment;
007import org.cpsolver.ifs.solution.Solution;
008import org.cpsolver.ifs.solver.Solver;
009import org.cpsolver.ifs.util.DataProperties;
010import org.cpsolver.ifs.util.ToolBox;
011import org.cpsolver.studentsct.model.Enrollment;
012import org.cpsolver.studentsct.model.Request;
013import org.cpsolver.studentsct.model.Student;
014
015
016/**
017 * A simple class that converts a solution (with real students) into an empty solution with
018 * expectations computed based on the assignments. It has two parameters, the input solution
019 * (XML file) and the output solution (also an XML file).
020 * <br>
021 * <br>
022 * 
023 * @version StudentSct 1.3 (Student Sectioning)<br>
024 *          Copyright (C) 2013 - 2014 Tomáš Müller<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 */
042
043public class Solution2Expectations {
044    private static org.apache.log4j.Logger sLog = org.apache.log4j.Logger.getLogger(Solution2Expectations.class);
045    
046    public static void main(String[] args) {
047        try {
048            ToolBox.configureLogging();
049            
050            DataProperties config = new DataProperties();
051            
052            String command = args[0];
053            if ("real2exp".equals(command)) {
054                StudentSectioningModel model = new StudentSectioningModel(config);
055                Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
056                StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
057                loader.setInputFile(new File(args[1]));
058                loader.load();
059                
060                sLog.info("Loaded: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
061                
062                for (Student s: model.getStudents()) s.setDummy(true);
063                model.computeOnlineSectioningInfos(assignment);
064                for (Student s: model.getStudents()) s.setDummy(false);
065                for (Request request: model.variables())
066                    assignment.unassign(0, request);
067                
068                Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
069                Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
070                solver.setInitalSolution(solution);
071                new StudentSectioningXMLSaver(solver).save(new File(args[2]));
072                
073                sLog.info("Saved: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));                
074            } else if ("ll2exp".equals(command)) {
075                StudentSectioningModel model = new StudentSectioningModel(config);
076                Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
077                StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
078                loader.setInputFile(new File(args[1]));
079                loader.load();
080                
081                sLog.info("Loaded: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
082                
083                model.computeOnlineSectioningInfos(assignment);
084                for (Request request: model.variables())
085                    assignment.unassign(0, request);
086                
087                Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
088                Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
089                solver.setInitalSolution(solution);
090                new StudentSectioningXMLSaver(solver).save(new File(args[2]));
091                
092                sLog.info("Saved: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));    
093            } else if ("students".equals(command)) {
094                StudentSectioningModel model = new StudentSectioningModel(config);
095                Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
096                StudentSectioningXMLLoader loader = new StudentSectioningXMLLoader(model, assignment);
097                loader.setInputFile(new File(args[1]));
098                loader.setLoadStudents(false);
099                loader.load();
100                
101                sLog.info("Loaded [1]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
102
103                StudentSectioningXMLLoader loder2 = new StudentSectioningXMLLoader(model, assignment);
104                loder2.setInputFile(new File(args[2]));
105                loder2.setLoadOfferings(false);
106                loder2.setLoadStudents(true);
107                loder2.load();
108                
109                sLog.info("Loaded [2]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
110
111                Solution<Request, Enrollment> solution = new Solution<Request, Enrollment>(model, assignment, 0, 0);
112                Solver<Request, Enrollment> solver = new Solver<Request, Enrollment>(config);
113                solver.setInitalSolution(solution);
114                new StudentSectioningXMLSaver(solver).save(new File(args[3]));
115                
116                sLog.info("Saved [3]: " + ToolBox.dict2string(model.getExtendedInfo(assignment), 2));
117            } else {
118                sLog.error("Unknown command: " + command);
119            }
120        } catch (Exception e) {
121            sLog.error(e.getMessage(), e);
122        }
123    }
124
125}