1 | package geniusweb.bidspace.pareto;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.eclipse.jdt.annotation.NonNull;
|
---|
10 |
|
---|
11 | import geniusweb.issuevalue.Bid;
|
---|
12 | import geniusweb.issuevalue.Value;
|
---|
13 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * PartialPareto contains a pareto surface of partial bids. Internal-only
|
---|
17 | * utility class for computing LinearAdditive pareto surface. Immutable.
|
---|
18 | */
|
---|
19 | class PartialPareto {
|
---|
20 | private final @NonNull List<@NonNull ParetoPoint> points = new ArrayList<>();;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @param points the points to be contained in this PartialPareto
|
---|
24 | */
|
---|
25 | private PartialPareto(@NonNull List<@NonNull ParetoPoint> points) {
|
---|
26 | if (points == null || points.contains(null))
|
---|
27 | throw new NullPointerException("points must not contain null");
|
---|
28 | this.points.addAll(points);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructor. Avoided the standard 'new' constructor as this is
|
---|
33 | * implemented recursively (O(log(#issues))).
|
---|
34 | *
|
---|
35 | * @param utilSpaces the {@link LinearAdditive}s
|
---|
36 | * @param issues the issues (subset of all issues in the space) to be
|
---|
37 | * used for this pareto
|
---|
38 | * @return the pareto surface (non-dominated bids) when considering only the
|
---|
39 | * given issues.
|
---|
40 | */
|
---|
41 | public static @NonNull PartialPareto create(
|
---|
42 | @NonNull List<@NonNull LinearAdditive> utilSpaces,
|
---|
43 | @NonNull List<@NonNull String> issues) {
|
---|
44 | if (issues.size() == 1) {
|
---|
45 | @NonNull
|
---|
46 | String issue = issues.get(0);
|
---|
47 | @NonNull
|
---|
48 | Map<@NonNull String, @NonNull Value> map = new HashMap<>();
|
---|
49 | @NonNull
|
---|
50 | List<@NonNull ParetoPoint> list = new LinkedList<>();
|
---|
51 | for (@NonNull
|
---|
52 | Value value : utilSpaces.get(0).getDomain().getValues(issue)) {
|
---|
53 | map.put(issue, value);
|
---|
54 | list = add(list, new ParetoPoint(new Bid(map), utilSpaces));
|
---|
55 | }
|
---|
56 | return new PartialPareto(list);
|
---|
57 | } else {
|
---|
58 | int halfway = issues.size() / 2;
|
---|
59 | return create(utilSpaces, issues.subList(0, halfway)).merge(
|
---|
60 | create(utilSpaces, issues.subList(halfway, issues.size())));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | *
|
---|
66 | * @return the ParetoPoints on the (possibly partial) Pareto surface
|
---|
67 | */
|
---|
68 | public @NonNull List<@NonNull ParetoPoint> getPoints() {
|
---|
69 | return points;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * This combines two partial paretos. Here is the heart of the algorithm.
|
---|
74 | *
|
---|
75 | * @param other another {@link PartialPareto} to be merged with this. The
|
---|
76 | * other pareto must be composed with non-overlapping range.
|
---|
77 | * @return merge of two partial paretos. Checks all partial1*partial2
|
---|
78 | * combinations and only non-dominated points are kept.
|
---|
79 | */
|
---|
80 | private @NonNull PartialPareto merge(@NonNull PartialPareto other) {
|
---|
81 | @NonNull
|
---|
82 | List<@NonNull ParetoPoint> merge = new LinkedList<>();// fast add and iterate
|
---|
83 | for (@NonNull
|
---|
84 | ParetoPoint point : points) {
|
---|
85 | for (@NonNull
|
---|
86 | ParetoPoint otherpoint : other.points) {
|
---|
87 | merge = add(merge, point.merge(otherpoint));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return new PartialPareto(merge);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Add a new ParetoPoint to a list
|
---|
95 | *
|
---|
96 | * @param list the existing list
|
---|
97 | * @param candidate a new {@link ParetoPoint} candidate
|
---|
98 | * @return a new list that either ignored the candidate, or added it and
|
---|
99 | * removed the old points that are dominated by the candidate
|
---|
100 | */
|
---|
101 | private static @NonNull List<@NonNull ParetoPoint> add(
|
---|
102 | @NonNull List<@NonNull ParetoPoint> list,
|
---|
103 | @NonNull ParetoPoint candidate) {
|
---|
104 | for (@NonNull
|
---|
105 | ParetoPoint existing : list) {
|
---|
106 | if (candidate.isDominatedBy(existing)) {
|
---|
107 | return list;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | // if we get here, candidate is not dominated so we add it
|
---|
111 | // and remove existing ones now dominated
|
---|
112 | @NonNull
|
---|
113 | List<@NonNull ParetoPoint> newlist = new LinkedList<ParetoPoint>();
|
---|
114 | newlist.add(candidate);
|
---|
115 | for (@NonNull
|
---|
116 | ParetoPoint existing : list) {
|
---|
117 | if (!existing.isDominatedBy(candidate)) {
|
---|
118 | newlist.add(existing);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | return newlist;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|