Changeset 4 for bidspace/src/test/java/geniusweb
- Timestamp:
- 09/18/19 10:00:22 (5 years ago)
- Location:
- bidspace/src/test/java/geniusweb/bidspace
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
bidspace/src/test/java/geniusweb/bidspace/BidsWithUtilityTest.java
r2 r4 1 1 package geniusweb.bidspace; 2 2 3 import static org.junit.Assert.assertEquals; 4 import static org.mockito.Matchers.any; 5 import static org.mockito.Mockito.mock; 6 import static org.mockito.Mockito.when; 3 import static org.junit.Assert.assertTrue; 7 4 8 5 import java.io.IOException; 9 6 import java.math.BigDecimal; 10 7 import java.math.BigInteger; 8 import java.math.RoundingMode; 11 9 import java.nio.file.Files; 12 10 import java.nio.file.Paths; 11 import java.util.Arrays; 13 12 import java.util.Collection; 14 import java.util.HashMap; 15 import java.util.LinkedList; 16 import java.util.Map; 13 import java.util.Random; 17 14 18 import org.junit.Before;19 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.Parameterized; 18 import org.junit.runners.Parameterized.Parameters; 20 19 21 20 import com.fasterxml.jackson.databind.ObjectMapper; 22 21 23 22 import geniusweb.issuevalue.Bid; 24 import geniusweb.issuevalue.DiscreteValue;25 import geniusweb.issuevalue.DiscreteValueSet;26 import geniusweb.issuevalue.Domain;27 import geniusweb.issuevalue.NumberValue;28 import geniusweb.issuevalue.NumberValueSet;29 import geniusweb.issuevalue.ValueSet;30 23 import geniusweb.profile.Profile; 31 24 import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace; 25 import tudelft.utilities.immutablelist.ImmutableList; 32 26 27 @RunWith(Parameterized.class) 33 28 public class BidsWithUtilityTest { 34 private static final DiscreteValue I1V2 = new DiscreteValue("i1v2"); 35 private static final DiscreteValue I1V1 = new DiscreteValue("i1v1"); 36 private static final NumberValue I2V1 = new NumberValue("2.00"); 37 private static final NumberValue I2V2 = new NumberValue("2.45"); 38 private static final NumberValue I2V3 = new NumberValue("2.90"); 39 private static final String DOMAINNAME = "testdomain"; 40 private static final String ISSUE1 = "issue1"; 41 private static final String ISSUE2 = "issue2"; 42 private static final Map<String, ValueSet> issues = new HashMap<>(); 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; 43 35 44 private static ValueSet values1; 45 private static ValueSet values2; 46 private static final BigDecimal TWO = new BigDecimal("2"); 47 private static final BigDecimal THREE = new BigDecimal("3"); 48 private final static BigDecimal ZERO_1 = new BigDecimal("0.1"), ZERO_2 = new BigDecimal("0.2"), 49 ZERO_3 = new BigDecimal("0.3"); 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 } 50 43 51 private static Domain domain; 52 private final static ObjectMapper jackson = new ObjectMapper(); 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 } 53 51 54 @Before 55 public void before() { 56 Collection<DiscreteValue> discretevalues1 = new LinkedList<>(); 57 discretevalues1.add(I1V1); 58 discretevalues1.add(I1V2); 59 values1 = new DiscreteValueSet(discretevalues1); 60 issues.put(ISSUE1, values1); 61 62 values2 = new NumberValueSet(TWO, THREE, new BigDecimal("0.45")); 63 issues.put(ISSUE2, values2); 64 65 domain = new Domain(DOMAINNAME, issues); 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); 66 72 67 73 } 68 74 69 75 @Test 70 public void smokeTest() { 71 LinearAdditiveUtilitySpace space = mock(LinearAdditiveUtilitySpace.class); 72 when(space.getDomain()).thenReturn(domain); 73 when(space.getUtility(any(Bid.class))).thenReturn(ZERO_2); 74 new BidsWithUtility(space, new BigDecimal("0.1"), new BigDecimal("0.3")); 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); 75 97 } 76 98 77 99 @Test 78 public void testSelection() { 79 LinearAdditiveUtilitySpace space = mock(LinearAdditiveUtilitySpace.class); 100 public void benchmark() { 101 System.out.println("\nBenchmarking " + profile.getName()); 102 BigInteger domainsize = new AllBidsList(profile.getDomain()).size(); 80 103 81 when(space.getDomain()).thenReturn(domain); 82 when(space.getUtility(any(Bid.class))).thenReturn(ZERO_2); 83 BidsWithUtility selected = new BidsWithUtility(space, new BigDecimal("0.1"), new BigDecimal("0.3")); 84 assertEquals(new BigInteger("6"), selected.size()); 85 } 86 87 @Test 88 public void testRealSpace() throws IOException { 89 String file1 = new String(Files.readAllBytes(Paths.get("src/test/resources/jobs/jobs1.json"))); 90 LinearAdditiveUtilitySpace profile = (LinearAdditiveUtilitySpace) jackson.readValue(file1, Profile.class); 91 BidsWithUtility selected = new BidsWithUtility(profile, ZERO_1, ZERO_3); 92 93 assertEquals(new BigInteger("56"), selected.size()); 94 for (Bid bid : selected) { 95 assertEquals(1, profile.getUtility(bid).compareTo(ZERO_1)); 96 assertEquals(-1, profile.getUtility(bid).compareTo(ZERO_3)); 97 } 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()); 98 111 } 99 112
Note:
See TracChangeset
for help on using the changeset viewer.