use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.
the class Test method test3.
public static void test3(DataProperties properties, File xmlFile) throws Exception {
int nrTests = properties.getPropertyInt("Test.NrTests", 1);
PrintWriter logStat = new PrintWriter(new FileWriter(properties.getProperty("General.Output") + File.separator + "output.csv"));
logStat.println("fillFact;nrResources;testNr;time[s];iters;speed[it/s];assigned;assigned[%];value;totalValue");
boolean saveSol = properties.getPropertyBoolean("General.SaveSolutionXML", true);
boolean assign = properties.getPropertyBoolean("General.InitialAssignment", true);
int forcedPerturbances = properties.getPropertyInt("General.ForcedPerturbances", 0);
for (int test = 1; test <= nrTests; test++) {
Assignment<Activity, Location> assignment = new DefaultSingleAssignment<Activity, Location>();
TimetableModel m = TimetableModel.loadFromXML(xmlFile, assign ? assignment : null);
if (forcedPerturbances > 0) {
List<Activity> initialVariables = new ArrayList<Activity>();
for (Activity v : m.variables()) {
if (v.getInitialAssignment() != null)
initialVariables.add(v);
}
for (int i = 0; i < forcedPerturbances; i++) {
if (initialVariables.isEmpty())
break;
Activity var = ToolBox.random(initialVariables);
initialVariables.remove(var);
var.removeInitialValue();
}
}
Solver<Activity, Location> s = new Solver<Activity, Location>(properties);
s.setInitalSolution(new Solution<Activity, Location>(m, assignment));
s.currentSolution().clearBest();
s.start();
try {
s.getSolverThread().join();
} catch (NullPointerException npe) {
}
if (s.lastSolution().getBestInfo() == null)
sLogger.error("No solution found :-(");
sLogger.debug("Last solution:" + s.lastSolution().getInfo());
Solution<Activity, Location> best = s.lastSolution();
sLogger.debug("Best solution:" + s.lastSolution().getBestInfo());
best.restoreBest();
int val = 0;
for (Location loc : best.getAssignment().assignedValues()) val += (int) loc.toDouble();
if (saveSol)
m.saveAsXML(properties, false, best, best.getAssignment(), new File(properties.getProperty("General.Output") + File.separator + "solution_" + test + ".xml"));
sLogger.debug("Last solution:" + best.getInfo());
logStat.println(sDoubleFormat.format(properties.getPropertyDouble("Generator.FillFactor", 0.0)) + ";" + sDoubleFormat.format(properties.getPropertyInt("Generator.NrRooms", 0)) + ";" + test + ";" + sDoubleFormat.format(best.getTime()) + ";" + best.getIteration() + ";" + sDoubleFormat.format((best.getIteration()) / best.getTime()) + ";" + best.getAssignment().nrAssignedVariables() + ";" + sDoubleFormat.format(100.0 * best.getAssignment().nrAssignedVariables() / best.getModel().variables().size()) + ";" + val);
sLogger.debug(" time: " + sDoubleFormat.format(best.getTime()) + " s");
sLogger.debug(" iteration: " + best.getIteration());
sLogger.debug(" speed: " + sDoubleFormat.format((best.getIteration()) / best.getTime()) + " it/s");
sLogger.debug(" assigned: " + best.getAssignment().nrAssignedVariables() + " (" + sDoubleFormat.format(100.0 * best.getAssignment().nrAssignedVariables() / best.getModel().variables().size()) + "%)");
sLogger.debug(" value: " + val);
logStat.flush();
}
logStat.close();
}
use of org.cpsolver.ifs.assignment.DefaultSingleAssignment 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();
}
}
}
use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.
the class SolutionEvaluator method main.
public static void main(String[] args) throws Exception {
ToolBox.configureLogging();
DataProperties properties = ToolBox.loadProperties(new java.io.File(args[0]));
properties.putAll(System.getProperties());
TimetableModel model = new TimetableModel(properties);
Assignment<Lecture, Placement> assignment = new DefaultSingleAssignment<Lecture, Placement>();
TimetableXMLLoader loader = new TimetableXMLLoader(model, assignment);
loader.setInputFile(new File(args[1]));
loader.load();
Solution<Lecture, Placement> solution = new Solution<Lecture, Placement>(model, assignment);
Test.saveOutputCSV(solution, new File(args[2]));
}
use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.
the class MistaTables method main.
public static void main(String[] args) {
try {
ToolBox.configureLogging();
DataProperties config = new DataProperties();
Table[] tables = new Table[] { new Problems(), new Rooms() };
for (int i = 0; i < args.length; i++) {
File file = new File(args[i]);
sLog.info("Loading " + file);
ExamModel model = new ExamModel(config);
Assignment<Exam, ExamPlacement> assignment = new DefaultSingleAssignment<Exam, ExamPlacement>();
model.load(new SAXReader().read(file), assignment);
String name = file.getName();
if (name.contains("."))
name = name.substring(0, name.indexOf('.'));
for (Table table : tables) table.add(name, model);
Progress.removeInstance(model);
}
sLog.info("Saving tables...");
File output = new File("tables");
output.mkdir();
for (Table table : tables) table.save(output);
sLog.info("All done.");
} catch (Exception e) {
sLog.error(e.getMessage(), e);
}
}
use of org.cpsolver.ifs.assignment.DefaultSingleAssignment in project cpsolver by UniTime.
the class PriorityStudentWeights method main.
/**
* Test case -- run to see the weights for a few courses
* @param args program arguments
*/
public static void main(String[] args) {
PriorityStudentWeights pw = new PriorityStudentWeights(new DataProperties());
DecimalFormat df = new DecimalFormat("0.000000");
Student s = new Student(0l);
new CourseRequest(1l, 0, false, s, ToolBox.toList(new Course(1, "A", "1", new Offering(0, "A")), new Course(1, "A", "2", new Offering(0, "A")), new Course(1, "A", "3", new Offering(0, "A"))), false, null);
new CourseRequest(2l, 1, false, s, ToolBox.toList(new Course(1, "B", "1", new Offering(0, "B")), new Course(1, "B", "2", new Offering(0, "B")), new Course(1, "B", "3", new Offering(0, "B"))), false, null);
new CourseRequest(3l, 2, false, s, ToolBox.toList(new Course(1, "C", "1", new Offering(0, "C")), new Course(1, "C", "2", new Offering(0, "C")), new Course(1, "C", "3", new Offering(0, "C"))), false, null);
new CourseRequest(4l, 3, false, s, ToolBox.toList(new Course(1, "D", "1", new Offering(0, "D")), new Course(1, "D", "2", new Offering(0, "D")), new Course(1, "D", "3", new Offering(0, "D"))), false, null);
new CourseRequest(5l, 4, false, s, ToolBox.toList(new Course(1, "E", "1", new Offering(0, "E")), new Course(1, "E", "2", new Offering(0, "E")), new Course(1, "E", "3", new Offering(0, "E"))), false, null);
new CourseRequest(6l, 5, true, s, ToolBox.toList(new Course(1, "F", "1", new Offering(0, "F")), new Course(1, "F", "2", new Offering(0, "F")), new Course(1, "F", "3", new Offering(0, "F"))), false, null);
new CourseRequest(7l, 6, true, s, ToolBox.toList(new Course(1, "G", "1", new Offering(0, "G")), new Course(1, "G", "2", new Offering(0, "G")), new Course(1, "G", "3", new Offering(0, "G"))), false, null);
Assignment<Request, Enrollment> assignment = new DefaultSingleAssignment<Request, Enrollment>();
Placement p = new Placement(null, new TimeLocation(1, 90, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With one distance conflict:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With two distance conflicts:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<DistanceConflict.Conflict> dc = new HashSet<DistanceConflict.Conflict>();
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, (Section) sections.iterator().next()));
dc.add(new DistanceConflict.Conflict(s, e, (Section) sections.iterator().next(), e, new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null)));
w[i] = pw.getWeight(assignment, e, dc, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("With 25% time overlapping conflict:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<TimeOverlapsCounter.Conflict> toc = new HashSet<TimeOverlapsCounter.Conflict>();
toc.add(new TimeOverlapsCounter.Conflict(s, 3, e, sections.iterator().next(), e, sections.iterator().next()));
w[i] = pw.getWeight(assignment, e, null, toc);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Disbalanced sections (by 2 / 10 students):");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
Subpart x = new Subpart(0, "Lec", "Lec", cfg, null);
Section a = new Section(0, 10, "x", x, p, null);
new Section(1, 10, "y", x, p, null);
sections.add(a);
a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
a.assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
cfg.getContext(assignment).assigned(assignment, new Enrollment(s.getRequests().get(0), i, cfg, sections, assignment));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Same choice sections:");
pw.iMPP = true;
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Same time sections:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Different time sections:");
Placement q = new Placement(null, new TimeLocation(1, 102, 12, 0, 0, null, null, new BitSet(), 10), new ArrayList<RoomLocation>());
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(1, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), q, null));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
System.out.println("Two sections, one same choice, one same time:");
for (Request r : s.getRequests()) {
CourseRequest cr = (CourseRequest) r;
double[] w = new double[] { 0.0, 0.0, 0.0 };
for (int i = 0; i < cr.getCourses().size(); i++) {
Config cfg = new Config(0l, -1, "", cr.getCourses().get(i).getOffering());
Set<SctAssignment> sections = new HashSet<SctAssignment>();
sections.add(new Section(0, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
sections.add(new Section(1, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null));
Enrollment e = new Enrollment(cr, i, cfg, sections, assignment);
Set<SctAssignment> other = new HashSet<SctAssignment>();
other.add(new Section(2, 1, "x", new Subpart(0, "Lec", "Lec", cfg, null), p, null));
other.add(new Section(3, 1, "y", new Subpart(1, "Rec", "Rec", cfg, null), p, null, new Instructor(1l, null, "Josef Novak", null)));
cr.setInitialAssignment(new Enrollment(cr, i, cfg, other, assignment));
w[i] = pw.getWeight(assignment, e, null, null);
}
System.out.println(cr + ": " + df.format(w[0]) + " " + df.format(w[1]) + " " + df.format(w[2]));
}
}
Aggregations