1 | package ai2020.group6;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import geniusweb.actions.PartyId;
|
---|
10 | import geniusweb.actions.Vote;
|
---|
11 | import geniusweb.actions.Votes;
|
---|
12 | import geniusweb.issuevalue.Bid;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * PowerWeightedOptInStrategy collects the amount of power backing a bid such that
|
---|
16 | * more sophisticated strategies could use this.
|
---|
17 | *
|
---|
18 | * @author Group 6
|
---|
19 | */
|
---|
20 | public abstract class PowerWeightedOptInStrategy implements IOptInStrategy {
|
---|
21 |
|
---|
22 | public abstract Vote vote ( MAState state, Bid bid, Integer power, Integer minpower );
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public Votes optInVote(MAState state, List<Votes> votes) {
|
---|
26 | Map<PartyId, Integer> agentpowers = state.getPowerMap();
|
---|
27 | Map<Bid, Integer> bidpowers = new HashMap<>();
|
---|
28 | Map<Bid, Integer> bidthresholds = new HashMap<>();
|
---|
29 | votes.stream().forEach(agentvotes -> {
|
---|
30 | PartyId agentid = agentvotes.getActor();
|
---|
31 | agentvotes.getVotes().forEach(vote -> {
|
---|
32 | Integer power = bidpowers.getOrDefault(vote.getBid(), 0);
|
---|
33 | Integer minpower = bidthresholds.getOrDefault(vote.getBid(), 0);
|
---|
34 | power += agentpowers.getOrDefault(agentid, 0);
|
---|
35 | bidpowers.put(vote.getBid(), power);
|
---|
36 | bidthresholds.put(vote.getBid(), Math.max(vote.getMinPower(), minpower));
|
---|
37 | });
|
---|
38 | });
|
---|
39 | Set<Vote> optin = new HashSet<Vote>();
|
---|
40 | bidpowers.forEach((bid, power) -> {
|
---|
41 | Vote vote = vote(state, bid, bidpowers.getOrDefault(bid, 0), bidthresholds.getOrDefault(bid, 0));
|
---|
42 | if (vote != null)
|
---|
43 | optin.add(vote);
|
---|
44 | });
|
---|
45 |
|
---|
46 | return new Votes(state.getId(), optin);
|
---|
47 | }
|
---|
48 |
|
---|
49 | }
|
---|