[1] | 1 | package genius.gui.progress.session;
|
---|
| 2 |
|
---|
| 3 | import java.awt.BorderLayout;
|
---|
| 4 | import java.awt.Color;
|
---|
| 5 | import java.awt.Dimension;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.JPanel;
|
---|
| 8 | import javax.swing.SwingUtilities;
|
---|
| 9 | import javax.swing.event.ListDataEvent;
|
---|
| 10 | import javax.swing.event.ListDataListener;
|
---|
| 11 |
|
---|
| 12 | import org.jfree.chart.ChartPanel;
|
---|
| 13 | import org.jfree.chart.JFreeChart;
|
---|
| 14 | import org.jfree.chart.axis.NumberAxis;
|
---|
| 15 | import org.jfree.chart.plot.XYPlot;
|
---|
| 16 | import org.jfree.chart.renderer.xy.XYDotRenderer;
|
---|
| 17 | import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
|
---|
| 18 | import org.jfree.data.xy.XYDataItem;
|
---|
| 19 | import org.jfree.data.xy.XYDataset;
|
---|
| 20 | import org.jfree.data.xy.XYSeries;
|
---|
| 21 | import org.jfree.data.xy.XYSeriesCollection;
|
---|
| 22 |
|
---|
| 23 | import genius.core.AgentID;
|
---|
| 24 | import genius.core.Bid;
|
---|
| 25 | import genius.core.BidIterator;
|
---|
| 26 | import genius.core.analysis.BidPoint;
|
---|
| 27 | import genius.core.analysis.MultilateralAnalysis;
|
---|
| 28 | import genius.core.utility.UtilitySpace;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * * Bilateral version of ProgressChart. Shows utility of party 1 on the X axis,
|
---|
| 32 | * and of party 2 on the Y (vertical) axis.
|
---|
| 33 | */
|
---|
| 34 | @SuppressWarnings("serial")
|
---|
| 35 | public class ProgressChartBi extends JPanel {
|
---|
| 36 |
|
---|
| 37 | private static final String UTILITY = " utility";
|
---|
| 38 | final XYSeriesCollection dataset = new XYSeriesCollection();
|
---|
| 39 | private OutcomesListModel model;
|
---|
| 40 | private XYSeries bidsA, bidsB, agreements;
|
---|
| 41 | private AgentID party1;
|
---|
| 42 | private AgentID party2;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param model
|
---|
| 46 | * all outcomes so far. Assumes that only items are being added
|
---|
| 47 | * to the OutcomesModel, one at a time. Assumes that model has 2
|
---|
| 48 | * parties.
|
---|
| 49 | * @param showAllBids
|
---|
| 50 | * all bids in the bidspace are shown iff this is true.
|
---|
| 51 | */
|
---|
| 52 | public ProgressChartBi(OutcomesListModel model, boolean showAllBids) {
|
---|
| 53 | this.model = model;
|
---|
| 54 | setLayout(new BorderLayout());
|
---|
| 55 | setMinimumSize(new Dimension(300, 300));
|
---|
| 56 | party1 = model.getParties().get(0).getID();
|
---|
| 57 | party2 = model.getParties().get(1).getID();
|
---|
| 58 | NumberAxis domainAxis = new NumberAxis(party1.toString() + UTILITY);
|
---|
| 59 | NumberAxis rangeAxis = new NumberAxis(party2.toString() + UTILITY);
|
---|
| 60 |
|
---|
| 61 | bidsA = new XYSeries(party1.toString(), false);
|
---|
| 62 | bidsB = new XYSeries(party2.toString(), false);
|
---|
| 63 | agreements = new XYSeries("agreements");
|
---|
| 64 | XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, new XYLineAndShapeRenderer());
|
---|
| 65 |
|
---|
| 66 | dataset.addSeries(agreements); // first one has priority on plot.
|
---|
| 67 | dataset.addSeries(bidsA);
|
---|
| 68 | dataset.addSeries(bidsB);
|
---|
| 69 |
|
---|
| 70 | // add some background info
|
---|
| 71 | MultilateralAnalysis analysis = new MultilateralAnalysis(model.getParties(), null, null);
|
---|
| 72 | dataset.addSeries(getNash(analysis));
|
---|
| 73 | dataset.addSeries(getKalai(analysis));
|
---|
| 74 | dataset.addSeries(getPareto(analysis));
|
---|
| 75 |
|
---|
| 76 | if (showAllBids) {
|
---|
| 77 | XYDotRenderer dotRenderer = new XYDotRenderer();
|
---|
| 78 | dotRenderer.setDotHeight(2);
|
---|
| 79 | dotRenderer.setDotWidth(2);
|
---|
| 80 | dotRenderer.setSeriesPaint(0, Color.PINK);
|
---|
| 81 | plot.setDataset(2, getAllBids());
|
---|
| 82 | plot.setRenderer(2, dotRenderer);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // fix the color of renderer 3: its color is invisible yellow ...
|
---|
| 86 | plot.getRenderer().setSeriesPaint(3, Color.BLACK);
|
---|
| 87 |
|
---|
| 88 | JFreeChart freechart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
|
---|
| 89 | ChartPanel chartPanel = new ChartPanel(freechart);
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * BAD: JFreeChart doesn't have a model so we must connect it directly.
|
---|
| 93 | * actually, there IS a model: {@link XYDataset}. Maybe we can fix this.
|
---|
| 94 | */
|
---|
| 95 | model.addListDataListener(new ListDataListener() {
|
---|
| 96 |
|
---|
| 97 | @Override
|
---|
| 98 | public void intervalRemoved(ListDataEvent e) {
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @Override
|
---|
| 102 | public void intervalAdded(ListDataEvent e) {
|
---|
| 103 | addNewOutcome();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | @Override
|
---|
| 107 | public void contentsChanged(ListDataEvent e) {
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | });
|
---|
| 111 |
|
---|
| 112 | add(chartPanel, BorderLayout.CENTER);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private XYSeries getKalai(MultilateralAnalysis analysis) {
|
---|
| 116 | XYSeries kalaiSeries = new XYSeries("Kalai");
|
---|
| 117 | BidPoint point = analysis.getKalaiPoint();
|
---|
| 118 | kalaiSeries.add(point.getUtilityA(), point.getUtilityB());
|
---|
| 119 | return kalaiSeries;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private XYSeries getNash(MultilateralAnalysis analysis) {
|
---|
| 123 | XYSeries nashSeries = new XYSeries("Nash");
|
---|
| 124 | BidPoint point = analysis.getNashPoint();
|
---|
| 125 | nashSeries.add(point.getUtilityA(), point.getUtilityB());
|
---|
| 126 | return nashSeries;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * Get all bids in a dataset. Just iterates over domain of first party
|
---|
| 131 | *
|
---|
| 132 | * @return {@link XYDataSet} containing all bids.
|
---|
| 133 | */
|
---|
| 134 | private XYDataset getAllBids() {
|
---|
| 135 |
|
---|
| 136 | UtilitySpace utils1 = model.getParties().get(0).getUtilitySpace();
|
---|
| 137 | UtilitySpace utils2 = model.getParties().get(1).getUtilitySpace();
|
---|
| 138 |
|
---|
| 139 | BidIterator bids = new BidIterator(utils1.getDomain());
|
---|
| 140 | XYSeries series = new XYSeries("All bids");
|
---|
| 141 | while (bids.hasNext()) {
|
---|
| 142 | Bid bid = bids.next();
|
---|
| 143 | series.add(utils1.getUtility(bid), utils2.getUtility(bid));
|
---|
| 144 | }
|
---|
| 145 | return new XYSeriesCollection(series);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | private XYSeries getPareto(MultilateralAnalysis analysis) {
|
---|
| 149 | XYSeries series = new XYSeries("pareto");
|
---|
| 150 | for (BidPoint point : analysis.getParetoFrontier()) {
|
---|
| 151 | series.add(point.getUtilityA(), point.getUtilityB());
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | return series;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private void addNewOutcome() {
|
---|
| 158 | SwingUtilities.invokeLater(new Runnable() {
|
---|
| 159 |
|
---|
| 160 | @Override
|
---|
| 161 | public void run() {
|
---|
| 162 | // invariant n is current number of data points in graph.
|
---|
| 163 | int n = bidsA.getItemCount() + bidsB.getItemCount();
|
---|
| 164 |
|
---|
| 165 | // continue till all model points are in graph
|
---|
| 166 | while (n < model.size()) {
|
---|
| 167 | Outcome outcome = model.get(n);
|
---|
| 168 | XYDataItem item = new XYDataItem((double) outcome.getDiscountedUtilities().get(0),
|
---|
| 169 | (double) outcome.getDiscountedUtilities().get(1));
|
---|
| 170 | if (party1.equals(outcome.getAgentID())) {
|
---|
| 171 | bidsA.add(item);
|
---|
| 172 | } else {
|
---|
| 173 | bidsB.add(item);
|
---|
| 174 | }
|
---|
| 175 | if (outcome.isAgreement()) {
|
---|
| 176 | agreements.add(item);
|
---|
| 177 | }
|
---|
| 178 | n++;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | }
|
---|
| 182 | });
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | }
|
---|