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