1 | package geniusweb.bidspace;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertTrue;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.math.BigDecimal;
|
---|
7 | import java.math.BigInteger;
|
---|
8 | import java.math.RoundingMode;
|
---|
9 | import java.nio.file.Files;
|
---|
10 | import java.nio.file.Paths;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Random;
|
---|
14 |
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.junit.runner.RunWith;
|
---|
17 | import org.junit.runners.Parameterized;
|
---|
18 | import org.junit.runners.Parameterized.Parameters;
|
---|
19 |
|
---|
20 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
21 |
|
---|
22 | import geniusweb.issuevalue.Bid;
|
---|
23 | import geniusweb.profile.Profile;
|
---|
24 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
25 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
26 |
|
---|
27 | @RunWith(Parameterized.class)
|
---|
28 | public 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 |
|
---|
36 | @Parameters
|
---|
37 | public static Collection<Object[]> data() {
|
---|
38 | return Arrays.asList(new Object[][] {
|
---|
39 | { "src/test/resources/jobs/jobs1.json", 11 },
|
---|
40 | { "src/test/resources/7issues/7issues1.json", 260000 },
|
---|
41 | { "src/test/resources/9issues/9issues1.json", 25000000 } });
|
---|
42 | }
|
---|
43 |
|
---|
44 | public BidsWithUtilityTest(String filename, long expectedsize)
|
---|
45 | throws IOException {
|
---|
46 | String file = new String(Files.readAllBytes(Paths.get(filename)));
|
---|
47 | profile = (LinearAdditiveUtilitySpace) jackson.readValue(file,
|
---|
48 | Profile.class);
|
---|
49 | this.expectedSize = expectedsize;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Test if the values are within acceptable range from the goal.
|
---|
54 | */
|
---|
55 | @Test
|
---|
56 | public void test() {
|
---|
57 | ImmutableList<Bid> list = new BidsWithUtility(profile, accuracy)
|
---|
58 | .getBids(utilityGoal);
|
---|
59 | Random rand = new Random();
|
---|
60 | // check not all but only 10000 random bids as list may be way too large
|
---|
61 | // to test them all. Testing 1 billion bids may take 15 minutes or so on
|
---|
62 | // quad core i7 @2.4GHz.....
|
---|
63 | // also notice that we may be checking only part of the list if
|
---|
64 | // the size of the list would become bigger than maxint.
|
---|
65 | for (int n = 0; n < 10000; n++) {
|
---|
66 | Bid bid = list.get(rand.nextInt(list.size().intValue()));
|
---|
67 | assertTrue(utilityGoal.contains(profile.getUtility(bid)
|
---|
68 | .setScale(accuracy - 1, RoundingMode.HALF_UP)));
|
---|
69 | }
|
---|
70 | assertTrue(
|
---|
71 | list.size().compareTo(BigInteger.valueOf(expectedSize)) >= 0);
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Test
|
---|
76 | public void testMaxUtil() {
|
---|
77 | BidsWithUtility bidswithutil = new BidsWithUtility(profile);
|
---|
78 | // notice, this is the *rounded* max util
|
---|
79 | BigDecimal maxutil = bidswithutil.getRange().getMax();
|
---|
80 | Interval goal = new Interval(
|
---|
81 | maxutil.subtract(new BigDecimal("0.00001")), maxutil);
|
---|
82 |
|
---|
83 | ImmutableList<Bid> bidsnearmax = bidswithutil.getBids(goal);
|
---|
84 | assertTrue(bidsnearmax.size() != BigInteger.ZERO);
|
---|
85 |
|
---|
86 | BigDecimal foundmax = BigDecimal.ZERO;
|
---|
87 | for (Bid bid : bidsnearmax) {
|
---|
88 | BigDecimal util = profile.getUtility(bid);
|
---|
89 | if (util.compareTo(foundmax) > 0) {
|
---|
90 | foundmax = util;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | // found maximum may be slightly lower or higher because we rounded
|
---|
94 | // all weighted utilities.
|
---|
95 | assertTrue(Math
|
---|
96 | .abs(foundmax.doubleValue() - maxutil.doubleValue()) < 0.0001);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void benchmark() {
|
---|
101 | System.out.println("\nBenchmarking " + profile.getName());
|
---|
102 | BigInteger domainsize = new AllBidsList(profile.getDomain()).size();
|
---|
103 |
|
---|
104 | long start = System.currentTimeMillis();
|
---|
105 | BidsWithUtility body = new BidsWithUtility(profile, accuracy);
|
---|
106 | ImmutableList<Bid> list = body.getBids(utilityGoal);
|
---|
107 | long end = System.currentTimeMillis();
|
---|
108 | System.out.println("run time: " + (end - start) / 1000. + "s");
|
---|
109 | System.out.println("Total size of bidspace:" + domainsize);
|
---|
110 | System.out.println("Result size: " + list.size());
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|