source: bidspace/src/test/java/geniusweb/bidspace/BidsWithUtilityTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 4.8 KB
Line 
1package geniusweb.bidspace;
2
3import static org.junit.Assert.assertTrue;
4
5import java.io.IOException;
6import java.math.BigDecimal;
7import java.math.BigInteger;
8import java.math.RoundingMode;
9import java.nio.file.Files;
10import java.nio.file.Paths;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.Random;
14
15import org.junit.Test;
16import org.junit.runner.RunWith;
17import org.junit.runners.Parameterized;
18import org.junit.runners.Parameterized.Parameters;
19
20import com.fasterxml.jackson.databind.ObjectMapper;
21
22import geniusweb.issuevalue.Bid;
23import geniusweb.profile.Profile;
24import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
25import tudelft.utilities.immutablelist.ImmutableList;
26
27@RunWith(Parameterized.class)
28public class BidsWithUtilityTest {
29 private final static ObjectMapper jackson = new ObjectMapper();
30 private final Interval utilityGoal = new Interval(new BigDecimal("0.50"),
31 new BigDecimal("0.51"));
32 private LinearAdditiveUtilitySpace profile;
33 private long expectedSize;
34 private int accuracy = 5;
35 private final static Runtime r = Runtime.getRuntime();
36
37 @Parameters
38 public static Collection<Object[]> data() {
39 return Arrays.asList(new Object[][] {
40 { "src/test/resources/jobs/jobs1.json", 11 },
41 { "src/test/resources/7issues/7issues1.json", 260000 },
42 { "src/test/resources/9issues/9issues1.json", 25000000 } });
43 }
44
45 public BidsWithUtilityTest(String filename, long expectedsize)
46 throws IOException {
47 String file = new String(Files.readAllBytes(Paths.get(filename)));
48 profile = (LinearAdditiveUtilitySpace) jackson.readValue(file,
49 Profile.class);
50 this.expectedSize = expectedsize;
51 }
52
53 /**
54 * Test if the values are within acceptable range from the goal.
55 */
56 @Test
57 public void test() {
58 ImmutableList<Bid> list = new BidsWithUtility(profile, accuracy)
59 .getBids(utilityGoal);
60 Random rand = new Random();
61 // check not all but only 10000 random bids as list may be way too large
62 // to test them all. Testing 1 billion bids may take 15 minutes or so on
63 // quad core i7 @2.4GHz.....
64 // also notice that we may be checking only part of the list if
65 // the size of the list would become bigger than maxint.
66 for (int n = 0; n < 10000; n++) {
67 Bid bid = list.get(rand.nextInt(list.size().intValue()));
68 assertTrue(utilityGoal.contains(profile.getUtility(bid)
69 .setScale(accuracy - 1, RoundingMode.HALF_UP)));
70 }
71 assertTrue(
72 list.size().compareTo(BigInteger.valueOf(expectedSize)) >= 0);
73
74 }
75
76 @Test
77 public void testMaxUtil() {
78 BidsWithUtility bidswithutil = new BidsWithUtility(profile);
79 // notice, this is the *rounded* max util
80 BigDecimal maxutil = bidswithutil.getRange().getMax();
81 Interval goal = new Interval(
82 maxutil.subtract(new BigDecimal("0.00001")), maxutil);
83
84 ImmutableList<Bid> bidsnearmax = bidswithutil.getBids(goal);
85 assertTrue(bidsnearmax.size() != BigInteger.ZERO);
86
87 BigDecimal foundmax = BigDecimal.ZERO;
88 for (Bid bid : bidsnearmax) {
89 BigDecimal util = profile.getUtility(bid);
90 if (util.compareTo(foundmax) > 0) {
91 foundmax = util;
92 }
93 }
94 // found maximum may be slightly lower or higher because we rounded
95 // all weighted utilities.
96 assertTrue(Math
97 .abs(foundmax.doubleValue() - maxutil.doubleValue()) < 0.0001);
98 }
99
100 @Test
101 public void benchmark() {
102 System.out.println("\nBenchmarking " + profile.getName());
103 BigInteger domainsize = new AllBidsList(profile.getDomain()).size();
104
105 long presize = usedMemory();
106 long start = System.currentTimeMillis();
107 BidsWithUtility body = new BidsWithUtility(profile, accuracy);
108 ImmutableList<Bid> list = body.getBids(utilityGoal);
109 long end = System.currentTimeMillis();
110 long postsize = usedMemory();
111 System.out.println("run time: " + (end - start) / 1000. + "s");
112 System.out.println("Total size of complete domain:" + domainsize);
113 System.out.println("size of selection: " + list.size());
114 System.out.println("Memory use of selection(MB):"
115 + (double) (postsize - presize) / (1024 * 1024));
116 }
117
118 /**
119 * see https://www.geeksforgeeks.org/java-lang-runtime-class-in-java/
120 *
121 * @return currently used memory after doing GC.
122 *
123 */
124 public static long usedMemory() {
125 for (int n = 0; n < 4; n++)
126 gc1(); // repeat as gct seems getting more agressive then
127 return used();
128 }
129
130 /**
131 *
132 * @return used heap mem. Does NOT GC. Uses {@link Runtime#totalMemory()}
133 * and {@link Runtime#freeMemory()} to avoid problems when JVM gets
134 * more system memory which can happen at any time.
135 */
136 private static long used() {
137 return r.totalMemory() - r.freeMemory();
138 }
139
140 /**
141 * repeat garbage collection till heap memory gets stable
142 */
143 private static void gc1() {
144 long usedMem1 = used(), usedMem2 = Long.MAX_VALUE;
145 for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
146 r.runFinalization();
147 r.gc();
148 Thread.currentThread().yield();
149
150 usedMem2 = usedMem1;
151 usedMem1 = used();
152 }
153 }
154}
Note: See TracBrowser for help on using the repository browser.