source: protocol/src/main/java/geniusweb/protocol/session/mopac/MOPACSettings.java@ 28

Last change on this file since 28 was 28, checked in by bart, 4 years ago

minor fixes to improve extendability

File size: 4.8 KB
Line 
1package geniusweb.protocol.session.mopac;
2
3import java.util.Collections;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.stream.Collectors;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9import com.fasterxml.jackson.annotation.JsonProperty;
10
11import geniusweb.deadline.Deadline;
12import geniusweb.protocol.session.OnePartyTeam;
13import geniusweb.protocol.session.SessionSettings;
14import geniusweb.protocol.session.TeamOfPartiesAndProfiles;
15import geniusweb.references.PartyWithProfile;
16import geniusweb.voting.VotingEvaluator;
17import 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 */
23public class MOPACSettings implements SessionSettings {
24 private final List<OnePartyTeam> participants;
25 private final Deadline deadline;
26 private final VotingEvaluator 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 MOPACSettings(
40 @JsonProperty("participants") List<OnePartyTeam> participants,
41 @JsonProperty("deadline") Deadline deadline,
42 @JsonProperty("votingevaluator") VotingEvaluator 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 MOPAC getProtocol(Reporter logger) {
59 return new MOPAC(new MOPACState(this), logger);
60 }
61
62 @Override
63 public List<TeamOfPartiesAndProfiles> 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.getAllParties().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 VotingEvaluator getVotingEvaluation() {
86 return votingevaluator;
87 }
88
89 @Override
90 public MOPACSettings with(TeamOfPartiesAndProfiles party) {
91 if (!(party instanceof OnePartyTeam))
92 throw new IllegalArgumentException(
93 "Added party must be OnePartyTeam but got " + party);
94 List<OnePartyTeam> newparts = new LinkedList<>(participants);
95 newparts.add((OnePartyTeam) party);
96 return new MOPACSettings(newparts, deadline, votingevaluator);
97 }
98
99 @Override
100 public String toString() {
101 return "MOPACSettings[" + participants + "," + deadline + ","
102 + votingevaluator.getClass().getSimpleName() + "]";
103 }
104
105 @Override
106 public int hashCode() {
107 final int prime = 31;
108 int result = 1;
109 result = prime * result
110 + ((deadline == null) ? 0 : deadline.hashCode());
111 result = prime * result
112 + ((participants == null) ? 0 : participants.hashCode());
113 result = prime * result
114 + ((votingevaluator == null) ? 0 : votingevaluator.hashCode());
115 return result;
116 }
117
118 @Override
119 public boolean equals(Object obj) {
120 if (this == obj)
121 return true;
122 if (obj == null)
123 return false;
124 if (getClass() != obj.getClass())
125 return false;
126 MOPACSettings other = (MOPACSettings) obj;
127 if (deadline == null) {
128 if (other.deadline != null)
129 return false;
130 } else if (!deadline.equals(other.deadline))
131 return false;
132 if (participants == null) {
133 if (other.participants != null)
134 return false;
135 } else if (!participants.equals(other.participants))
136 return false;
137 if (votingevaluator == null) {
138 if (other.votingevaluator != null)
139 return false;
140 } else if (!votingevaluator.equals(other.votingevaluator))
141 return false;
142 return true;
143 }
144
145 /**
146 * @throws IllegalArgumentException if teams have improper power settings.
147 */
148 private void checkTeams() {
149 for (OnePartyTeam party : participants) {
150 Object power = party.getParty().getParameters().get("power");
151 if (power != null) {
152 if (!(power instanceof Integer))
153 throw new IllegalArgumentException(
154 "parameter 'power' for party" + party
155 + " must be integer but found " + power);
156 if ((Integer) power < 1)
157 throw new IllegalArgumentException(
158 "parameter 'power' for party" + party
159 + " must be >=1 but found " + power);
160
161 }
162 }
163 }
164
165}
Note: See TracBrowser for help on using the repository browser.