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 * @version CourseTT 1.3 (University Course Timetabling)<br>
024 *          Copyright (C) 2013 - 2023 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 */
042public class StudentOnlineConflict extends StudentConflict {
043    private String iOnlineRoom = null;
044    
045    @Override
046    public void configure(DataProperties properties) {   
047        super.configure(properties);
048        iOnlineRoom = properties.getProperty("StudentConflict.OnlineRoom",
049                properties.getProperty("General.OnlineRoom", "(?i)ONLINE|"));
050    }
051    
052    public boolean isOnline(Placement p) {
053        // no room -- StudentConflict.OnlineRoom must allow for a blank string
054        if (p.getNrRooms() == 0)
055            return "".matches(iOnlineRoom);
056        // one room -- room name must match StudentConflict.OnlineRoom
057        if (p.getNrRooms() == 1)
058            return (p.getRoomLocation().getName() != null && p.getRoomLocation().getName().matches(iOnlineRoom));
059        // multiple rooms -- all rooms must match StudentConflict.OnlineRoom
060        for (RoomLocation r: p.getRoomLocations())
061            if (r.getName() == null || !r.getName().matches(iOnlineRoom)) return false;
062        return true;
063    }
064    
065    /**
066     * Are the two placements at the same day? True when the two placements share days and weeks.
067     */
068    public boolean shareDays(Placement p1, Placement p2) {
069        return p1 != null && p2 != null && p1.getTimeLocation().shareDays(p2.getTimeLocation()) && p1.getTimeLocation().shareWeeks(p2.getTimeLocation());
070    }
071    
072    /**
073     * There is a conflict when {@link StudentOnlineConflict#isOnline(Placement)} differs for the two placements
074     * and they are placed on the same day ({@link StudentOnlineConflict#shareDays(Placement, Placement)} returns true).
075     */
076    @Override
077    public boolean inConflict(Placement p1, Placement p2) {
078        return p1 != null && p2 != null && isOnline(p1) != isOnline(p2) && shareDays(p1, p2);
079    }
080    
081    @Override
082    public boolean isApplicable(Lecture l1, Lecture l2) {
083        return l1 != null && l2 != null && !ignore(l1, l2) && applicable(l1, l2);
084    }
085    
086    @Override
087    public double getWeightDefault(DataProperties config) {
088        return config.getPropertyDouble("Comparator.StudentOnlineConflictWeight", 0.5 * config.getPropertyDouble("Comparator.StudentConflictWeight", 1.0));
089    }
090    
091    @Override
092    public String getPlacementSelectionWeightName() {
093        return "Placement.StudentOnlineConflictWeight";
094    }
095    
096    @Override
097    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info) {
098        super.getInfo(assignment, info);
099        double conf = getValue(assignment);
100        if (conf > 0.0)
101            info.put("Student online conflicts", sDoubleFormat.format(conf));
102    }
103    
104    @Override
105    public void getInfo(Assignment<Lecture, Placement> assignment, Map<String, String> info, Collection<Lecture> variables) {
106        super.getInfo(assignment, info, variables);
107        double conf = getValue(assignment, variables);
108        if (conf > 0.0)
109            info.put("Student online conflicts", sDoubleFormat.format(conf));
110    }
111}