source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/bidspace/pareto/PartialPareto.java@ 837

Last change on this file since 837 was 825, checked in by wouter, 6 months ago

#291 move annotation to above the javadoc

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