source: src/main/java/negotiator/parties/EnterBidDialogExtended.java@ 48

Last change on this file since 48 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 20.1 KB
Line 
1/*
2 * EnterBidDialog.java
3 *
4 * Created on November 16, 2006, 10:18 AM
5 */
6
7package negotiator.parties;
8
9import java.awt.BorderLayout;
10import java.awt.Color;
11import java.awt.Component;
12import java.awt.Container;
13import java.awt.Dimension;
14import java.awt.FlowLayout;
15import java.awt.GridBagConstraints;
16import java.awt.GridBagLayout;
17import java.awt.Insets;
18import java.awt.event.ActionEvent;
19import java.text.DecimalFormat;
20
21import javax.swing.BorderFactory;
22import javax.swing.BoxLayout;
23import javax.swing.DefaultCellEditor;
24import javax.swing.JButton;
25import javax.swing.JDialog;
26import javax.swing.JLabel;
27import javax.swing.JOptionPane;
28import javax.swing.JPanel;
29import javax.swing.JProgressBar;
30import javax.swing.JTable;
31import javax.swing.JTextArea;
32import javax.swing.JTextField;
33import javax.swing.border.Border;
34import javax.swing.border.TitledBorder;
35import javax.swing.table.AbstractTableModel;
36import javax.swing.table.TableCellRenderer;
37
38import org.jfree.chart.ChartPanel;
39import org.jfree.chart.JFreeChart;
40
41import genius.core.Bid;
42import genius.core.actions.Accept;
43import genius.core.actions.EndNegotiation;
44import genius.core.actions.Offer;
45import genius.core.exceptions.Warning;
46import genius.core.issue.Value;
47import genius.core.utility.AdditiveUtilitySpace;
48import genius.core.utility.Evaluator;
49import genius.gui.chart.UtilityPlot;
50
51/**
52 * This dialog works based on the {@link Evaluator}s and therefore only can be
53 * used with {@link AdditiveUtilitySpace}. TODO can we lift this requirement?
54 *
55 * @author W.Pasman
56 */
57@SuppressWarnings("serial")
58public class EnterBidDialogExtended extends JDialog {
59
60 private NegoInfo negoinfo; // the table model
61 private genius.core.actions.Action selectedAction;
62 private UIAgentExtended agent;
63 private JTextArea negotiationMessages = new JTextArea("NO MESSAGES YET");
64 // Wouter: we have some whitespace in the buttons,
65 // that makes nicer buttons and also artificially increases the window size.
66 private JButton buttonAccept = new JButton(" Accept Opponent Bid ");
67 private JButton buttonEnd = new JButton("End Negotiation");
68 private JButton buttonBid = new JButton(" Do Bid ");
69 private JPanel buttonPanel = new JPanel();
70 private JTable BidTable;
71
72 // alinas variables
73 private JTable BidHistoryTable;
74 private HistoryInfo historyinfo; // the table model
75 private ChartPanel chartPanel;
76 private JPanel defaultChartPanel;
77 // private ScatterPlot plot;
78 private UtilityPlot plot;
79 private Bid lastOppBid;
80
81 /**
82 *
83 * @param agent
84 * @param parent
85 * @param modal
86 * @param us
87 * @param lastOppBid
88 * last oppponent bid that can be accepted, or null if no such
89 * bid.
90 * @throws Exception
91 */
92 public EnterBidDialogExtended(UIAgentExtended agent, java.awt.Frame parent,
93 boolean modal, AdditiveUtilitySpace us, Bid lastOppBid)
94 throws Exception {
95 super(parent, modal);
96 this.agent = agent;
97 this.lastOppBid = lastOppBid;
98 negoinfo = new NegoInfo(null, null, us);
99 historyinfo = new HistoryInfo(agent, null, null, us);
100 initThePanel();
101 }
102
103 // quick hack.. we can't refer to the Agent's utilitySpace because
104 // the field is protected and there is no getUtilitySpace function either.
105 // therefore the Agent has to inform us when utilspace changes.
106 public void setUtilitySpace(AdditiveUtilitySpace us) {
107 negoinfo.utilitySpace = us;
108 historyinfo.utilitySpace = us;
109 }
110
111 private void initThePanel() {
112 if (negoinfo == null)
113 throw new NullPointerException("negoinfo is null");
114 Container pane = getContentPane();
115 // gridbag layout:
116 GridBagLayout gridbag = new GridBagLayout();
117 GridBagConstraints c = new GridBagConstraints();
118
119 pane.setLayout(gridbag);
120
121 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
122 setTitle("Choose action for agent " + agent.getName());
123 // setSize(new java.awt.Dimension(600, 400));
124 // setBounds(0,0,640,480);
125
126 c.gridx = 0;
127 c.gridy = 1;
128 c.gridwidth = 2;
129 c.fill = GridBagConstraints.HORIZONTAL;
130 c.insets = new Insets(0, 10, 0, 10);
131 // createFrom panel for history of bids
132 BidHistoryTable = new JTable(historyinfo);
133 BidHistoryTable.setGridColor(Color.lightGray);
134 // setting the columns that contain numbers to a small width:
135 BidHistoryTable.getColumnModel().getColumn(0).setMaxWidth(50);
136 BidHistoryTable.getColumnModel().getColumn(2).setMaxWidth(50);
137 BidHistoryTable.getColumnModel().getColumn(4).setMaxWidth(50);
138 JPanel tablepaneHistory = new JPanel(new BorderLayout());
139 tablepaneHistory.add(BidHistoryTable.getTableHeader(), "North");
140 tablepaneHistory.add(BidHistoryTable, "Center");
141 Border blackline = BorderFactory.createLineBorder(Color.black);
142 TitledBorder title = BorderFactory.createTitledBorder(blackline,
143 "History of Bids:");
144 // for having the title in the center
145 // title.setTitleJustification(TitledBorder.CENTER);
146 tablepaneHistory.setBorder(title);
147 pane.add(tablepaneHistory, c);
148
149 c.gridwidth = 1;
150 c.gridheight = 4;
151 c.gridx = 0;
152 c.gridy = 2;
153 c.insets = new Insets(10, 10, 10, 10);
154 // adding the chart
155 defaultChartPanel = new JPanel();
156 title = BorderFactory.createTitledBorder(blackline,
157 "Utilities of Bids per round:");
158
159 defaultChartPanel.setBorder(title);
160 pane.remove(defaultChartPanel);
161 pane.add(defaultChartPanel, c);
162
163 // user input:
164 JPanel userInputPanel = new JPanel();
165 userInputPanel
166 .setLayout(new BoxLayout(userInputPanel, BoxLayout.Y_AXIS));
167 title = BorderFactory.createTitledBorder(blackline,
168 "Please place your bid:");
169
170 userInputPanel.setBorder(title);
171 negotiationMessages.setBackground(Color.lightGray);
172 negotiationMessages.setEditable(false);
173 userInputPanel.add(negotiationMessages);
174
175 // createFrom center panel: the bid table
176 BidTable = new JTable(negoinfo);
177 // BidTable.setModel(negoinfo); // need a model for column size etc...
178 // Why doesn't this work???
179 BidTable.setGridColor(Color.lightGray);
180 BidTable.setRowHeight(18);
181 JPanel tablepane = new JPanel(new BorderLayout());
182 tablepane.add(BidTable.getTableHeader(), "North");
183 tablepane.add(BidTable, "Center");
184 userInputPanel.add(tablepane);
185 buttonAccept.setEnabled(lastOppBid != null);
186 // createFrom the buttons:
187 buttonPanel.setLayout(new FlowLayout());
188 buttonPanel.add(buttonEnd);
189 buttonPanel.add(buttonAccept);
190 buttonPanel.add(buttonBid);
191 userInputPanel.add(buttonPanel);
192
193 c.gridwidth = 1;
194 c.gridheight = 1;
195 c.gridx = 1;
196 c.gridy = 3;
197 c.weighty = 0;
198 c.fill = GridBagConstraints.HORIZONTAL;
199 c.insets = new Insets(10, 10, 10, 10);
200 pane.add(userInputPanel, c);
201 buttonBid.setSelected(true);
202
203 // set action listeners for the buttons
204 buttonBid.addActionListener(new java.awt.event.ActionListener() {
205 public void actionPerformed(java.awt.event.ActionEvent evt) {
206 buttonBidActionPerformed(evt);
207 }
208 });
209 // buttonSkip.addActionListener(new java.awt.event.ActionListener() {
210 // public void actionPerformed(java.awt.event.ActionEvent evt) {
211 // buttonSkipActionPerformed(evt);
212 // }
213 // });
214 buttonEnd.addActionListener(new java.awt.event.ActionListener() {
215 public void actionPerformed(java.awt.event.ActionEvent evt) {
216 buttonEndActionPerformed(evt);
217 }
218 });
219 buttonAccept.addActionListener(new java.awt.event.ActionListener() {
220 public void actionPerformed(java.awt.event.ActionEvent evt) {
221 buttonAcceptActionPerformed(evt);
222 }
223 });
224 pack(); // pack will do complete layout, getting all cells etc.
225 }
226
227 private Bid getBid() {
228 Bid bid = null;
229 try {
230 bid = negoinfo.getBid();
231 } catch (Exception e) {
232 JOptionPane.showMessageDialog(null,
233 "There is a problem with your bid: " + e.getMessage());
234 }
235 return bid;
236 }
237
238 private void buttonBidActionPerformed(java.awt.event.ActionEvent evt) {
239
240 Bid bid = getBid();
241 if (bid != null) {
242 selectedAction = new Offer(agent.getAgentID(), bid);
243 setVisible(false);
244 }
245 }
246
247 private void buttonAcceptActionPerformed(java.awt.event.ActionEvent evt) {
248 Bid bid = getBid();
249 if (bid != null) {
250 System.out.println("Accept performed");
251 selectedAction = new Accept(agent.getAgentID(), lastOppBid);
252 setVisible(false);
253 }
254 }
255
256 private void buttonEndActionPerformed(java.awt.event.ActionEvent evt) {
257 System.out.println("End Negotiation performed");
258 selectedAction = new EndNegotiation(agent.getAgentID());
259 setVisible(false);
260 }
261
262 /**
263 * This is called by UIAgent repeatedly, to ask for next action.
264 *
265 * @param opponentAction
266 * is action done by opponent
267 * @param myPreviousBid
268 * @return our next negotionat action.
269 */
270 public genius.core.actions.Action askUserForAction(
271 genius.core.actions.Action opponentAction, Bid myPreviousBid) {
272 historyinfo.nrOfBids = agent.historyOfBids.size();
273 negoinfo.lastAccepted = null;
274
275 if (opponentAction == null) {
276 negotiationMessages.setText("Opponent did not send any action.");
277 }
278 if (opponentAction instanceof Accept) {
279 negotiationMessages.setText("Opponent accepted your last bid!");
280 negoinfo.lastAccepted = myPreviousBid;
281
282 }
283 if (opponentAction instanceof EndNegotiation) {
284 negotiationMessages.setText("Opponent cancels the negotiation.");
285 }
286 if (opponentAction instanceof Offer) {
287 negotiationMessages.setText("Opponent proposes the following bid:");
288 negoinfo.lastAccepted = ((Offer) opponentAction).getBid();
289 }
290 try {
291 negoinfo.setOurBid(myPreviousBid);
292 } catch (Exception e) {
293 System.out.println("error in askUserForAction:" + e.getMessage());
294 e.printStackTrace();
295 }
296
297 BidTable.setDefaultRenderer(BidTable.getColumnClass(0),
298 new MyCellRenderer(negoinfo));
299 BidHistoryTable.setDefaultRenderer(BidHistoryTable.getColumnClass(0),
300 new MyHistoryCellRenderer(historyinfo));
301 BidHistoryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // needs some
302 // fixing so
303 // that the
304 // bids are
305 // visible
306 // properly
307 BidTable.setDefaultEditor(BidTable.getColumnClass(0), new MyCellEditor(
308 negoinfo));
309
310 int round = agent.bidCounter;
311 System.out
312 .println("round# " + round + "/" + agent.historyOfBids.size());
313
314 // createFrom a new plot of the bid utilities for each round
315 double[][] myBidSeries = new double[2][round];
316 double[][] oppBidSeries = new double[2][round];
317
318 if (round > 0) {
319 System.out.println(agent.historyOfBids.get(0));
320 for (int i = 0; i < round; i++) {
321 try {
322 System.out.println("i " + i);
323 Bid oppBid = agent.historyOfBids.get(i).getOppentBid();
324 Bid ourBid = agent.historyOfBids.get(i).getOurBid();
325 double utilOpp = 0;
326 double ourUtil = 0;
327
328 if (agent.utilitySpace != null) {
329 if (oppBid != null)
330 utilOpp = agent.utilitySpace.getUtility(oppBid);
331 ourUtil = agent.utilitySpace.getUtility(ourBid);
332 } else {
333 System.out.println("agent.utilSpace=null");
334 }
335
336 myBidSeries[0][i] = i + 1;
337 myBidSeries[1][i] = ourUtil;
338
339 oppBidSeries[0][i] = i + 1;
340 oppBidSeries[1][i] = utilOpp;
341
342 } catch (Exception e) {
343 e.printStackTrace();
344 }
345 }
346 }
347
348 // if there is a chart already, remove and draw new one
349 if (defaultChartPanel.getComponents().length > 0)
350 defaultChartPanel.remove(chartPanel);
351 // plot = new ScatterPlot(myBidSeries, oppBidSeries);
352 plot = new UtilityPlot(myBidSeries, oppBidSeries);
353 JFreeChart chart = plot.getChart();
354 chartPanel = new ChartPanel(chart);
355 chartPanel.setPreferredSize(new Dimension(350, 350));
356 defaultChartPanel.add(chartPanel);
357
358 pack();
359 repaint();
360 setVisible(true); // this returns only after the panel closes.
361 negoinfo.comboBoxes.get(0).requestFocusInWindow();
362 return selectedAction;
363 }
364}
365
366/********************************************************/
367
368/**
369 * @author Alina HistoryInfo is the class that contains the former bids and
370 * fills the JTable for the history with it.
371 */
372@SuppressWarnings("serial")
373class HistoryInfo extends AbstractTableModel {
374 public Bid ourOldBid;
375 public Bid oppOldBid;
376 public int nrOfBids = 0;
377 private UIAgentExtended agent;
378 private String[] colNames = { "Round", "Bid of your Opponent", "u(opp)",
379 "Your Bid", "u(own)" };
380
381 public String getColumnName(int col) {
382 return colNames[col];
383 }
384
385 public AdditiveUtilitySpace utilitySpace;
386
387 HistoryInfo(UIAgentExtended agent, Bid our, Bid opponent,
388 AdditiveUtilitySpace us) throws Exception {
389 this.agent = agent;
390 utilitySpace = us;
391 }
392
393 public int getColumnCount() {
394 return 5;
395 }
396
397 public int getRowCount() {
398 return 10; // needs to be dynamically changed, right now we will have a
399 // problem when there are more than 10 rounds
400 }
401
402 public Component getValueAt(int row, int col) {
403
404 if (nrOfBids != 0 && row < nrOfBids) {
405 // get the bids for the row-th round:
406 Bid oppBid = agent.historyOfBids.get(row).getOppentBid();
407 Bid ourBid = agent.historyOfBids.get(row).getOurBid();
408
409 switch (col) {
410 case 0:
411 return new JLabel(Integer.toString(row + 1));// roundcount
412 case 1:
413 String str1 = "No Bid yet.";
414 if (oppBid != null) {
415 str1 = new String(oppBid.toString());
416 str1 = str1.substring(4, str1.length() - 3);
417 }
418 return new JTextArea(str1); // opponent bid as string
419 case 2:
420 try {
421 double utilOpp = 0.0;
422 if (oppBid != null)
423 utilOpp = utilitySpace.getUtility(oppBid);// utility of
424 // opponent
425 // bid
426
427 DecimalFormat df = new DecimalFormat("0.00");
428 return new JTextArea(df.format(utilOpp));
429 } catch (Exception e) {
430 }
431 ;
432 case 3:
433 String str2 = new String(ourBid.toString());
434 str2 = str2.substring(4, str2.length() - 3);
435 return new JTextArea(str2); // our bid as string
436 case 4:
437 try {
438 double utilOur = utilitySpace.getUtility(ourBid);// utility
439 // of
440 // our
441 // bid
442 DecimalFormat df = new DecimalFormat("0.00");
443 return new JTextArea(df.format(utilOur));
444 } catch (Exception e) {
445 }
446 ;
447 }
448 }
449
450 return null;
451 }
452
453}
454
455/********************************************************/
456
457class NegoShowOffer extends NegoInfo {
458 private Bid topic;
459
460 public NegoShowOffer(Bid our, Bid opponent, AdditiveUtilitySpace us,
461 Bid topic) throws Exception {
462 super(our, opponent, us);
463 this.topic = topic;
464 }
465
466 private String[] colNames = { "Issue", "Current offer" };
467
468 @Override
469 public int getColumnCount() {
470 return 2;
471 }
472
473 @Override
474 public String getColumnName(int col) {
475 return colNames[col];
476 }
477
478 @Override
479 public Component getValueAt(int row, int col) {
480 if (row == issues.size()) {
481 if (col == 0)
482 return new JLabel("Utility:");
483 if (utilitySpace == null)
484 return new JLabel("No UtilSpace");
485 Bid bid;
486 if (col == 1)
487 bid = lastAccepted;
488 else
489 try {
490 bid = getBid();
491 } catch (Exception e) {
492 bid = null;
493 System.out.println("Internal err with getBid:"
494 + e.getMessage());
495 }
496 ;
497 JProgressBar bar = new JProgressBar(0, 100);
498 bar.setStringPainted(true);
499 try {
500 bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
501 bar.setIndeterminate(false);
502 } catch (Exception e) {
503 new Warning("Exception during cost calculation:"
504 + e.getMessage(), false, 1);
505 bar.setIndeterminate(true);
506 }
507
508 return bar;
509 }
510
511 switch (col) {
512 case 0:
513 return new JTextArea(issues.get(row).getName());
514 case 1:
515 Value value = null;
516 try {
517 value = getCurrentEval(topic, row);
518 } catch (Exception e) {
519 System.out.println("Err EnterBidDialog2.getValueAt: "
520 + e.getMessage());
521 }
522 if (value == null)
523 return new JTextArea("-");
524 return new JTextArea(value.toString());
525
526 }
527 return null;
528 }
529}
530
531class NegoProposeOffer extends NegoInfo {
532 NegoProposeOffer(Bid our, Bid opponent, AdditiveUtilitySpace us)
533 throws Exception {
534 super(our, opponent, us);
535 }
536
537 private String[] colNames = { "Issue", "Offer" };
538
539 @Override
540 public Component getValueAt(int row, int col) {
541 switch (col) {
542 case 0:
543 return super.getValueAt(row, col);
544 case 1:
545 return super.getValueAt(row, 2);
546 default:
547 return null;
548 }
549 }
550
551 @Override
552 public String getColumnName(int col) {
553 return colNames[col];
554 }
555
556 @Override
557 public boolean isCellEditable(int row, int col) {
558 return (col == 1 && row < issues.size());
559 }
560
561 @Override
562 public int getColumnCount() {
563 return 2;
564 }
565
566 @Override
567 public void actionPerformed(ActionEvent e) {
568 // System.out.println("event d!"+e);
569 // receiveMessage the cost and utility of our own bid.
570 fireTableCellUpdated(issues.size(), 1);
571 fireTableCellUpdated(issues.size() + 1, 1);
572 }
573}
574
575class NegoOffer extends NegoInfo {
576 NegoOffer(Bid our, Bid opponent, AdditiveUtilitySpace us) throws Exception {
577 super(our, opponent, us);
578 }
579
580 private String[] colNames = { "Issue", "Most recently accepted",
581 "Current offer" };
582
583 @Override
584 public Component getValueAt(int row, int col) {
585 if (row == issues.size()) {
586 if (col == 0)
587 return new JLabel("Utility:");
588 if (utilitySpace == null)
589 return new JLabel("No UtilSpace");
590 Bid bid;
591 if (col == 1)
592 bid = lastAccepted;
593 else
594 try {
595 bid = getBid();
596 } catch (Exception e) {
597 bid = null;
598 System.out.println("Internal err with getBid:"
599 + e.getMessage());
600 }
601 ;
602 JProgressBar bar = new JProgressBar(0, 100);
603 bar.setStringPainted(true);
604 try {
605 bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
606 bar.setIndeterminate(false);
607 } catch (Exception e) {
608 new Warning("Exception during cost calculation:"
609 + e.getMessage(), false, 1);
610 bar.setIndeterminate(true);
611 }
612
613 return bar;
614 }
615
616 switch (col) {
617 case 0:
618 return new JTextArea(issues.get(row).getName());
619 case 1:
620 Value value = null;
621 try {
622 value = getCurrentEval(lastAccepted, row);
623 } catch (Exception e) {
624 System.out.println("Err EnterBidDialog2.getValueAt: "
625 + e.getMessage());
626 }
627 if (value == null)
628 return new JTextArea("-");
629 return new JTextArea(value.toString());
630 case 2:
631 value = null;
632 try {
633 value = getCurrentEval(ourOldBid, row);
634 } catch (Exception e) {
635 System.out.println("Err EnterBidDialog2.getValueAt: "
636 + e.getMessage());
637 }
638 if (value == null)
639 return new JTextArea("-");
640 return new JTextArea(value.toString());
641
642 }
643 return null;
644 }
645
646 @Override
647 public String getColumnName(int col) {
648 return colNames[col];
649 }
650}
651
652/********************************************************************/
653
654class MyCellRenderer implements TableCellRenderer {
655 NegoInfo negoinfo;
656
657 public MyCellRenderer(NegoInfo n) {
658 negoinfo = n;
659 }
660
661 // the default converts everything to string...
662 public Component getTableCellRendererComponent(JTable table, Object value,
663 boolean isSelected, boolean hasFocus, int row, int column) {
664 return negoinfo.getValueAt(row, column);
665 }
666}
667
668/********************************************************************/
669
670class MyHistoryCellRenderer implements TableCellRenderer {
671 HistoryInfo historyinfo;
672
673 public MyHistoryCellRenderer(HistoryInfo n) {
674 historyinfo = n;
675 }
676
677 // the default converts everything to string...
678 public Component getTableCellRendererComponent(JTable table, Object value,
679 boolean isSelected, boolean hasFocus, int row, int column) {
680 return historyinfo.getValueAt(row, column);
681 }
682}
683
684/********************************************************/
685
686class MyCellEditor extends DefaultCellEditor {
687 private static final long serialVersionUID = 1L;
688 NegoInfo negoinfo;
689
690 public MyCellEditor(NegoInfo n) {
691 super(new JTextField("vaag")); // Java wants us to call super class, who
692 // cares...
693 negoinfo = n;
694 setClickCountToStart(1);
695 }
696
697 public Component getTableCellEditorComponent(JTable table, Object value,
698 boolean isSelected, int row, int column) {
699 return negoinfo.getValueAt(row, column);
700 }
701
702}
Note: See TracBrowser for help on using the repository browser.