1 | package negotiator.parties;
|
---|
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 |
|
---|
31 | /** Creates a new instance of UIAgent */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * One agent will be kept alive over multiple sessions. Init will be called
|
---|
35 | * at the start of each nego session.
|
---|
36 | */
|
---|
37 | @Override
|
---|
38 | public String getVersion() {
|
---|
39 | return "2.0";
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void init() {
|
---|
43 |
|
---|
44 | System.out.println("try to init UIAgent");
|
---|
45 | System.out.println("Utility Space initialized: " + utilitySpace);
|
---|
46 | historyOfBids = new ArrayList<NegoRoundData>();
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | private EnterBidDialogExtended getDialog() throws Exception {
|
---|
51 | EnterBidDialogExtended ui = new EnterBidDialogExtended(this, null,
|
---|
52 | true, (AdditiveUtilitySpace) utilitySpace, oppPreviousBid);
|
---|
53 | // alina: dialog in the center- doesnt really work
|
---|
54 | Toolkit t = Toolkit.getDefaultToolkit();
|
---|
55 | int x = (int) ((t.getScreenSize().getWidth() - ui.getWidth()) / 2);
|
---|
56 | int y = (int) ((t.getScreenSize().getHeight() - ui.getHeight()) / 2);
|
---|
57 | ui.setLocation(x, y);
|
---|
58 | return ui;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void ReceiveMessage(Action opponentAction) {
|
---|
62 | this.opponentAction = opponentAction;
|
---|
63 | if (opponentAction instanceof Accept)
|
---|
64 | JOptionPane.showMessageDialog(null,
|
---|
65 | "Opponent accepted your last offer.");
|
---|
66 |
|
---|
67 | if (opponentAction instanceof EndNegotiation)
|
---|
68 | JOptionPane.showMessageDialog(null,
|
---|
69 | "Opponent canceled the negotiation session");
|
---|
70 |
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public Action chooseAction() {
|
---|
75 | try {
|
---|
76 | return chooseAction1();
|
---|
77 | } catch (Exception e) {
|
---|
78 | e.printStackTrace();
|
---|
79 | }
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Action chooseAction1() throws Exception {
|
---|
84 | Action action = getDialog().askUserForAction(opponentAction,
|
---|
85 | myPreviousBid);
|
---|
86 | if ((action != null) && (action instanceof Offer)) {
|
---|
87 | myPreviousBid = ((Offer) action).getBid();
|
---|
88 | if (opponentAction != null) {
|
---|
89 | oppPreviousBid = ((Offer) opponentAction).getBid();
|
---|
90 | roundData = new NegoRoundData(oppPreviousBid, myPreviousBid);
|
---|
91 | historyOfBids.add(roundData);
|
---|
92 | }
|
---|
93 | // does this happen only the first time?
|
---|
94 | else {
|
---|
95 | roundData = new NegoRoundData(null, myPreviousBid);
|
---|
96 | historyOfBids.add(roundData);
|
---|
97 | }
|
---|
98 | bidCounter++;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return action;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public boolean isUIAgent() {
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public Bid getMyPreviousBid() {
|
---|
109 | return myPreviousBid;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public Bid getOppPreviousBid() {
|
---|
113 | return oppPreviousBid;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public SupportedNegotiationSetting getSupportedNegotiationSetting() {
|
---|
118 | return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | class NegoRoundData {
|
---|
123 | private Bid lastOppBid;
|
---|
124 | private Bid ourLastBid;
|
---|
125 |
|
---|
126 | public NegoRoundData(Bid lastOppBid, Bid ourLastBid) {
|
---|
127 | this.lastOppBid = lastOppBid;
|
---|
128 | this.ourLastBid = ourLastBid;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public Bid getOppentBid() {
|
---|
132 | return lastOppBid;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public Bid getOurBid() {
|
---|
136 | return ourLastBid;
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|