1 | package ai2020.group6;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.math.BigDecimal;
|
---|
5 | import java.math.RoundingMode;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.logging.Level;
|
---|
11 |
|
---|
12 | import geniusweb.actions.Action;
|
---|
13 | import geniusweb.actions.Offer;
|
---|
14 | import geniusweb.actions.PartyId;
|
---|
15 | import geniusweb.inform.Finished;
|
---|
16 | import geniusweb.inform.Inform;
|
---|
17 | import geniusweb.inform.OptIn;
|
---|
18 | import geniusweb.inform.Settings;
|
---|
19 | import geniusweb.inform.Voting;
|
---|
20 | import geniusweb.inform.YourTurn;
|
---|
21 | import geniusweb.issuevalue.Bid;
|
---|
22 | import geniusweb.party.Capabilities;
|
---|
23 | import geniusweb.party.DefaultParty;
|
---|
24 | import geniusweb.profile.Profile;
|
---|
25 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
26 | import geniusweb.profile.utilityspace.UtilitySpace;
|
---|
27 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
28 | import geniusweb.profileconnection.ProfileInterface;
|
---|
29 | import geniusweb.progress.Progress;
|
---|
30 | import geniusweb.progress.ProgressRounds;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * MADefault party is a modular basis for a MOPaC negotiation agent, such that
|
---|
34 | * a new agent only has to define its strategies for the different phases without
|
---|
35 | * having to worry about about the design of the MOPaC negotiation and the
|
---|
36 | * implementation of the MAState interface.
|
---|
37 | *
|
---|
38 | * @author Group 6
|
---|
39 | */
|
---|
40 | public abstract class MADefaultParty extends DefaultParty implements MAState {
|
---|
41 |
|
---|
42 | public PartyId id;
|
---|
43 | protected ProfileInterface profileint;
|
---|
44 |
|
---|
45 | public Settings settings;
|
---|
46 | public Progress progress;
|
---|
47 |
|
---|
48 | public List<Action> actionHistory;
|
---|
49 |
|
---|
50 | public IAcceptanceStrategy acceptanceStrategy;
|
---|
51 | protected abstract IAcceptanceStrategy getAccceptanceStrategy ( Settings settings );
|
---|
52 |
|
---|
53 | public IBiddingStrategy biddingStrategy;
|
---|
54 | protected abstract IBiddingStrategy getBiddingStrategy ( Settings settings );
|
---|
55 |
|
---|
56 | public IOptInStrategy optinStrategy;
|
---|
57 | protected abstract IOptInStrategy getOptInStrategy ( Settings settings );
|
---|
58 |
|
---|
59 | // public Map<PartyId, IOpponentModel> opponentModels;
|
---|
60 | // protected abstract IOpponentModel initNewOpponentModel ( Settings settings ) ;
|
---|
61 | public Map<PartyId, Integer> powers;
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void notifyChange ( Inform info ) {
|
---|
65 | if ( info instanceof Settings ) {
|
---|
66 | handleSettings((Settings) info); return; }
|
---|
67 |
|
---|
68 | if ( info instanceof YourTurn ) {
|
---|
69 | handleYourTurn(); return; }
|
---|
70 |
|
---|
71 | if ( info instanceof Voting ) {
|
---|
72 | handleVoting((Voting) info); return; }
|
---|
73 |
|
---|
74 | if ( info instanceof OptIn ) {
|
---|
75 | handleOptIn((OptIn) info); return; }
|
---|
76 |
|
---|
77 | if ( info instanceof Finished ) {
|
---|
78 | handleFinished((Finished) info); return; }
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void handleSettings ( Settings settings ) {
|
---|
83 | id = settings.getID();
|
---|
84 | try {
|
---|
85 | profileint = ProfileConnectionFactory.create(settings.getProfile().getURI(), getReporter());
|
---|
86 | } catch (Exception e) { e.printStackTrace(); }
|
---|
87 | this.settings = settings;
|
---|
88 | progress = settings.getProgress();
|
---|
89 |
|
---|
90 | actionHistory = new ArrayList<Action>();
|
---|
91 |
|
---|
92 | acceptanceStrategy = getAccceptanceStrategy(settings);
|
---|
93 | biddingStrategy = getBiddingStrategy(settings);
|
---|
94 | optinStrategy = getOptInStrategy(settings);
|
---|
95 | // opponentModels = new HashMap<PartyId, IOpponentModel>();
|
---|
96 | }
|
---|
97 | public void handleYourTurn ( ) {
|
---|
98 | Bid bid = biddingStrategy.generateBid(this);
|
---|
99 |
|
---|
100 | Action action = new Offer(id, bid);
|
---|
101 | actionHistory.add(action);
|
---|
102 |
|
---|
103 | try {
|
---|
104 | getConnection().send(action);
|
---|
105 | } catch (IOException e) { e.printStackTrace(); }
|
---|
106 | }
|
---|
107 | public void handleVoting ( Voting voting ) {
|
---|
108 | // voting.getBids().stream().forEach(offer -> {
|
---|
109 | // PartyId oid = offer.getActor();
|
---|
110 | // IOpponentModel om = opponentModels.getOrDefault(oid, initNewOpponentModel(settings));
|
---|
111 | // IOpponentModel nom = om.updateOpponentModel(this, offer);
|
---|
112 | // opponentModels.replace(oid, nom);
|
---|
113 | // });
|
---|
114 |
|
---|
115 | powers = voting.getPowers();
|
---|
116 |
|
---|
117 | Action action = acceptanceStrategy.acceptanceVote(this, voting.getBids());
|
---|
118 |
|
---|
119 | actionHistory.add(action);
|
---|
120 |
|
---|
121 | try {
|
---|
122 | getConnection().send(action);
|
---|
123 | } catch (IOException e) { e.printStackTrace(); }
|
---|
124 | }
|
---|
125 | public void handleOptIn ( OptIn optin ) {
|
---|
126 | // optin.getVotes().stream().forEach(votes -> {
|
---|
127 | // PartyId oid = votes.getActor();
|
---|
128 | // if (oid.equals(id)) return;
|
---|
129 | // IOpponentModel om = opponentModels.get(oid);
|
---|
130 | // IOpponentModel nom = om.updateOpponentModel(this, votes);
|
---|
131 | // opponentModels.replace(oid, nom);
|
---|
132 | // });
|
---|
133 |
|
---|
134 |
|
---|
135 | Action action = optinStrategy.optInVote(this, optin.getVotes());
|
---|
136 |
|
---|
137 | actionHistory.add(action);
|
---|
138 |
|
---|
139 | try {
|
---|
140 | getConnection().send(action);
|
---|
141 | } catch (IOException e) { e.printStackTrace(); }
|
---|
142 |
|
---|
143 | if (progress instanceof ProgressRounds)
|
---|
144 | progress = ((ProgressRounds) progress).advance();
|
---|
145 | }
|
---|
146 | public void handleFinished ( Finished finished ) {
|
---|
147 | getReporter().log(Level.INFO, "Final ourcome:" + finished);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public PartyId getId ( ) {
|
---|
151 | return id;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public Profile getProfile ( ) {
|
---|
155 | try {
|
---|
156 | return profileint.getProfile();
|
---|
157 | } catch (IOException e) { e.printStackTrace(); return null; }
|
---|
158 | }
|
---|
159 |
|
---|
160 | public Action getLastAction ( ) {
|
---|
161 | return actionHistory.get(actionHistory.size()-1);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public Map<PartyId, Integer> getPowerMap ( ) {
|
---|
165 | return powers;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public BigDecimal getProgressTime ( ) {
|
---|
169 | return BigDecimal.valueOf(progress.get(System.currentTimeMillis())).setScale(6, RoundingMode.HALF_UP);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public UtilitySpace getUtilitySpace() {
|
---|
173 | try {
|
---|
174 | return (UtilitySpace) profileint.getProfile();
|
---|
175 | } catch (IOException e) { e.printStackTrace(); return null; }
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public Capabilities getCapabilities() {
|
---|
180 | return new Capabilities(Collections.singleton("MOPAC"),
|
---|
181 | Collections.singleton(LinearAdditive.class));
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public String getDescription() {
|
---|
186 | return "Modular MOPaC agent";
|
---|
187 | }
|
---|
188 | }
|
---|