001package org.cpsolver.coursett.criteria.additional;
002
003import java.util.Collection;
004import java.util.Map;
005
006import org.cpsolver.coursett.criteria.StudentConflict;
007import org.cpsolver.coursett.model.Lecture;
008import org.cpsolver.coursett.model.Placement;
009import org.cpsolver.coursett.model.RoomLocation;
010import org.cpsolver.ifs.assignment.Assignment;
011import org.cpsolver.ifs.util.DataProperties;
012
013/**
014 * An experimental criterion that tries to minimize cases where a student has an online and in-person
015 * class on the same day. Online classes are identified by a regular expression matching the room name
016 * and set in the General.OnlineRoom parameter (defaults to (?i)ONLINE|). Classes without a 
017 * room are considered online when the General.OnlineRoom parameter matches a blank string.
018 * If a class has multiple rooms, all rooms must be online for the class to be considered online. 
019 * The criterion is weighted by the Comparator.StudentOnlineConflictWeight parameter, defaults
020 * to one half of the Comparator.StudentConflictWeight.
021 * <br>
022 * 
023 * @author  Tomáš Müller
024 * @version CourseTT 1.3 (University Course Timetabling)<br>
025 *          Copyright (C) 2013 - 2023 Tomáš Müller<br>
026 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
027 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
028 * <br>
029 *          This library is free software; you can redistribute it and/or modify
030 *          it under the terms of the GNU Lesser General Public License as
031 *          published by the Free Software Foundation; either version 3 of the
032 *          License, or (at your option) any later version. <br>
033 * <br>
034 *          This library is distributed in the hope that it will be useful, but
035 *          WITHOUT ANY WARRANTY; without even the implied warranty of
036 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
037 *          Lesser General Public License for more details. <br>
038 * <br>
039 *          You should have received a copy of the GNU Lesser General Public
040 *          License along with this library; if not see
041 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
042 */
043public class StudentOnlineConflict extends StudentConflict {
044    private String iOnlineRoom = null;
045    
046    @Override
047    public void configure(DataProperties properties) {   
048        super.configure(properties);
049        iOnlineRoom = properties.getProperty("StudentConflict.OnlineRoom",
050                properties.getProperty("General.OnlineRoom", "(?i)ONLINE|"));
051    }
052    
053    public boolean isOnline(Placement p) {
054        // no room -- StudentConflict.OnlineRoom must allow for a blank string
055        if (p.getNrRooms() == 0)
056            return "".matches(iOnlineRoom);
057        // one room -- room name must match StudentConflict.OnlineRoom
058        if (p.getNrRooms() == 1)
059            return (p.getRoomLocation().getName() != null && p.getRoomLocation().getName().matches(iOnlineRoom));
060        // multiple rooms -- all rooms must match StudentConflict.OnlineRoom
061        for (RoomLocation r: p.getRoomLocations())
062            if (r.getName() == null || !r.getName().matches(iOnlineRoom)) return false;
063        return true;
064    }
065    
066    /**
067     * Are the two placements at the same day? True when the two placements share days and weeks.
068     */
069    public boolean shareDays(Placement p1, Placement p2) {
070        return p1 != null && p2 != null && p1.getTimeLocation().shareDays(p2.getTimeLocation()) && p1.getTimeLocation().shareWeeks(p2.getTimeLocation());
071    }
072    
073    /**
074     * There is a conflict when {@link StudentOnlineConflict#isOnline(Placement)} differs for the two placements
075     * and they are placed on the same day ({@link StudentOnlineConflict#shareDays(Placement, Placement)} returns true).
076     */
077    @Override
078    public boolean inConflict(Placement p1, Placement p2) {
079        return p1 != null && p2 != null && isOnline(p1) != isOnline(p2) && shareDays(p1, p2);
080    }
081    
082    @Override
083    public boolean isApplicable(Lecture l1, Lecture l2) {
084        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
085    }
086    
087    @Override
088    public double getWeightDefault(DataProperties config) {
089        return config.getPropertyDouble("Comparator.StudentOnlineConflictWeight", 0.5 * config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
090    }
091    
092    @Override
093    public String getPlacementSelectionWeightName() {
094        return "Placement.StudentOnlineConflictWeight";
095    }
096    
097    @Override
098    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
099        super.getInfo(assignment, info);
100        double conf = getValue(assignment);
101        if (conf > 0.0)
102            info.put("Student online conflicts", sDoubleFormat.format(conf));
103    }
104    
105    @Override
106    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
107        super.getInfo(assignment, info, variables);
108        double conf = getValue(assignment, variables);
109        if (conf > 0.0)
110            info.put("Student online conflicts", sDoubleFormat.format(conf));
111    }
112}