source: src/main/java/genius/gui/session/SessionPanel.java@ 209

Last change on this file since 209 was 166, checked in by Tim Baarslag, 6 years ago

RLBOA agents can listen to sessions only when training with access to partner preferences

Refined and renamed an example of a party that deals with preference uncertainty by defining a custom UtilitySpace based on the closest known bid.

File size: 8.9 KB
Line 
1package genius.gui.session;
2
3import java.util.List;
4
5import java.util.ArrayList;
6import java.util.Date;
7import java.util.concurrent.ExecutionException;
8import java.util.concurrent.TimeoutException;
9
10import java.awt.BorderLayout;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.text.DateFormat;
14import java.text.SimpleDateFormat;
15
16import javax.swing.JFrame;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19import javax.xml.stream.XMLStreamException;
20
21import genius.core.AgentID;
22import genius.core.exceptions.InstantiateException;
23import genius.core.exceptions.NegotiatorException;
24import genius.core.listener.Listener;
25import genius.core.logging.ConsoleLogger;
26import genius.core.logging.FileLogger;
27import genius.core.logging.XmlLogger;
28import genius.core.misc.RLBOAUtils;
29import genius.core.parties.NegotiationParty;
30import genius.core.parties.NegotiationPartyInternal;
31import genius.core.parties.SessionsInfo;
32import genius.core.protocol.MultilateralProtocol;
33import genius.core.repository.ParticipantRepItem;
34import genius.core.repository.ProfileRepItem;
35import genius.core.session.ExecutorWithTimeout;
36import genius.core.session.MultilateralSessionConfiguration;
37import genius.core.session.Participant;
38import genius.core.session.RepositoryException;
39import genius.core.session.Session;
40import genius.core.session.SessionConfiguration;
41import genius.core.session.SessionManager;
42import genius.core.session.TournamentManager;
43import genius.core.tournament.TournamentConfiguration;
44import genius.gui.progress.session.ActionDocumentModel;
45import genius.gui.progress.session.OutcomesListModel;
46import genius.gui.progress.session.SessionProgressUI;
47
48/**
49 * Session Panel. Asks user to configure a session. When user presses run, the
50 * panel changes into a progress panel and a session runner is started.
51 */
52@SuppressWarnings("serial")
53public class SessionPanel extends JPanel {
54 public SessionPanel() {
55 final SessionModel model = new SessionModel();
56
57 setLayout(new BorderLayout());
58 add(new SessionConfigPanel(model), BorderLayout.CENTER);
59 model.addListener(new Listener<MultilateralSessionConfiguration>() {
60 @Override
61 public void notifyChange(final MultilateralSessionConfiguration config) {
62 new Thread(new Runnable() {
63 @Override
64 public void run() {
65 boolean showChart = model.getShowChart().getValue();
66 boolean biChart = model.getParticipantsModel().getSize() == 2
67 && model.getBilateralUtilUtilPlot().getValue();
68 runSession(config, showChart, biChart, model.getBilateralShowAllBids().getValue(),
69 model.getPrintEnabled().getValue());
70 }
71 }).start();
72 }
73
74 });
75
76 }
77
78 /**
79 * Runs a session and waits for completion.
80 *
81 * @param config
82 * @param showChart
83 * true iff a progress chart should be shown.
84 * @param useBiChart
85 * true iff the bilateral progress chart is to be used.
86 * @param showAllBids
87 */
88 private void runSession(MultilateralSessionConfiguration config, boolean showChart, boolean useBiChart,
89 boolean showAllBids, boolean isPrintEnabled) {
90 System.out.println("run session, with " + config);
91 try {
92 start(config, showChart, useBiChart, showAllBids, isPrintEnabled);
93 } catch (InstantiateException | RepositoryException | NegotiatorException | XMLStreamException | IOException
94 | TimeoutException | ExecutionException e) {
95 e.printStackTrace();
96 JOptionPane.showMessageDialog(null, "Session failed to run: " + e.getMessage(), "Warning",
97 JOptionPane.WARNING_MESSAGE);
98 }
99
100 }
101
102 /**
103 *
104 * @param config
105 * @param showChart
106 * @param showBiChart
107 * true if progress chart has to be shown
108 * @param showAllBids
109 * if the bilateral progress chart should be used. Ignored if
110 * showBiChart is false.
111 * @param isPrintEnabled
112 * true iff system out print is enabled.
113 * @throws InstantiateException
114 * @throws RepositoryException
115 * @throws NegotiatorException
116 * @throws XMLStreamException
117 * @throws IOException
118 * @throws TimeoutException
119 * @throws ExecutionException
120 */
121 public void start(MultilateralSessionConfiguration config, boolean showChart, boolean showBiChart,
122 boolean showAllBids, boolean isPrintEnabled) throws InstantiateException, RepositoryException,
123 NegotiatorException, XMLStreamException, IOException, TimeoutException, ExecutionException {
124
125 if (config.getParties().size() < 2) {
126 throw new IllegalArgumentException("There should be at least two negotiating agents !");
127 }
128
129 MultilateralProtocol protocol = TournamentManager.getProtocol(config.getProtocol());
130 SessionsInfo info = new SessionsInfo(protocol, config.getPersistentDataType(), isPrintEnabled);
131 Session session = new Session(config.getDeadline(), info);
132
133 ExecutorWithTimeout executor = new ExecutorWithTimeout(1000 * config.getDeadline().getTimeOrDefaultTimeout());
134 List<NegotiationPartyInternal> negoparties = getNegotiationParties(config, session, info, executor);
135 SessionManager sessionManager = new SessionManager((SessionConfiguration) config, negoparties, session,
136 executor);
137
138 // When training, the RLBOA agents can listen to the sessions
139 if (TournamentConfiguration.getBooleanOption("accessPartnerPreferences", false))
140 RLBOAUtils.addReinforcementAgentListeners(negoparties, sessionManager);
141
142 displayProgress(negoparties, sessionManager, showChart, showBiChart, showAllBids);
143
144 // connect the loggers.
145 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
146 String fileName = String.format("log/Log-Session_%s", dateFormat.format(new Date()));
147 FileLogger filelogger = new FileLogger(fileName);
148 XmlLogger xmlLogger = new XmlLogger(new FileOutputStream(fileName + ".xml"), "Session");
149 sessionManager.addListener(filelogger);
150 sessionManager.addListener(xmlLogger);
151 sessionManager.addListener(new ConsoleLogger());
152
153 System.out.println("Negotiation session has started.");
154 Thread t = new Thread(sessionManager);
155 t.start();
156 try {
157 t.join();
158 } catch (InterruptedException e) {
159 e.printStackTrace();
160 }
161
162 // close the loggers
163 System.out.println("Negotiation session has stopped.");
164 try {
165 filelogger.close();
166 } catch (IOException e) {
167 e.printStackTrace();
168 }
169 try {
170 xmlLogger.close();
171 } catch (IOException e) {
172 e.printStackTrace();
173 }
174 info.close();
175
176 }
177
178 /**
179 *
180 * @param negoparties
181 * @param sessionManager
182 * @param showChart
183 * true iff any progress chart has to be shown
184 * @param biChart
185 * if the bilateral progress chart has to be shown. Ignored if
186 * showChart is false.
187 * @param showAllBids
188 */
189 private void displayProgress(List<NegotiationPartyInternal> negoparties, SessionManager sessionManager,
190 boolean showChart, boolean biChart, boolean showAllBids) {
191 OutcomesListModel model = new OutcomesListModel(negoparties);
192 ActionDocumentModel actiondocument = new ActionDocumentModel();
193 sessionManager.addListener(model);
194 sessionManager.addListener(actiondocument);
195 removeAll();
196 add(new SessionProgressUI(model, actiondocument, showChart, biChart, showAllBids), BorderLayout.CENTER);
197 revalidate();
198 }
199
200 /**
201 *
202 * @param config
203 * @param session
204 * @param info
205 * @param executor
206 * the executor in which this session runs.
207 * @return the parties for this negotiation. Converts the config into actual
208 * {@link NegotiationParty}s
209 * @throws RepositoryException
210 * @throws NegotiatorException
211 * @throws ExecutionException
212 * @throws TimeoutException
213 */
214 private List<NegotiationPartyInternal> getNegotiationParties(MultilateralSessionConfiguration config,
215 Session session, SessionsInfo info, ExecutorWithTimeout executor)
216 throws RepositoryException, NegotiatorException, TimeoutException, ExecutionException {
217 List<ParticipantRepItem> parties = new ArrayList<>();
218 List<ProfileRepItem> profiles = new ArrayList<>();
219 List<AgentID> names = new ArrayList<AgentID>();
220
221 for (Participant participant : config.getParties()) {
222 ParticipantRepItem strategy = participant.getStrategy();
223 parties.add(strategy);
224 if (!strategy.isMediator()) {
225 profiles.add(participant.getProfile());
226 names.add(participant.getId());
227 }
228 }
229
230 return TournamentManager.getPartyList(executor, config, info, session);
231 // List<NegotiationPartyInternal> negoparties =
232 // TournamentGenerator.generateSessionParties(parties, profiles,
233 // names, session, info);
234 // return negoparties;
235 }
236
237 private List<NegotiationPartyInternal> getNonMediators(List<NegotiationPartyInternal> negoparties) {
238 List<NegotiationPartyInternal> list = new ArrayList<>();
239 for (NegotiationPartyInternal party : negoparties) {
240 if (!party.isMediator()) {
241 list.add(party);
242 }
243 }
244 return list;
245 }
246
247 /**
248 * simple stub to run this stand-alone (for testing).
249 *
250 * @param args
251 */
252 public static void main(String[] args) {
253 final JFrame gui = new JFrame();
254 gui.setLayout(new BorderLayout());
255 gui.getContentPane().add(new SessionPanel(), BorderLayout.CENTER);
256 gui.pack();
257 gui.setVisible(true);
258 }
259
260}
Note: See TracBrowser for help on using the repository browser.