source: protocol/src/main/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsSettings.java@ 44

Last change on this file since 44 was 44, checked in by bart, 2 years ago

Added time-dependent parties for python and simpleRunner-GUI for java

File size: 11.5 KB
Line 
1package geniusweb.protocol.tournament.allpermutations;
2
3import java.math.BigInteger;
4import java.util.Collections;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.function.BiFunction;
8
9import com.fasterxml.jackson.annotation.JsonCreator;
10import com.fasterxml.jackson.annotation.JsonProperty;
11
12import geniusweb.protocol.session.SessionSettings;
13import geniusweb.protocol.session.TeamInfo;
14import geniusweb.protocol.session.saop.SAOPSettings;
15import geniusweb.protocol.session.shaop.SHAOPSettings;
16import geniusweb.protocol.tournament.ProfileList;
17import geniusweb.protocol.tournament.Team;
18import geniusweb.protocol.tournament.TournamentProtocol;
19import geniusweb.protocol.tournament.TournamentSettings;
20import geniusweb.references.PartyWithParameters;
21import geniusweb.references.PartyWithProfile;
22import geniusweb.references.ProfileRef;
23import tudelft.utilities.immutablelist.FixedList;
24import tudelft.utilities.immutablelist.ImmutableList;
25import tudelft.utilities.immutablelist.MapList;
26import tudelft.utilities.immutablelist.MapThreadList;
27import tudelft.utilities.immutablelist.Permutations;
28import tudelft.utilities.immutablelist.PermutationsOrderedWithoutReturn;
29import tudelft.utilities.immutablelist.PermutationsWithReturn;
30import tudelft.utilities.immutablelist.PermutationsWithoutReturn;
31import tudelft.utilities.immutablelist.Repeat;
32import tudelft.utilities.immutablelist.Tuple;
33import tudelft.utilities.immutablelist.Tuples;
34import tudelft.utilities.logging.Reporter;
35
36/**
37 * Takes a number of parties and profiles and makes all permutations of them
38 * with the given number of parties and profiles. All profiles must be in the
39 * same domain.
40 * <p>
41 * All sessions are based on the given {@link SessionSettings} (can be
42 * {@link SAOPSettings} or {@link SHAOPSettings}). The parties for each
43 * tournament session are added to this basic sessionsettings. sessionsettings
44 * contains basically all the normal run-sessionsettings. This is used as a
45 * "template" for all sessions of the tournament. You can put any use any
46 * session setting here, and each session will be run according to the protocol
47 * you select here.
48 *
49 * The participants list usually is empty. The AllPermutationsProtocol adds the
50 * the required parties to this list. So if you provide a non-empty list here,
51 * then these parties would be present in every session in the tournament.
52 * <p>
53 * If the tournament is used with a SHAOP sessionsettings, the first member of
54 * each team is assumed to be the SHAOP party, the second the COB party. To
55 * match this, the first element of the ProfileList will have to be a partial
56 * profile to fit the needs of the SHAOP party. The second profile will have to
57 * be a normal profile, to fit the needs of the COB party. Note that you can
58 * generate a partial profile using the <code>partial=N</code> option of the
59 * profiles server.
60 */
61public class AllPermutationsSettings implements TournamentSettings {
62
63 protected final List<Team> teams;
64 protected final List<ProfileList> profileslists;
65 protected final boolean reuseTeams;
66 protected final int teamsPerSession;
67 protected final SessionSettings sessionsettings;
68 protected final int numberTournaments;
69
70 /**
71 *
72 * @param teams a list of {@link Team}s. Must contain at least
73 * {@link #teamsPerSession} elements. The
74 * {@link #teamsPerSession} must match the protocol:
75 * (SAOP:1, SHAOP:2)
76 * @param reuseTeams if true, we use PermutationsWithReturn, if false
77 * we use PermutationsWithoutReturn to create the
78 * teams.
79 * @param plists list of available {@link ProfileList}s to be
80 * permutated over the teams. Each
81 * {@link ProfileList} must contain
82 * {@link #teamsPerSession} elements.
83 * @param teamsPerSession number of parties per session, must be at least 2.
84 * @param sesettings The generic {@link SessionSettings}.
85 * {@link SessionSettings#with(TeamInfo)} will be
86 * used to add the required {@link TeamInfo}s
87 * @param nTournaments the number of times the tournament should be run.
88 */
89 @JsonCreator
90 public AllPermutationsSettings(@JsonProperty("teams") List<Team> teams,
91 @JsonProperty("profileslists") List<ProfileList> plists,
92 @JsonProperty("reuseTeams") boolean reuseTeams,
93 @JsonProperty("teamsPerSession") int teamsPerSession,
94 @JsonProperty("sessionsettings") SessionSettings sesettings,
95 @JsonProperty("numberTournaments") int nTournaments) {
96 if (teamsPerSession < 2)
97 throw new IllegalArgumentException("teamsPerSession must be >=2");
98 if (teams == null || teams.size() < teamsPerSession)
99 throw new IllegalArgumentException("teams must contain at least "
100 + teamsPerSession + " teams");
101 if (plists == null || plists.size() < 2)
102 throw new IllegalArgumentException(
103 "profileslist must contain at least " + teamsPerSession
104 + " ProfileList's");
105 if (nTournaments <= 0)
106 throw new IllegalArgumentException("nTournaments must be >0");
107 int teamsize = sesettings.getTeamSize();
108
109 for (Team team : teams) {
110 if (team.getParties().size() != teamsize) {
111 throw new IllegalArgumentException("All teams should have size "
112 + teamsize + " but found " + team.getParties().size());
113 }
114 }
115 for (ProfileList profile : plists) {
116 if (profile.getProfiles().size() != teamsize) {
117 throw new IllegalArgumentException(
118 "All profiles should have size equal to the team size "
119 + teamsize + " but found "
120 + profile.getProfiles().size());
121 }
122 }
123 this.teamsPerSession = teamsPerSession;
124 this.reuseTeams = reuseTeams;
125 this.teams = teams;
126 this.profileslists = plists;
127 this.sessionsettings = sesettings;
128 this.numberTournaments = nTournaments;
129 }
130
131 @Override
132 public Double getMaxRunTime() {
133 return permutations().size().doubleValue()
134 * (sessionsettings.getMaxRunTime() + 2d);
135 }
136
137 /**
138 *
139 * @return number of times the entire tournament is repeated, &gt;0
140 */
141 public int getNrTournaments() {
142 return this.numberTournaments;
143 }
144
145 @Override
146 public TournamentProtocol getProtocol(Reporter logger) {
147 AllPermutationsState state = new AllPermutationsState(this,
148 permutations(), Collections.emptyList());
149 return new AllPermutationsProtocol(state, logger);
150 }
151
152 /**
153 * @return {@link ImmutableList} containing all {@link SessionSettings} for
154 * all Sessions in the correct order.
155 */
156 public ImmutableList<SessionSettings> permutations() {
157 ImmutableList<ImmutableList<TeamInfo>> partylistlist;
158 ImmutableList<Team> partieslist = new FixedList<Team>(teams);
159 ImmutableList<ProfileList> profileslist = new FixedList<ProfileList>(
160 profileslists);
161 partylistlist = getParticipantsOneTournament(partieslist, profileslist,
162 teamsPerSession, reuseTeams);
163 partylistlist = new Repeat<ImmutableList<TeamInfo>>(partylistlist,
164 BigInteger.valueOf(numberTournaments), true);
165
166 return new MapList<>(partyproflist -> createSetting(partyproflist),
167 partylistlist);
168 }
169
170 @Override
171 public String toString() {
172 return "AllPermutationsSettings[" + contentString() + "]";
173 }
174
175 protected String contentString() {
176 return teams + "," + reuseTeams + "," + profileslists + ","
177 + teamsPerSession + "," + sessionsettings + ","
178 + numberTournaments;
179 }
180
181 @Override
182 public int hashCode() {
183 final int prime = 31;
184 int result = 1;
185 result = prime * result + numberTournaments;
186 result = prime * result
187 + ((profileslists == null) ? 0 : profileslists.hashCode());
188 result = prime * result + (reuseTeams ? 1231 : 1237);
189 result = prime * result
190 + ((sessionsettings == null) ? 0 : sessionsettings.hashCode());
191 result = prime * result + ((teams == null) ? 0 : teams.hashCode());
192 result = prime * result + teamsPerSession;
193 return result;
194 }
195
196 @Override
197 public boolean equals(Object obj) {
198 if (this == obj)
199 return true;
200 if (obj == null)
201 return false;
202 if (getClass() != obj.getClass())
203 return false;
204 AllPermutationsSettings other = (AllPermutationsSettings) obj;
205 if (numberTournaments != other.numberTournaments)
206 return false;
207 if (profileslists == null) {
208 if (other.profileslists != null)
209 return false;
210 } else if (!profileslists.equals(other.profileslists))
211 return false;
212 if (reuseTeams != other.reuseTeams)
213 return false;
214 if (sessionsettings == null) {
215 if (other.sessionsettings != null)
216 return false;
217 } else if (!sessionsettings.equals(other.sessionsettings))
218 return false;
219 if (teams == null) {
220 if (other.teams != null)
221 return false;
222 } else if (!teams.equals(other.teams))
223 return false;
224 if (teamsPerSession != other.teamsPerSession)
225 return false;
226 return true;
227 }
228
229 /**
230 *
231 * @param partyproflist a list of {@link PartyWithProfile} settings
232 * @return {@link SessionSettings}
233 */
234 protected SessionSettings createSetting(
235 ImmutableList<TeamInfo> partyproflist) {
236 SessionSettings settings = sessionsettings;
237 for (TeamInfo partyprof : partyproflist) {
238 settings = settings.with(partyprof);
239 }
240 return settings;
241 }
242
243 /**
244 *
245 * @param parties the parties in the tournament
246 * @param profiles the profiles in the tournament
247 * @param n number of items to draw. Assumed &ge;1.
248 * @param drawPartyWithPutback if parties can be drawn multiple times
249 * @return list of sessionsets where each sessionset contains settings for a
250 * session: a list with {@link TeamInfo}s. The sessionsets are made
251 * by making all combinations of parties and profiles. Profiles are
252 * drawn with replace.
253 */
254 protected ImmutableList<ImmutableList<TeamInfo>> getParticipantsOneTournament(
255 ImmutableList<Team> parties, ImmutableList<ProfileList> profiles,
256 int n, boolean drawPartyWithPutback) {
257
258 Permutations<Team> partiesPermutations;
259 if (drawPartyWithPutback) {
260 partiesPermutations = new PermutationsWithReturn<>(parties, n);
261 } else {
262 partiesPermutations = new PermutationsWithoutReturn<>(parties, n);
263 }
264
265 Permutations<ProfileList> profilesPermutations = new PermutationsOrderedWithoutReturn<>(
266 profiles, n);
267
268 // each tuple contains session info: a list of Team and a list of
269 // ProfileList.
270 Tuples<ImmutableList<Team>, ImmutableList<ProfileList>> tuples = new Tuples<>(
271 partiesPermutations, profilesPermutations);
272
273 return new MapList<Tuple<ImmutableList<Team>, ImmutableList<ProfileList>>, ImmutableList<TeamInfo>>(
274 tuple -> teamlist(tuple), tuples);
275 }
276
277 /**
278 *
279 * @param tuple a tuple with (1) ImmutableList<Team> (2)
280 * ImmutableList<Profiles>
281 * @return list of TeamOfPartiesAndProfiles, each picked from the two lists
282 * in order.
283 */
284 private ImmutableList<TeamInfo> teamlist(
285 Tuple<ImmutableList<Team>, ImmutableList<ProfileList>> tuple) {
286 BiFunction<Team, ProfileList, TeamInfo> makeparty = new BiFunction<Team, ProfileList, TeamInfo>() {
287 @Override
288 public TeamInfo apply(Team team, ProfileList profilelist) {
289 List<PartyWithParameters> parties = team.getParties();
290 List<ProfileRef> profiles = profilelist.getProfiles();
291 if (parties.size() != profiles.size())
292 throw new IllegalArgumentException(
293 "Parties and profiles list must match, but found "
294 + team + " and " + profilelist);
295
296 List<PartyWithProfile> theteams = new LinkedList<>();
297 for (int n = 0; n < parties.size(); n++) {
298 theteams.add(new PartyWithProfile(parties.get(n),
299 profiles.get(n)));
300 }
301 return new TeamInfo(theteams);
302 }
303
304 };
305 return new MapThreadList<TeamInfo, Team, ProfileList>(makeparty,
306 tuple.get1(), tuple.get2());
307 }
308
309}
Note: See TracBrowser for help on using the repository browser.