Search in sources :

Example 1 with Model

use of org.cpsolver.ifs.model.Model in project cpsolver by UniTime.

the class Test method execute.

/**
     * Run the problem
     */
public void execute() {
    int nrSolvers = getProperties().getPropertyInt("Parallel.NrSolvers", 1);
    Solver<TeachingRequest.Variable, TeachingAssignment> solver = (nrSolvers == 1 ? new Solver<TeachingRequest.Variable, TeachingAssignment>(getProperties()) : new ParallelSolver<TeachingRequest.Variable, TeachingAssignment>(getProperties()));
    Assignment<TeachingRequest.Variable, TeachingAssignment> assignment = (nrSolvers <= 1 ? new DefaultSingleAssignment<TeachingRequest.Variable, TeachingAssignment>() : new DefaultParallelAssignment<TeachingRequest.Variable, TeachingAssignment>());
    if (!load(new File(getProperties().getProperty("input", "input/solution.xml")), assignment))
        return;
    solver.setInitalSolution(new Solution<TeachingRequest.Variable, TeachingAssignment>(this, assignment));
    solver.currentSolution().addSolutionListener(new SolutionListener<TeachingRequest.Variable, TeachingAssignment>() {

        @Override
        public void solutionUpdated(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
        }

        @Override
        public void getInfo(Solution<TeachingRequest.Variable, TeachingAssignment> solution, Map<String, String> info) {
        }

        @Override
        public void getInfo(Solution<TeachingRequest.Variable, TeachingAssignment> solution, Map<String, String> info, Collection<TeachingRequest.Variable> variables) {
        }

        @Override
        public void bestCleared(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
        }

        @Override
        public void bestSaved(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
            Model<TeachingRequest.Variable, TeachingAssignment> m = solution.getModel();
            Assignment<TeachingRequest.Variable, TeachingAssignment> a = solution.getAssignment();
            System.out.println("**BEST[" + solution.getIteration() + "]** " + m.toString(a));
        }

        @Override
        public void bestRestored(Solution<TeachingRequest.Variable, TeachingAssignment> solution) {
        }
    });
    solver.start();
    try {
        solver.getSolverThread().join();
    } catch (InterruptedException e) {
    }
    Solution<TeachingRequest.Variable, TeachingAssignment> solution = solver.lastSolution();
    solution.restoreBest();
    sLog.info("Best solution found after " + solution.getBestTime() + " seconds (" + solution.getBestIteration() + " iterations).");
    sLog.info("Number of assigned variables is " + solution.getModel().assignedVariables(solution.getAssignment()).size());
    sLog.info("Total value of the solution is " + solution.getModel().getTotalValue(solution.getAssignment()));
    sLog.info("Info: " + ToolBox.dict2string(solution.getExtendedInfo(), 2));
    File outDir = new File(getProperties().getProperty("output", "output"));
    outDir.mkdirs();
    save(outDir, solution.getAssignment());
    try {
        generateReports(outDir, assignment);
    } catch (IOException e) {
        sLog.error("Failed to write reports: " + e.getMessage(), e);
    }
    ConflictStatistics<TeachingRequest.Variable, TeachingAssignment> cbs = null;
    for (Extension<TeachingRequest.Variable, TeachingAssignment> extension : solver.getExtensions()) {
        if (ConflictStatistics.class.isInstance(extension)) {
            cbs = (ConflictStatistics<TeachingRequest.Variable, TeachingAssignment>) extension;
        }
    }
    if (cbs != null) {
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileWriter(new File(outDir, "cbs.txt")));
            out.println(cbs.toString());
            out.flush();
            out.close();
        } catch (IOException e) {
            sLog.error("Failed to write CBS: " + e.getMessage(), e);
        } finally {
            if (out != null)
                out.close();
        }
    }
}
Also used : Solver(org.cpsolver.ifs.solver.Solver) ParallelSolver(org.cpsolver.ifs.solver.ParallelSolver) DefaultParallelAssignment(org.cpsolver.ifs.assignment.DefaultParallelAssignment) FileWriter(java.io.FileWriter) TeachingRequest(org.cpsolver.instructor.model.TeachingRequest) DefaultParallelAssignment(org.cpsolver.ifs.assignment.DefaultParallelAssignment) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) Assignment(org.cpsolver.ifs.assignment.Assignment) TeachingAssignment(org.cpsolver.instructor.model.TeachingAssignment) PrintWriter(java.io.PrintWriter) ParallelSolver(org.cpsolver.ifs.solver.ParallelSolver) IOException(java.io.IOException) TeachingAssignment(org.cpsolver.instructor.model.TeachingAssignment) Model(org.cpsolver.ifs.model.Model) InstructorSchedulingModel(org.cpsolver.instructor.model.InstructorSchedulingModel) DefaultSingleAssignment(org.cpsolver.ifs.assignment.DefaultSingleAssignment) File(java.io.File)

Aggregations

File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Assignment (org.cpsolver.ifs.assignment.Assignment)1 DefaultParallelAssignment (org.cpsolver.ifs.assignment.DefaultParallelAssignment)1 DefaultSingleAssignment (org.cpsolver.ifs.assignment.DefaultSingleAssignment)1 Model (org.cpsolver.ifs.model.Model)1 ParallelSolver (org.cpsolver.ifs.solver.ParallelSolver)1 Solver (org.cpsolver.ifs.solver.Solver)1 InstructorSchedulingModel (org.cpsolver.instructor.model.InstructorSchedulingModel)1 TeachingAssignment (org.cpsolver.instructor.model.TeachingAssignment)1 TeachingRequest (org.cpsolver.instructor.model.TeachingRequest)1