[52] | 1 | package geniusweb.protocol.session.mopac;
|
---|
| 2 |
|
---|
| 3 | import java.util.Collections;
|
---|
| 4 | import java.util.LinkedList;
|
---|
| 5 | import java.util.List;
|
---|
| 6 | import java.util.stream.Collectors;
|
---|
| 7 |
|
---|
| 8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 10 |
|
---|
| 11 | import geniusweb.deadline.Deadline;
|
---|
| 12 | import geniusweb.protocol.session.SessionSettings;
|
---|
| 13 | import geniusweb.protocol.session.TeamInfo;
|
---|
| 14 | import geniusweb.references.PartyWithProfile;
|
---|
| 15 | import geniusweb.voting.VotingEvaluator;
|
---|
| 16 | import tudelft.utilities.logging.Reporter;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Settings for MOPAC negotiation. in MOPAC, each party may get a "power"
|
---|
| 20 | * parameter containing an natural number ≤1.
|
---|
| 21 | */
|
---|
| 22 | public class MOPACSettings implements SessionSettings {
|
---|
| 23 | private final List<TeamInfo> participants;
|
---|
| 24 | private final Deadline deadline;
|
---|
| 25 | private final VotingEvaluator votingevaluator;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | *
|
---|
| 29 | * @param participants the list of {@link PartyWithProfile} in clockwise
|
---|
| 30 | * order. There must be at least 2 to run the MOPAC
|
---|
| 31 | * protocol. This is not tested in the constructor
|
---|
| 32 | * because this can be initialized with less, for use in
|
---|
| 33 | * TournamentSettings.
|
---|
| 34 | * @param deadline the {@link Deadline} for the negotiation
|
---|
| 35 | * @param votingeval the {@link VotingEvaluator} to use.
|
---|
| 36 | */
|
---|
| 37 | @JsonCreator
|
---|
| 38 | public MOPACSettings(
|
---|
| 39 | @JsonProperty("participants") List<TeamInfo> participants,
|
---|
| 40 | @JsonProperty("deadline") Deadline deadline,
|
---|
| 41 | @JsonProperty("votingevaluator") VotingEvaluator votingeval) {
|
---|
| 42 | this.participants = participants;
|
---|
| 43 | this.deadline = deadline;
|
---|
| 44 | if (participants == null || deadline == null || votingeval == null)
|
---|
| 45 | throw new IllegalArgumentException(
|
---|
| 46 | "participants, deadline and votingeval must be not null");
|
---|
| 47 | this.votingevaluator = votingeval;
|
---|
| 48 | checkTeams();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Override
|
---|
| 52 | public Double getMaxRunTime() {
|
---|
| 53 | return deadline.getDuration() / 1000d;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
| 57 | public MOPAC getProtocol(Reporter logger) {
|
---|
| 58 | return new MOPAC(new MOPACState(this), logger);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | @Override
|
---|
| 62 | public List<TeamInfo> getTeams() {
|
---|
| 63 | return Collections.unmodifiableList(participants);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * @return the deadline for this negotiation
|
---|
| 68 | */
|
---|
| 69 | public Deadline getDeadline() {
|
---|
| 70 | return deadline;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Override
|
---|
| 74 | public List<PartyWithProfile> getAllParties() {
|
---|
| 75 | return participants.stream()
|
---|
| 76 | .map(particip -> particip.getParties().get(0))
|
---|
| 77 | .collect(Collectors.toList());
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @return a class that allows us to evaluate the voting results in
|
---|
| 82 | * different ways, selectable by the user.
|
---|
| 83 | */
|
---|
| 84 | public VotingEvaluator getVotingEvaluator() {
|
---|
| 85 | return votingevaluator;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public MOPACSettings with(TeamInfo team) {
|
---|
| 90 | if (team.getSize() != 1)
|
---|
| 91 | throw new IllegalArgumentException(
|
---|
| 92 | "Added party must have one party but got " + team);
|
---|
| 93 | List<TeamInfo> newparts = new LinkedList<>(participants);
|
---|
| 94 | newparts.add(team);
|
---|
| 95 | return new MOPACSettings(newparts, deadline, votingevaluator);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public String toString() {
|
---|
| 100 | return "MOPACSettings[" + participants + "," + deadline + ","
|
---|
| 101 | + votingevaluator.getClass().getSimpleName() + "]";
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public Integer getTeamSize() {
|
---|
| 106 | return 1;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Override
|
---|
| 110 | public int hashCode() {
|
---|
| 111 | final int prime = 31;
|
---|
| 112 | int result = 1;
|
---|
| 113 | result = prime * result
|
---|
| 114 | + ((deadline == null) ? 0 : deadline.hashCode());
|
---|
| 115 | result = prime * result
|
---|
| 116 | + ((participants == null) ? 0 : participants.hashCode());
|
---|
| 117 | result = prime * result
|
---|
| 118 | + ((votingevaluator == null) ? 0 : votingevaluator.hashCode());
|
---|
| 119 | return result;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | @Override
|
---|
| 123 | public boolean equals(Object obj) {
|
---|
| 124 | if (this == obj)
|
---|
| 125 | return true;
|
---|
| 126 | if (obj == null)
|
---|
| 127 | return false;
|
---|
| 128 | if (getClass() != obj.getClass())
|
---|
| 129 | return false;
|
---|
| 130 | MOPACSettings other = (MOPACSettings) obj;
|
---|
| 131 | if (deadline == null) {
|
---|
| 132 | if (other.deadline != null)
|
---|
| 133 | return false;
|
---|
| 134 | } else if (!deadline.equals(other.deadline))
|
---|
| 135 | return false;
|
---|
| 136 | if (participants == null) {
|
---|
| 137 | if (other.participants != null)
|
---|
| 138 | return false;
|
---|
| 139 | } else if (!participants.equals(other.participants))
|
---|
| 140 | return false;
|
---|
| 141 | if (votingevaluator == null) {
|
---|
| 142 | if (other.votingevaluator != null)
|
---|
| 143 | return false;
|
---|
| 144 | } else if (!votingevaluator.equals(other.votingevaluator))
|
---|
| 145 | return false;
|
---|
| 146 | return true;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * @throws IllegalArgumentException if teams have improper power settings.
|
---|
| 151 | */
|
---|
| 152 | private void checkTeams() {
|
---|
| 153 | for (TeamInfo team : participants) {
|
---|
| 154 | if (team.getSize() != 1)
|
---|
| 155 | throw new IllegalArgumentException(
|
---|
| 156 | "All teams must be size 1 but found " + team);
|
---|
| 157 | PartyWithProfile party = team.getParties().get(0);
|
---|
| 158 | Object power = party.getParty().getParameters().get("power");
|
---|
| 159 | if (power != null) {
|
---|
| 160 | if (!(power instanceof Integer))
|
---|
| 161 | throw new IllegalArgumentException(
|
---|
| 162 | "parameter 'power' for party" + party
|
---|
| 163 | + " must be integer but found " + power);
|
---|
| 164 | if ((Integer) power < 1)
|
---|
| 165 | throw new IllegalArgumentException(
|
---|
| 166 | "parameter 'power' for party" + party
|
---|
| 167 | + " must be >=1 but found " + power);
|
---|
| 168 |
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | }
|
---|