source: ai2020/group6/MADefaultParty.java@ 5

Last change on this file since 5 was 3, checked in by wouter, 4 years ago

#1925 added group6 code.

File size: 5.6 KB
RevLine 
[3]1package ai2020.group6;
2
3import java.io.IOException;
4import java.math.BigDecimal;
5import java.math.RoundingMode;
6import java.util.ArrayList;
7import java.util.Collections;
8import java.util.List;
9import java.util.Map;
10import java.util.logging.Level;
11
12import geniusweb.actions.Action;
13import geniusweb.actions.Offer;
14import geniusweb.actions.PartyId;
15import geniusweb.inform.Finished;
16import geniusweb.inform.Inform;
17import geniusweb.inform.OptIn;
18import geniusweb.inform.Settings;
19import geniusweb.inform.Voting;
20import geniusweb.inform.YourTurn;
21import geniusweb.issuevalue.Bid;
22import geniusweb.party.Capabilities;
23import geniusweb.party.DefaultParty;
24import geniusweb.profile.Profile;
25import geniusweb.profile.utilityspace.LinearAdditive;
26import geniusweb.profile.utilityspace.UtilitySpace;
27import geniusweb.profileconnection.ProfileConnectionFactory;
28import geniusweb.profileconnection.ProfileInterface;
29import geniusweb.progress.Progress;
30import 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 */
40public 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}
Note: See TracBrowser for help on using the repository browser.