1 | package negotiator.ahbuneagent.linearorder;
|
---|
2 |
|
---|
3 | import geniusweb.issuevalue.Bid;
|
---|
4 | import geniusweb.issuevalue.Domain;
|
---|
5 | import geniusweb.profile.DefaultPartialOrdering;
|
---|
6 | import geniusweb.profile.Profile;
|
---|
7 | import geniusweb.profile.utilityspace.UtilitySpace;
|
---|
8 |
|
---|
9 | import java.math.BigDecimal;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | /*A simple list of bids, but all bids are fully ordered (better or worse than
|
---|
15 | other bids in the list).*/
|
---|
16 | public class SimpleLinearOrdering implements UtilitySpace {
|
---|
17 |
|
---|
18 | private final Domain domain;
|
---|
19 | private final List<Bid> bids; // worst bid first, best bid last.
|
---|
20 |
|
---|
21 | public SimpleLinearOrdering(Profile profile) {
|
---|
22 | this(profile.getDomain(), getSortedBids(profile));
|
---|
23 | }
|
---|
24 |
|
---|
25 | SimpleLinearOrdering(Domain domain, List<Bid> bids) {
|
---|
26 | this.domain = domain;
|
---|
27 | this.bids = bids;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public Bid getMinBid(){
|
---|
31 | if(bids.size() > 0){
|
---|
32 | return bids.get(0);
|
---|
33 | }
|
---|
34 | return null;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public Bid getMaxBid(){
|
---|
38 | if(bids.size() > 0){
|
---|
39 | return bids.get(bids.size() - 1);
|
---|
40 | }
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Bid getBidByIndex(int index) {
|
---|
45 | if(index < bids.size()){
|
---|
46 | return bids.get(index);
|
---|
47 | }
|
---|
48 | return null;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public int getKnownBidsSize(){
|
---|
52 | return bids.size();
|
---|
53 | }
|
---|
54 |
|
---|
55 | //a list of bids in the profile sorted from low to high utility.
|
---|
56 | private static List<Bid> getSortedBids(Profile profile) {
|
---|
57 | if (!(profile instanceof DefaultPartialOrdering)) {
|
---|
58 | throw new UnsupportedOperationException("Only DefaultPartialOrdering supported");
|
---|
59 | }
|
---|
60 | DefaultPartialOrdering prof = (DefaultPartialOrdering) profile;
|
---|
61 | List<Bid> bidslist = prof.getBids();
|
---|
62 | // NOTE sort defaults to ascending order
|
---|
63 | bidslist.sort((b1, b2) -> prof.isPreferredOrEqual(b1, b2) ? 1 : -1);
|
---|
64 |
|
---|
65 | return bidslist;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public String getName() {
|
---|
70 | throw new UnsupportedOperationException();
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public Domain getDomain() {
|
---|
75 | return domain;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public Bid getReservationBid() {
|
---|
80 | throw new UnsupportedOperationException();
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public BigDecimal getUtility(Bid bid) {
|
---|
85 | if (!bids.contains(bid)) {
|
---|
86 | return BigDecimal.ZERO;
|
---|
87 | }
|
---|
88 | return new BigDecimal(bids.indexOf(bid) + 1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public boolean contains(Bid bid) {
|
---|
92 | return bids.contains(bid);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public List<Bid> getBids() {
|
---|
96 | return Collections.unmodifiableList(bids);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*SimpleLinearOrdering, updated with the given comparison. Thee
|
---|
100 | bid will be inserted after the first bid that is not worse than
|
---|
101 | bid.*/
|
---|
102 | public SimpleLinearOrdering with(Bid bid, List<Bid> worseBids) {
|
---|
103 | int n = 0;
|
---|
104 | while (n < bids.size() && worseBids.contains(bids.get(n))) {
|
---|
105 | n++;
|
---|
106 | }
|
---|
107 | LinkedList<Bid> newbids = new LinkedList<Bid>(bids);
|
---|
108 | newbids.add(n, bid);
|
---|
109 |
|
---|
110 | return new SimpleLinearOrdering(domain, newbids);
|
---|
111 | }
|
---|
112 | }
|
---|