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

Last change on this file since 27 was 27, checked in by bart, 4 years ago
  • party capabilities now include profile information. Existing parties may need small fix * plot layout shows accepts * VotingEvaluator use group power instead of group size * runsession you can now also select MOPAC-only parties
File size: 3.7 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
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}
Note: See TracBrowser for help on using the repository browser.