source: protocol/src/main/java/geniusweb/protocol/tournament/allpermutationslearn/AllPermutationsLearnSettings.java@ 39

Last change on this file since 39 was 39, checked in by bart, 3 years ago

Geniusweb 2.0.3. MOPAC protocol translated to Python. Small fixes.

File size: 10.8 KB
Line 
1package geniusweb.protocol.tournament.allpermutationslearn;
2
3import java.math.BigInteger;
4import java.net.URISyntaxException;
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Collections;
8import java.util.List;
9import java.util.stream.Collectors;
10
11import com.fasterxml.jackson.annotation.JsonCreator;
12import com.fasterxml.jackson.annotation.JsonProperty;
13import com.fasterxml.jackson.databind.ObjectMapper;
14
15import geniusweb.actions.FileLocation;
16import geniusweb.protocol.session.SessionSettings;
17import geniusweb.protocol.session.TeamInfo;
18import geniusweb.protocol.session.learn.LearnSettings;
19import geniusweb.protocol.tournament.ProfileList;
20import geniusweb.protocol.tournament.Team;
21import geniusweb.protocol.tournament.TournamentProtocol;
22import geniusweb.protocol.tournament.allpermutations.AllPermutationsSettings;
23import geniusweb.references.Parameters;
24import geniusweb.references.PartyWithProfile;
25import geniusweb.references.ProfileRef;
26import tudelft.utilities.immutablelist.FixedList;
27import tudelft.utilities.immutablelist.ImmutableList;
28import tudelft.utilities.immutablelist.JoinedList;
29import tudelft.utilities.immutablelist.MapThreadList;
30import tudelft.utilities.logging.Reporter;
31
32/**
33 * Almost the same as AllPermutationsSettings.
34 * <p>
35 * The only additional field is the additional learnTime. This has a maximum to
36 * the maximum number of sessions, {@link #MAX_SESSIONS}. This is because we
37 * need to store all the {@link FileLocation}s explicitly.
38 * <p>
39 * Only the first team member is actually getting the learn time.
40 * <p>
41 * We assume that all parties have the proper persistentstate parameter already.
42 * This is assumed to be set in the {@link Team} settings
43 */
44public class AllPermutationsLearnSettings extends AllPermutationsSettings {
45
46 /**
47 * MAX_SESSIONS is the maximum number of sessions in a tournament. It is
48 * depending on a number of factors:
49 * <ul>
50 * <li>The memory use to store all UUIDs for all parties
51 * <li>The MAX_MESSAGE_SIZE of a websocket message.
52 * </ul>
53 * The MAX_MESSAGE_SIZE of the partyserver currently is 20MB. A single UUID
54 * is about 45 bytes including double quotes and commas
55 * </ul>
56 */
57 private final static int MAX_SESSIONS = 400000;
58 private final static ObjectMapper jackson = new ObjectMapper();
59
60 private final LearnSettings learnSettings;
61
62 /**
63 *
64 * @param teams a list of {@link Team}s. Must contain at least
65 * {@link #teamsPerSession} elements. The
66 * {@link #teamsPerSession} must match the protocol:
67 * (SAOP:1, SHAOP:2)
68 * @param reuseTeams if true, we use PermutationsWithReturn, if false
69 * we use PermutationsWithoutReturn to create the
70 * teams.
71 * @param plists list of available {@link ProfileList}s to be
72 * permutated over the teams. Each
73 * {@link ProfileList} must contain
74 * {@link #teamsPerSession} elements.
75 * @param teamsPerSession number of parties per session, must be at least 2.
76 * @param sesettings The generic {@link SessionSettings}.
77 * {@link SessionSettings#with(TeamInfo)} will be
78 * used to add the required {@link TeamInfo}s. All
79 * settings must contain a 'persistentstate'
80 * parameter with a {@link FileLocation}.
81 * @param nTournaments the number of times the tournament should be run.
82 * @param learnSet the {@link LearnSettings} to be used for the
83 * learning phase. The participants list should be
84 * empty as this will be filled up from the teams
85 * provided above. Only the deadline is used.
86 */
87 @JsonCreator
88 public AllPermutationsLearnSettings(@JsonProperty("teams") List<Team> teams,
89 @JsonProperty("profileslists") List<ProfileList> plists,
90 @JsonProperty("reuseTeams") boolean reuseTeams,
91 @JsonProperty("teamsPerSession") int teamsPerSession,
92 @JsonProperty("sessionsettings") SessionSettings sesettings,
93 @JsonProperty("numberTournaments") int nTournaments,
94 @JsonProperty("learnSettings") LearnSettings learnSet) {
95 super(teams, plists, reuseTeams, teamsPerSession, sesettings,
96 nTournaments);
97 this.learnSettings = learnSet;
98 }
99
100 @Override
101 public TournamentProtocol getProtocol(Reporter logger) {
102 AllPermutationsLearnState state = new AllPermutationsLearnState(this,
103 permutations(), Collections.emptyList());
104 return new AllPermutationsLearnProtocol(state, logger);
105 }
106
107 @Override
108 public Double getMaxRunTime() {
109 return super.permutations().size().doubleValue()
110 * (sessionsettings.getMaxRunTime() + 2d)
111 + learnSettings.getMaxRunTime();
112 }
113
114 /**
115 * @return {@link ImmutableList} containing all {@link SessionSettings} for
116 * all Sessions in the correct order.
117 */
118 @Override
119 public ImmutableList<SessionSettings> permutations() {
120
121 ImmutableList<ImmutableList<TeamInfo>> partylistlist;
122 ImmutableList<Team> partieslist = new FixedList<Team>(teams);
123 ImmutableList<ProfileList> profileslist = new FixedList<ProfileList>(
124 profileslists);
125 partylistlist = getParticipantsOneTournament(partieslist, profileslist,
126 teamsPerSession, reuseTeams);
127
128 if (partylistlist.size().multiply(BigInteger.valueOf(numberTournaments))
129 .compareTo(BigInteger.valueOf(MAX_SESSIONS)) > 0)
130 throw new IllegalArgumentException(
131 "learn-tournament exceeds max number of sessions "
132 + MAX_SESSIONS);
133
134 // we have to generate separate filelocations for each session...
135 ImmutableList<SessionSettings> allSettings = null;
136 for (int tournamentnr = 0; tournamentnr < getNrTournaments(); tournamentnr++) {
137 FixedList<FixedList<FileLocation>> filelocs = createFileLocations(
138 partylistlist.size().longValue());
139 ImmutableList<SessionSettings> settings = new MapThreadList<>(
140 (parties, flocs) -> createSetting(parties, flocs),
141 partylistlist, filelocs);
142 settings = new JoinedList<SessionSettings>(settings,
143 getLearnSessions(filelocs, partylistlist));
144 allSettings = allSettings == null ? settings
145 : new JoinedList<SessionSettings>(allSettings, settings);
146 }
147
148 return allSettings;
149 }
150
151 @Override
152 public String toString() {
153 return "AllPermutationsLearnSettings[" + contentString() + ","
154 + learnSettings + "]";
155 }
156
157 /**
158 *
159 * @param numsessions the number of sessions in the tournament
160 * @return A list of list of {@link FileLocation}s. The outer list has one
161 * entry for each session, the inner list has one
162 * {@link FileLocation} per party.
163 */
164 private FixedList<FixedList<FileLocation>> createFileLocations(
165 long numsessions) {
166 // like before, but all sessions need a new negotiationdata
167 List<FixedList<FileLocation>> filelocations = new ArrayList<>();
168 for (long sessionnr = 0; sessionnr < numsessions; sessionnr++) {
169 List<FileLocation> locs = new ArrayList<>();
170 for (int teamnr = 0; teamnr < teamsPerSession; teamnr++) {
171 locs.add(new FileLocation());
172 }
173 filelocations.add(new FixedList<>(locs));
174 }
175 FixedList<FixedList<FileLocation>> filelocs = new FixedList<>(
176 filelocations);
177 return filelocs;
178 }
179
180 /**
181 * @param allsessionsettings the 'traditional session settings.
182 * @param filelocs the {@link FileLocation}s for each session, for
183 * each party
184 * @param partylistlist the lists of all teams for the sessions, with
185 * unaltered parameters matching the
186 * {@link #teams} list
187 * @return the final sessions, doing learning for all parties. In this
188 * session, the number of parties may be larger than
189 * {@link #teamsPerSession}. Learning may be spread over multiple
190 * sessions, although we don't do this currently.
191 */
192 private ImmutableList<SessionSettings> getLearnSessions(
193 FixedList<FixedList<FileLocation>> filelocs,
194 ImmutableList<ImmutableList<TeamInfo>> partylistlist) {
195 try {
196 ProfileRef NOPROFILE = new ProfileRef("not://aprofile");
197
198 SessionSettings session = learnSettings;
199 // add all the teams, with updated parameters, to the session
200 for (Team team : teams) {
201 List<FileLocation> files = getNegoData(team, filelocs,
202 partylistlist);
203 // Tricky: correct parameter is list of UUID strings
204 List<String> filesstrings = files.stream()
205 .map(file -> file.getUUIDString())
206 .collect(Collectors.toList());
207 Parameters parameters = new Parameters().with("negotiationdata",
208 filesstrings);
209 // add all the FileLocations and NOPROFILE to all team members
210 List<PartyWithProfile> teaminfo = team.getParties().stream()
211 .map(pwp -> new PartyWithProfile(pwp.with(parameters),
212 NOPROFILE))
213 .collect(Collectors.toList());
214 session = session.with(new TeamInfo(teaminfo));
215 }
216
217 return new FixedList<SessionSettings>(Arrays.asList(session));
218 } catch (URISyntaxException e) {
219 throw new RuntimeException("Failed creating learn session", e);
220 }
221 }
222
223 /**
224 * @param team the team for which the {@link FileLocation}s are needed
225 * @param filelocs the used {@link FileLocation}s for all sessions, all
226 * teams.
227 * @param TeamInfo the TeamInfo used for each session, team. Same dimensions
228 * as filelocs
229 * @return the list of FileLocations that were provided to the given team in
230 * the negotiation sessions.
231 */
232 private List<FileLocation> getNegoData(Team team,
233 FixedList<FixedList<FileLocation>> filelocs,
234 ImmutableList<ImmutableList<TeamInfo>> partylistlist) {
235 List<FileLocation> locs = new ArrayList<>();
236 for (long sessionnr = 0; sessionnr < filelocs.size()
237 .longValue(); sessionnr++) {
238 for (int teamnr = 0; teamnr < teamsPerSession; teamnr++) {
239 if (team.equals(
240 partylistlist.get(sessionnr).get(teamnr).getTeam()))
241 locs.add(filelocs.get(sessionnr).get(teamnr));
242 }
243 }
244 return locs;
245
246 }
247
248 /**
249 *
250 * @param partyproflist a list of {@link PartyWithProfile} settings. Now,
251 * each party gets a new negotiationdata object in
252 * every session.
253 * @param locs the FileLocations for the negotiationdata for all
254 * teams
255 * @return {@link SessionSettings}, with each party parameters extended with
256 * its {@link FileLocation}
257 */
258 private SessionSettings createSetting(ImmutableList<TeamInfo> partyproflist,
259 ImmutableList<FileLocation> locs) {
260 SessionSettings settings = sessionsettings;
261 for (int teamnr = 0; teamnr < partyproflist.size()
262 .intValue(); teamnr++) {
263 Parameters extraparams = new Parameters().with("negotiationdata",
264 Arrays.asList(locs.get(teamnr)));
265 TeamInfo teaminfo = partyproflist.get(teamnr).with(extraparams);
266 settings = settings.with(teaminfo);
267 }
268 return settings;
269 }
270
271}
Note: See TracBrowser for help on using the repository browser.