1 | package agents;
|
---|
2 |
|
---|
3 | import java.awt.Toolkit;
|
---|
4 | import java.util.ArrayList;
|
---|
5 |
|
---|
6 | import javax.swing.JOptionPane;
|
---|
7 |
|
---|
8 | import genius.core.Agent;
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.SupportedNegotiationSetting;
|
---|
11 | import genius.core.actions.Accept;
|
---|
12 | import genius.core.actions.Action;
|
---|
13 | import genius.core.actions.EndNegotiation;
|
---|
14 | import genius.core.actions.Offer;
|
---|
15 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | *
|
---|
19 | * @author W.Pasman, modified version of Dmytro's UIAgent
|
---|
20 | */
|
---|
21 | public class UIAgentExtended extends Agent {
|
---|
22 | private Action opponentAction = null;
|
---|
23 | private Bid myPreviousBid = null;
|
---|
24 |
|
---|
25 | // Alina added...
|
---|
26 | private Bid oppPreviousBid = null;
|
---|
27 | protected int bidCounter = 0;
|
---|
28 | protected NegoRoundData roundData;
|
---|
29 | protected ArrayList<NegoRoundData> historyOfBids = null;
|
---|
30 | private Bid lastReceivedBid = null;
|
---|
31 |
|
---|
32 | /** Creates a new instance of UIAgent */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * One agent will be kept alive over multiple sessions. Init will be called
|
---|
36 | * at the start of each nego session.
|
---|
37 | */
|
---|
38 | @Override
|
---|
39 | public String getVersion() {
|
---|
40 | return "2.0";
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void init() {
|
---|
45 |
|
---|
46 | System.out.println("try to init UIAgent");
|
---|
47 | System.out.println("Utility Space initialized: " + utilitySpace);
|
---|
48 | historyOfBids = new ArrayList<NegoRoundData>();
|
---|
49 | }
|
---|
50 |
|
---|
51 | private EnterBidDialogExtended getDialog() throws Exception {
|
---|
52 | EnterBidDialogExtended ui = new EnterBidDialogExtended(this, null, true,
|
---|
53 | (AdditiveUtilitySpace) utilitySpace, lastReceivedBid);
|
---|
54 | // alina: dialog in the center- doesnt really work
|
---|
55 | Toolkit t = Toolkit.getDefaultToolkit();
|
---|
56 | int x = (int) ((t.getScreenSize().getWidth() - ui.getWidth()) / 2);
|
---|
57 | int y = (int) ((t.getScreenSize().getHeight() - ui.getHeight()) / 2);
|
---|
58 | ui.setLocation(x, y);
|
---|
59 | return ui;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void ReceiveMessage(Action opponentAction) {
|
---|
64 | this.opponentAction = opponentAction;
|
---|
65 | if (opponentAction instanceof Accept)
|
---|
66 | JOptionPane.showMessageDialog(null,
|
---|
67 | "Opponent accepted your last offer.");
|
---|
68 |
|
---|
69 | if (opponentAction instanceof EndNegotiation)
|
---|
70 | JOptionPane.showMessageDialog(null,
|
---|
71 | "Opponent canceled the negotiation session");
|
---|
72 |
|
---|
73 | if (opponentAction instanceof Offer) {
|
---|
74 | lastReceivedBid = ((Offer) opponentAction).getBid();
|
---|
75 | }
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public Action chooseAction() {
|
---|
81 | try {
|
---|
82 | return chooseAction1();
|
---|
83 | } catch (Exception e) {
|
---|
84 | e.printStackTrace();
|
---|
85 | }
|
---|
86 | return null; // too bad, this will report the bug.
|
---|
87 | }
|
---|
88 |
|
---|
89 | public Action chooseAction1() throws Exception {
|
---|
90 | Action action = getDialog().askUserForAction(opponentAction,
|
---|
91 | myPreviousBid);
|
---|
92 | if ((action != null) && (action instanceof Offer)) {
|
---|
93 | myPreviousBid = ((Offer) action).getBid();
|
---|
94 | if (opponentAction != null) {
|
---|
95 | oppPreviousBid = ((Offer) opponentAction).getBid();
|
---|
96 | roundData = new NegoRoundData(oppPreviousBid, myPreviousBid);
|
---|
97 | historyOfBids.add(roundData);
|
---|
98 | }
|
---|
99 | // does this happen only the first time?
|
---|
100 | else {
|
---|
101 | roundData = new NegoRoundData(null, myPreviousBid);
|
---|
102 | historyOfBids.add(roundData);
|
---|
103 | }
|
---|
104 | bidCounter++;
|
---|
105 | }
|
---|
106 |
|
---|
107 | return action;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public boolean isUIAgent() {
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public Bid getMyPreviousBid() {
|
---|
115 | return myPreviousBid;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public Bid getOppPreviousBid() {
|
---|
119 | return oppPreviousBid;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public SupportedNegotiationSetting getSupportedNegotiationSetting() {
|
---|
124 | return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public String getDescription() {
|
---|
129 | return "Human user interface to place bids";
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|
133 |
|
---|
134 | class NegoRoundData {
|
---|
135 | private Bid lastOppBid;
|
---|
136 | private Bid ourLastBid;
|
---|
137 |
|
---|
138 | public NegoRoundData(Bid lastOppBid, Bid ourLastBid) {
|
---|
139 | this.lastOppBid = lastOppBid;
|
---|
140 | this.ourLastBid = ourLastBid;
|
---|
141 | }
|
---|
142 |
|
---|
143 | public Bid getOppentBid() {
|
---|
144 | return lastOppBid;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public Bid getOurBid() {
|
---|
148 | return ourLastBid;
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|