use of org.cpsolver.ifs.model.SimpleNeighbour in project cpsolver by UniTime.
the class RandomSwapMove method selectNeighbour.
@Override
public Neighbour<V, T> selectNeighbour(Solution<V, T> solution) {
Model<V, T> model = solution.getModel();
Assignment<V, T> assignment = solution.getAssignment();
double total = model.getTotalValue(assignment);
int varIdx = ToolBox.random(model.variables().size());
for (int i = 0; i < model.variables().size(); i++) {
V variable = model.variables().get((i + varIdx) % model.variables().size());
List<T> values = variable.values(solution.getAssignment());
if (values.isEmpty())
continue;
int valIdx = ToolBox.random(values.size());
T old = variable.getAssignment(assignment);
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
int attempts = 0;
long startTime = JProf.currentTimeMillis();
for (int j = 0; j < values.size(); j++) {
T value = values.get((j + valIdx) % values.size());
if (value.equals(old))
continue;
Set<T> conflicts = model.conflictValues(assignment, value);
if (conflicts.contains(value))
continue;
if (conflicts.isEmpty()) {
SimpleNeighbour<V, T> n = new SimpleNeighbour<V, T>(variable, value);
if (!iHC || n.value(assignment) <= 0)
return n;
else
continue;
}
Map<V, T> assignments = new HashMap<V, T>();
assignments.put(variable, value);
for (T conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
assignment.assign(solution.getIteration(), value);
Double v = resolve(solution, total, startTime, assignments, new ArrayList<T>(conflicts), 0);
if (!conflicts.isEmpty())
attempts++;
assignment.unassign(solution.getIteration(), variable);
for (T conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
if (old != null)
assignment.assign(solution.getIteration(), old);
if (v != null)
return new SwapNeighbour(assignments.values(), v);
if (attempts >= iMaxAttempts)
break;
}
} finally {
lock.unlock();
}
}
return null;
}
use of org.cpsolver.ifs.model.SimpleNeighbour in project cpsolver by UniTime.
the class ExamTabuSearch method selectNeighbour.
/**
* Neighbor selection
*/
@Override
public Neighbour<Exam, ExamPlacement> selectNeighbour(Solution<Exam, ExamPlacement> solution) {
if (iFirstIteration < 0)
iFirstIteration = solution.getIteration();
TabuList tabu = getContext(solution.getAssignment());
long idle = solution.getIteration() - Math.max(iFirstIteration, solution.getBestIteration());
if (idle > iMaxIdleIterations) {
sLog.debug(" [tabu] max idle iterations reached");
iFirstIteration = -1;
if (tabu.size() > 0)
tabu.clear();
return null;
}
if (tabu.size() > 0 && iTabuMaxSize > iTabuMinSize) {
if (idle == 0) {
tabu.resize(iTabuMinSize);
} else if (idle % (iMaxIdleIterations / (iTabuMaxSize - iTabuMinSize)) == 0) {
tabu.resize(Math.min(iTabuMaxSize, tabu.size() + 1));
}
}
boolean acceptConflicts = solution.getModel().getBestUnassignedVariables() > 0;
ExamModel model = (ExamModel) solution.getModel();
Assignment<Exam, ExamPlacement> assignment = solution.getAssignment();
double bestEval = 0.0;
List<ExamPlacement> best = null;
for (Exam exam : model.variables()) {
ExamPlacement assigned = assignment.getValue(exam);
double assignedVal = (assigned == null ? iConflictWeight : iValueWeight * assigned.toDouble(assignment));
for (ExamPeriodPlacement period : exam.getPeriodPlacements()) {
Set<ExamRoomPlacement> rooms = exam.findBestAvailableRooms(assignment, period);
if (rooms == null)
rooms = exam.findRoomsRandom(assignment, period, false);
if (rooms == null)
continue;
ExamPlacement value = new ExamPlacement(exam, period, rooms);
if (value.equals(assigned))
continue;
double eval = iValueWeight * value.toDouble(assignment) - assignedVal;
if (acceptConflicts) {
Set<ExamPlacement> conflicts = model.conflictValues(assignment, value);
for (ExamPlacement conflict : conflicts) {
eval -= iValueWeight * conflict.toDouble(assignment);
eval += iConflictWeight * (1.0 + (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflict, value)));
}
} else {
if (model.inConflict(assignment, value))
continue;
}
if (tabu.size() > 0 && tabu.contains(exam.getId() + ":" + value.getPeriod().getIndex())) {
int un = model.variables().size() - assignment.nrAssignedVariables() - (assigned == null ? 0 : 1);
if (un > model.getBestUnassignedVariables())
continue;
if (un == model.getBestUnassignedVariables() && model.getTotalValue(assignment) + eval >= solution.getBestValue())
continue;
}
if (best == null || bestEval > eval) {
if (best == null)
best = new ArrayList<ExamPlacement>();
else
best.clear();
best.add(value);
bestEval = eval;
} else if (bestEval == eval) {
best.add(value);
}
}
}
if (best == null) {
sLog.debug(" [tabu] --none--");
iFirstIteration = -1;
if (tabu.size() > 0)
tabu.clear();
return null;
}
ExamPlacement bestVal = ToolBox.random(best);
if (sLog.isDebugEnabled()) {
Set<ExamPlacement> conflicts = model.conflictValues(assignment, bestVal);
double wconf = (iStat == null ? 0.0 : iStat.countRemovals(solution.getIteration(), conflicts, bestVal));
sLog.debug(" [tabu] " + bestVal + " (" + (assignment.getValue(bestVal.variable()) == null ? "" : "was=" + assignment.getValue(bestVal.variable()) + ", ") + "val=" + bestEval + (conflicts.isEmpty() ? "" : ", conf=" + (wconf + conflicts.size()) + "/" + conflicts) + ")");
}
if (tabu.size() > 0)
tabu.add(bestVal.variable().getId() + ":" + bestVal.getPeriod().getIndex());
return new SimpleNeighbour<Exam, ExamPlacement>(bestVal.variable(), bestVal);
}
use of org.cpsolver.ifs.model.SimpleNeighbour in project cpsolver by UniTime.
the class RoomSwap method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
double total = model.getTotalValue(assignment);
int varIdx = ToolBox.random(model.variables().size());
for (int i = 0; i < model.variables().size(); i++) {
Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
Placement old = assignment.getValue(lecture);
if (old == null || old.getNrRooms() != 1)
continue;
List<RoomLocation> values = lecture.roomLocations();
if (values.isEmpty())
continue;
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
int attempts = 0;
int valIdx = ToolBox.random(values.size());
long startTime = JProf.currentTimeMillis();
for (int j = 0; j < values.size(); j++) {
RoomLocation room = values.get((j + valIdx) % values.size());
if (room.getPreference() > 50)
continue;
if (room.equals(old.getRoomLocation()))
continue;
Placement placement = new Placement(lecture, old.getTimeLocation(), room);
if (!placement.isValid())
continue;
Set<Placement> conflicts = model.conflictValues(assignment, placement);
if (conflicts.contains(placement))
continue;
if (conflicts.isEmpty()) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
else
continue;
}
Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
assignments.put(lecture, placement);
for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
assignment.assign(solution.getIteration(), placement);
Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
if (!conflicts.isEmpty())
attempts++;
assignment.unassign(solution.getIteration(), lecture);
for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
assignment.assign(solution.getIteration(), old);
if (v != null)
return new SwapNeighbour(assignments.values(), v);
if (attempts >= iMaxAttempts)
break;
}
} finally {
lock.unlock();
}
}
return null;
}
use of org.cpsolver.ifs.model.SimpleNeighbour in project cpsolver by UniTime.
the class TimeSwap method selectNeighbour.
@Override
public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
TimetableModel model = (TimetableModel) solution.getModel();
Assignment<Lecture, Placement> assignment = solution.getAssignment();
double total = model.getTotalValue(assignment);
int varIdx = ToolBox.random(model.variables().size());
for (int i = 0; i < model.variables().size(); i++) {
Lecture lecture = model.variables().get((i + varIdx) % model.variables().size());
Placement old = lecture.getAssignment(assignment);
if (old == null)
continue;
List<TimeLocation> values = lecture.timeLocations();
if (values.isEmpty())
continue;
Lock lock = solution.getLock().writeLock();
lock.lock();
try {
int attempts = 0;
long startTime = JProf.currentTimeMillis();
int valIdx = ToolBox.random(values.size());
for (int j = 0; j < values.size(); j++) {
TimeLocation time = values.get((j + valIdx) % values.size());
if (time.getPreference() > 50)
continue;
if (time.equals(old.getTimeLocation()))
continue;
Placement placement = null;
if (lecture.getNrRooms() == 0)
placement = new Placement(lecture, time, (RoomLocation) null);
else if (lecture.getNrRooms() == 1)
placement = new Placement(lecture, time, old.getRoomLocation());
else
placement = new Placement(lecture, time, old.getRoomLocations());
if (!placement.isValid())
continue;
Set<Placement> conflicts = model.conflictValues(assignment, placement);
if (conflicts.contains(placement))
continue;
if (conflicts.isEmpty()) {
SimpleNeighbour<Lecture, Placement> n = new SimpleNeighbour<Lecture, Placement>(lecture, placement);
if (!iHC || n.value(assignment) <= 0)
return n;
else
continue;
}
Map<Lecture, Placement> assignments = new HashMap<Lecture, Placement>();
assignments.put(lecture, placement);
for (Placement conflict : conflicts) assignment.unassign(solution.getIteration(), conflict.variable());
assignment.assign(solution.getIteration(), placement);
Double v = resolve(solution, total, startTime, assignments, new ArrayList<Placement>(conflicts), 0);
if (!conflicts.isEmpty())
attempts++;
assignment.unassign(solution.getIteration(), lecture);
for (Placement conflict : conflicts) assignment.assign(solution.getIteration(), conflict);
assignment.assign(solution.getIteration(), old);
if (v != null)
return new SwapNeighbour(assignments.values(), v);
if (attempts >= iMaxAttempts)
break;
}
} finally {
lock.unlock();
}
}
return null;
}
use of org.cpsolver.ifs.model.SimpleNeighbour in project cpsolver by UniTime.
the class ShuffleStudentsSelection method selectNeighbour.
@Override
public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
Shuffle shuffle = null;
while ((shuffle = iQueue.poll()) != null) {
Progress.getInstance(solution.getModel()).incProgress();
// Take an item from the queue, try backtrack first
Neighbour<Request, Enrollment> n = iBacktrack.selectNeighbour(solution, shuffle);
if (n != null)
return n;
// If fails, just assign the request randomly and hope for the best
List<Enrollment> adepts = new ArrayList<Enrollment>();
for (Enrollment e : shuffle.getRequest().values(solution.getAssignment())) {
if (shuffle.matchFilter(e))
adepts.add(e);
}
if (!adepts.isEmpty()) {
return new SimpleNeighbour<Request, Enrollment>(shuffle.getRequest(), ToolBox.random(adepts));
}
}
return null;
}
Aggregations