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

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

Version 1.5.

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. But SAOP can be initialized with less, for
33 * use in TournamentSettings.
34 *
35 * All provided participants must
36 *
37 * @param deadline
38 * @param minimumVotes the minimum number of votes allowed for a bid.
39 */
40 @JsonCreator
41 public MOPACSettings(
42 @JsonProperty("participants") List<OnePartyTeam> participants,
43 @JsonProperty("deadline") Deadline deadline,
44 @JsonProperty("votingevaluator") VotingEvaluator votingeval) {
45 this.participants = participants;
46 this.deadline = deadline;
47 if (participants == null || deadline == null || votingeval == null)
48 throw new IllegalArgumentException(
49 "participants, deadline and votingeval must be not null");
50 this.votingevaluator = votingeval;
51 checkTeams();
52 }
53
54 @Override
55 public Double getMaxRunTime() {
56 return deadline.getDuration() / 1000d;
57 }
58
59 @Override
60 public MOPAC getProtocol(Reporter logger) {
61 return new MOPAC(new MOPACState(this), logger);
62 }
63
64 @Override
65 public List<TeamOfPartiesAndProfiles> getTeams() {
66 return Collections.unmodifiableList(participants);
67 }
68
69 /**
70 * @return the deadline for this negotiation
71 */
72 public Deadline getDeadline() {
73 return deadline;
74 }
75
76 @Override
77 public List<PartyWithProfile> getAllParties() {
78 return participants.stream()
79 .map(particip -> particip.getAllParties().get(0))
80 .collect(Collectors.toList());
81 }
82
83 /**
84 * @return a class that allows us to evaluate the voting results in
85 * different ways, selectable by the user.
86 */
87 public VotingEvaluator getVotingEvaluation() {
88 return votingevaluator;
89 }
90
91 @Override
92 public SessionSettings with(TeamOfPartiesAndProfiles party) {
93 if (!(party instanceof OnePartyTeam))
94 throw new IllegalArgumentException(
95 "Added party must be OnePartyTeam but got " + party);
96 List<OnePartyTeam> newparts = new LinkedList<>(participants);
97 newparts.add((OnePartyTeam) party);
98 return new MOPACSettings(newparts, deadline, votingevaluator);
99 }
100
101 @Override
102 public String toString() {
103 return "MOPACSettings[" + participants + "," + deadline + ","
104 + votingevaluator.getClass().getSimpleName() + "]";
105 }
106
107 @Override
108 public int hashCode() {
109 final int prime = 31;
110 int result = 1;
111 result = prime * result
112 + ((deadline == null) ? 0 : deadline.hashCode());
113 result = prime * result
114 + ((participants == null) ? 0 : participants.hashCode());
115 result = prime * result
116 + ((votingevaluator == null) ? 0 : votingevaluator.hashCode());
117 return result;
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (this == obj)
123 return true;
124 if (obj == null)
125 return false;
126 if (getClass() != obj.getClass())
127 return false;
128 MOPACSettings other = (MOPACSettings) obj;
129 if (deadline == null) {
130 if (other.deadline != null)
131 return false;
132 } else if (!deadline.equals(other.deadline))
133 return false;
134 if (participants == null) {
135 if (other.participants != null)
136 return false;
137 } else if (!participants.equals(other.participants))
138 return false;
139 if (votingevaluator == null) {
140 if (other.votingevaluator != null)
141 return false;
142 } else if (!votingevaluator.equals(other.votingevaluator))
143 return false;
144 return true;
145 }
146
147 /**
148 * @throws IllegalArgumentException if teams have improper power settings.
149 */
150 private void checkTeams() {
151 for (OnePartyTeam party : participants) {
152 Object power = party.getParty().getParameters().get("power");
153 if (power != null) {
154 if (!(power instanceof Integer))
155 throw new IllegalArgumentException(
156 "parameter 'power' for party" + party
157 + " must be integer but found " + power);
158 if ((Integer) power < 1)
159 throw new IllegalArgumentException(
160 "parameter 'power' for party" + party
161 + " must be >=1 but found " + power);
162
163 }
164 }
165 }
166
167}
Note: See TracBrowser for help on using the repository browser.