source: src/main/java/genius/core/analysis/ParetoFrontier.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 3.1 KB
Line 
1package genius.core.analysis;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.List;
8
9/**
10 * Class which stores the Pareto-frontier. This class can be used to easily add
11 * bids to the Pareto-frontier when calculating it.
12 *
13 * @author Tim Baarslag
14 */
15public class ParetoFrontier implements Serializable {
16
17 /**
18 *
19 */
20 private static final long serialVersionUID = 576939761881501811L;
21 /** List holding the Pareto-frontier bids. */
22 private List<BidPoint> frontier;
23
24 /**
25 * Create an empty list to store the Pareto-frontier.
26 */
27 public ParetoFrontier() {
28 frontier = new ArrayList<BidPoint>();
29 }
30
31 /**
32 * Determines if a bid should be added to the Pareto-frontier. A bid is
33 * added when it strictly dominates a bid in the Pareto-frontier OR when it
34 * is equal to a bid in the Pareto-frontier.
35 *
36 * @param bidpoint
37 * bid to be added to the Pareto-frontier.
38 */
39 public void mergeIntoFrontier(BidPoint bidpoint) {
40 for (BidPoint f : frontier) {
41 if (bidpoint.isStrictlyDominatedBy(f)) {
42 // the bidpoint is dominated, and therefore not added
43 return;
44 }
45 if (f.isStrictlyDominatedBy(bidpoint)) {
46 // the bidpoint dominates a Pareto-bid, which is therefore
47 // replaced
48 merge(bidpoint, f);
49 return;
50 }
51 }
52 // the bid must be equal to an existing bid, therefore add it
53 frontier.add(bidpoint);
54 }
55
56 /**
57 * Adds a bid to the Pareto frontier
58 *
59 * @param newParetoBid
60 * new Pareto bid.
61 * @param dominatedParetoBid
62 * old Pareto bid which must now be removed.
63 */
64 private void merge(BidPoint newParetoBid, BidPoint dominatedParetoBid) {
65 // remove dominated bid.
66 frontier.remove(dominatedParetoBid);
67 // there might still be more bids which should be removed.
68 ArrayList<BidPoint> toBeRemoved = new ArrayList<BidPoint>();
69 for (BidPoint f : frontier) {
70 if (f.isStrictlyDominatedBy(newParetoBid)) // delete dominated
71 // frontier points
72 toBeRemoved.add(f);
73 }
74 frontier.removeAll(toBeRemoved);
75 frontier.add(newParetoBid);
76 }
77
78 /**
79 * Returns true if the given BidPoint is not part of the Pareto-frontier.
80 *
81 * @param bid
82 * to be checked if it is Pareto-optimal.
83 * @return true if NOT pareto-optimal bid.
84 */
85 public boolean isBelowFrontier(BidPoint bid) {
86 for (BidPoint f : this.getFrontier())
87 if (bid.isStrictlyDominatedBy(f))
88 return true;
89 return false;
90 }
91
92 /**
93 * Order the bids based on the utility for agent A.
94 */
95 public void sort() {
96 Collections.sort(frontier, new Comparator<BidPoint>() {
97 @Override
98 public int compare(BidPoint x, BidPoint y) {
99 if (x.getUtilityA() < y.getUtilityA())
100 return -1;
101 else if (x.getUtilityA() > y.getUtilityA())
102 return 1;
103 else
104 return 0;
105 }
106 });
107 }
108
109 /**
110 * Returns the Pareto-optimal frontier.
111 *
112 * @return Pareto-optimal frontier.
113 */
114 public List<BidPoint> getFrontier() {
115 return frontier;
116 }
117}
Note: See TracBrowser for help on using the repository browser.