Repository: bigtop Updated Branches: refs/heads/master b5c3f9119 -> 259fc42c9
BIGTOP-1931. Add multinomial product purchasing model to BPS Data Generator Project: http://git-wip-us.apache.org/repos/asf/bigtop/repo Commit: http://git-wip-us.apache.org/repos/asf/bigtop/commit/259fc42c Tree: http://git-wip-us.apache.org/repos/asf/bigtop/tree/259fc42c Diff: http://git-wip-us.apache.org/repos/asf/bigtop/diff/259fc42c Branch: refs/heads/master Commit: 259fc42c919acfe96490a57dadeabf3549091f5e Parents: b5c3f91 Author: RJ Nowling <[email protected]> Authored: Sun Jul 12 12:12:24 2015 -0500 Committer: RJ Nowling <[email protected]> Committed: Sun Jul 12 12:12:24 2015 -0500 ---------------------------------------------------------------------- .../bigpetstore/datagenerator/Constants.java | 32 ++--- .../bigpetstore/datagenerator/cli/Driver.java | 117 +++++++++++++--- .../datagenerator/cli/Simulation.java | 14 +- .../datagenerator/datamodels/Pair.java | 20 +++ .../framework/markovmodels/MarkovModel.java | 9 ++ .../framework/markovmodels/MarkovProcess.java | 1 - .../framework/pdfs/DiscretePDF.java | 6 + .../MarkovModelProductCategorySampler.java | 119 +++++++++++++++++ .../purchase/MarkovPurchasingModel.java | 3 +- .../purchase/MultinomialPurchasingModel.java | 67 ++++++++++ .../MultinomialPurchasingModelSampler.java | 132 +++++++++++++++++++ .../ProductCategoryMarkovModelSampler.java | 119 ----------------- .../purchase/ProductCategoryPDFSampler.java | 117 ---------------- .../generators/purchase/PurchasingModel.java | 4 +- .../purchase/PurchasingModelSamplerBuilder.java | 50 +------ .../purchase/StaticPurchasingModel.java | 61 --------- .../purchase/StaticPurchasingModelSampler.java | 49 ------- .../TestProductCategoryMarkovModelSampler.java | 4 +- 18 files changed, 486 insertions(+), 438 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/Constants.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/Constants.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/Constants.java index f43cf92..c719054 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/Constants.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/Constants.java @@ -26,14 +26,8 @@ public class Constants { public static enum PurchasingModelType { - STATIC, - DYNAMIC; - } - - public static enum DistributionType - { - BOUNDED_MULTIMODAL_GAUSSIAN, - EXPONENTIAL; + MULTINOMIAL, + MARKOV; } public static final File COORDINATES_FILE = new File("zips.csv"); @@ -60,7 +54,7 @@ public class Constants public static final double AVERAGE_CUSTOMER_STORE_DISTANCE = 5.0; // miles - public static final PurchasingModelType PURCHASING_MODEL_TYPE = PurchasingModelType.DYNAMIC; + public static final PurchasingModelType PURCHASING_MODEL_TYPE = PurchasingModelType.MULTINOMIAL; public static final List<Pair<Double, Double>> PRODUCT_MSM_FIELD_WEIGHT_GAUSSIANS = ImmutableList.of(Pair.create(0.15, 0.1), Pair.create(0.85, 0.1)); public static final double PRODUCT_MSM_FIELD_WEIGHT_LOWERBOUND = 0.05; @@ -74,20 +68,12 @@ public class Constants public static final double PRODUCT_MSM_LOOPBACK_WEIGHT_LOWERBOUND = 0.05; public static final double PRODUCT_MSM_LOOPBACK_WEIGHT_UPPERBOUND = 0.95; - public static final DistributionType STATIC_PURCHASING_MODEL_FIELD_WEIGHT_DISTRIBUTION_TYPE = DistributionType.BOUNDED_MULTIMODAL_GAUSSIAN; - public static final DistributionType STATIC_PURCHASING_MODEL_FIELD_VALUE_WEIGHT_DISTRIBUTION_TYPE = DistributionType.EXPONENTIAL; - - public static final List<Pair<Double, Double>> STATIC_FIELD_WEIGHT_GAUSSIANS = ImmutableList.of(Pair.create(0.15, 0.1), Pair.create(0.85, 0.1)); - public static final double STATIC_FIELD_WEIGHT_LOWERBOUND = 0.05; - public static final double STATIC_FIELD_WEIGHT_UPPERBOUND = 0.95; - - public static final List<Pair<Double, Double>> STATIC_FIELD_VALUE_WEIGHT_GAUSSIANS = ImmutableList.of(Pair.create(0.15, 0.1), Pair.create(0.85, 0.1)); - public static final double STATIC_FIELD_VALUE_WEIGHT_LOWERBOUND = 0.05; - public static final double STATIC_FIELD_VALUE_WEIGHT_UPPERBOUND = 0.95; - - public static final double STATIC_FIELD_WEIGHT_EXPONENTIAL = 0.25; - public static final double STATIC_FIELD_VALUE_WEIGHT_EXPONENTIAL = 2.0; - + public static final double PRODUCT_MULTINOMIAL_HIGH_WEIGHT = 10.0; + public static final double PRODUCT_MULTINOMIAL_NEUTRAL_WEIGHT = 1.0; + public static final double PRODUCT_MULTINOMIAL_LOW_WEIGHT = 0.1; + public static final double PRODUCT_MULTINOMIAL_MIN_PERCENT = 0.01; // 1% + public static final int PRODUCT_MULTINOMIAL_MIN_COUNT = 2; + public static final double PRODUCT_MULTINOMIAL_MAX_PERCENT = 0.1; // 10% public static final String PRODUCT_QUANTITY = "quantity"; public static final String PRODUCT_CATEGORY = "category"; http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Driver.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Driver.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Driver.java index 6dc3b59..435b620 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Driver.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Driver.java @@ -20,13 +20,17 @@ import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Collection; -import java.util.Random; +import java.util.List; import org.apache.bigtop.bigpetstore.datagenerator.DataLoader; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Customer; import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Pair; import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Store; import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Transaction; import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.InputData; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; +import org.apache.bigtop.bigpetstore.datagenerator.generators.purchase.PurchasingModel; public class Driver @@ -44,7 +48,7 @@ public class Driver { String usage = "BigPetStore Data Generator\n" + "\n" + - "Usage: java -jar bps-data-generator-v0.2.java outputDir nStores nCustomers nPurchasingModels simulationLength [seed]\n" + + "Usage: java -jar bps-data-generator-v0.2.java outputDir nStores nCustomers nPurchasingModels simulationLength seed\n" + "\n" + "outputDir - (string) directory to write files\n" + "nStores - (int) number of stores to generate\n" + @@ -125,22 +129,15 @@ public class Driver System.exit(1); } - if(args.length == NPARAMS) + try { - try - { - seed = Long.parseLong(args[++i]); - } - catch(Exception e) - { - System.err.println("Unable to parse '" + args[i] + "' as a long for the seed.\n"); - printUsage(); - System.exit(1); - } + seed = Long.parseLong(args[++i]); } - else + catch(Exception e) { - seed = (new Random()).nextLong(); + System.err.println("Unable to parse '" + args[i] + "' as a long for the seed.\n"); + printUsage(); + System.exit(1); } } @@ -175,6 +172,92 @@ public class Driver outputStream.close(); } + private void writeCustomers(Collection<Customer> customers) throws Exception + { + File outputFile = new File(outputDir.toString() + File.separator + "customers.txt"); + System.out.println(outputFile.toString()); + OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); + + for(Customer customer : customers) + { + String record = customer.getId() + ","; + Pair<String, String> name = customer.getName(); + record += name.getFirst() + "," + name.getSecond() + ","; + record += customer.getLocation().getZipcode() + ","; + record += customer.getLocation().getCity() + ","; + record += customer.getLocation().getState() + "\n"; + + outputStream.write(record.getBytes()); + } + + outputStream.close(); + } + + private void writeStores(Collection<Store> stores) throws Exception + { + File outputFile = new File(outputDir.toString() + File.separator + "stores.txt"); + System.out.println(outputFile.toString()); + OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); + + for(Store store : stores) + { + String record = store.getId() + ","; + record += store.getLocation().getZipcode() + ","; + record += store.getLocation().getCity() + ","; + record += store.getLocation().getState() + "\n"; + + outputStream.write(record.getBytes()); + } + + outputStream.close(); + } + + private void writeProducts(Collection<ProductCategory> productCategories) throws Exception + { + File outputFile = new File(outputDir.toString() + File.separator + "products.txt"); + System.out.println(outputFile.toString()); + OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); + + for(ProductCategory category : productCategories) + { + + for(Product product : category.getProducts()) + { + String record = category.getCategoryLabel() + ","; + record += product.toString() + "\n"; + + outputStream.write(record.getBytes()); + } + } + + outputStream.close(); + } + + private void writePurchasingProfiles(List<ProductCategory> productCategories, List<PurchasingModel> profiles) throws Exception + { + File outputFile = new File(outputDir.toString() + File.separator + "purchasing_profiles.txt"); + System.out.println(outputFile.toString()); + OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); + + for(ProductCategory category : productCategories) + { + int i = 0; + for(PurchasingModel model : profiles) + { + Object productModel = model.getProfile(category.getCategoryLabel()); + String record = category.getCategoryLabel() + ","; + record += i + ","; + record += productModel.toString() + "\n"; + + outputStream.write(record.getBytes()); + + i += 1; + } + } + + outputStream.close(); + } + public Simulation buildSimulation(InputData inputData) { return new Simulation(inputData, nStores, nCustomers, nPurchasingModels, simulationTime, seed); @@ -186,6 +269,10 @@ public class Driver simulation.simulate(); + writeStores(simulation.getStores()); + writeCustomers(simulation.getCustomers()); + writeProducts(simulation.getProductCategories()); + writePurchasingProfiles(simulation.getProductCategories(), simulation.getPurchasingProfiles()); writeTransactions(simulation.getTransactions()); } public void run(String[] args) throws Exception http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Simulation.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Simulation.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Simulation.java index f8bf18f..4b9b500 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Simulation.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/cli/Simulation.java @@ -15,7 +15,6 @@ */ package org.apache.bigtop.bigpetstore.datagenerator.cli; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Vector; @@ -50,6 +49,7 @@ public class Simulation List<Store> stores; List<Customer> customers; Sampler<PurchasingModel> purchasingModelSampler; + List<PurchasingModel> purchasingProfiles; List<Transaction> transactions; List<ProductCategory> productCategories; @@ -109,7 +109,7 @@ public class Simulation System.out.println("Generating purchasing profiles"); PurchasingModelGenerator generator = new PurchasingModelGenerator(productCategories, seedFactory); - Collection<PurchasingModel> purchasingProfiles = new Vector<PurchasingModel>(); + purchasingProfiles = new Vector<PurchasingModel>(); for(int i = 0; i < nPurchasingModels; i++) { PurchasingModel profile = generator.generate(); @@ -175,4 +175,14 @@ public class Simulation { return inputData; } + + public List<ProductCategory> getProductCategories() + { + return this.productCategories; + } + + public List<PurchasingModel> getPurchasingProfiles() + { + return this.purchasingProfiles; + } } http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/datamodels/Pair.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/datamodels/Pair.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/datamodels/Pair.java index 9f3053b..c9e7361 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/datamodels/Pair.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/datamodels/Pair.java @@ -57,6 +57,26 @@ public class Pair<A, B> implements Serializable return list; } + @Override + public boolean equals(Object o) + { + if(!this.getClass().isInstance(o)) + { + return false; + } + + Pair other = (Pair) o; + + return other.getFirst().equals(first) && + other.getSecond().equals(second); + } + + @Override + public int hashCode() + { + return toString().hashCode(); + } + public String toString() { return "Pair(" + first + ", " + second + ")"; http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovModel.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovModel.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovModel.java index cf2a40d..0b90e2b 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovModel.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovModel.java @@ -20,6 +20,8 @@ import java.util.Map; public class MarkovModel<T> implements Serializable { + private static final long serialVersionUID = 8378109656005603192L; + final Map<T, Map<T, Double>> transitionWeights; final Map<T, Double> startWeights; @@ -38,4 +40,11 @@ public class MarkovModel<T> implements Serializable { return startWeights; } + + @Override + public String toString() + { + return "MarkModel(" + startWeights + "," + transitionWeights + ")"; + } + } http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovProcess.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovProcess.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovProcess.java index 2a72e65..d0bd6c5 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovProcess.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/markovmodels/MarkovProcess.java @@ -22,7 +22,6 @@ import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.RouletteWh import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableTable; public class MarkovProcess<T> implements Sampler<T> { http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/pdfs/DiscretePDF.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/pdfs/DiscretePDF.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/pdfs/DiscretePDF.java index 9d0d6f2..190aca3 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/pdfs/DiscretePDF.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/framework/pdfs/DiscretePDF.java @@ -43,4 +43,10 @@ public class DiscretePDF<T> implements ProbabilityDensityFunction<T> return 0.0; } + + @Override + public String toString() + { + return "DiscretePDF(" + probabilities.toString() +")"; + } } http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovModelProductCategorySampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovModelProductCategorySampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovModelProductCategorySampler.java new file mode 100644 index 0000000..c842ff1 --- /dev/null +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovModelProductCategorySampler.java @@ -0,0 +1,119 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; + +import java.util.Map; + +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; +import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.MarkovModel; +import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.MarkovModelBuilder; +import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; + +import com.google.common.collect.Maps; + +public class MarkovModelProductCategorySampler implements Sampler<MarkovModel<Product>> +{ + final ProductCategory productCategory; + final Sampler<Double> fieldSimilarityWeightSampler; + final Sampler<Double> loopbackWeightSampler; + + final Map<String, Double> fieldWeights; + Map<String, Double> fieldSimilarityWeights; + double loopbackWeight; + + public MarkovModelProductCategorySampler(ProductCategory productCategory, + Map<String, Double> fieldWeights, Sampler<Double> fieldSimilarityWeightSampler, + Sampler<Double> loopbackWeightSampler) + { + this.productCategory = productCategory; + + this.fieldSimilarityWeightSampler = fieldSimilarityWeightSampler; + this.fieldWeights = fieldWeights; + this.loopbackWeightSampler = loopbackWeightSampler; + } + + protected void generateWeights() throws Exception + { + fieldSimilarityWeights = Maps.newHashMap(); + + for(String fieldName : productCategory.getFieldNames()) + { + fieldSimilarityWeights.put(fieldName,fieldSimilarityWeightSampler.sample()); + } + + loopbackWeight = loopbackWeightSampler.sample(); + } + + protected double productPairWeight(Product product1, Product product2) + { + double weightSum = 0.0; + for(String fieldName : productCategory.getFieldNames()) + { + double fieldWeight = this.fieldWeights.get(fieldName); + + if(product1.getFieldValue(fieldName).equals(product2.getFieldValue(fieldName))) + { + fieldWeight *= this.fieldSimilarityWeights.get(fieldName); + } + else + { + fieldWeight *= (1.0 - this.fieldSimilarityWeights.get(fieldName)); + } + + weightSum += fieldWeight; + } + return weightSum; + } + + public MarkovModel<Product> sample() throws Exception + { + generateWeights(); + + MarkovModelBuilder<Product> builder = new MarkovModelBuilder<Product>(); + + for(Product product1 : productCategory.getProducts()) + { + builder.addStartState(product1, 1.0); + + double weightSum = 0.0; + for(Product product2 : productCategory.getProducts()) + { + if(!product1.equals(product2)) + { + weightSum += productPairWeight(product1, product2); + } + } + + for(Product product2 : productCategory.getProducts()) + { + double weight = 0.0; + if(!product1.equals(product2)) + { + weight = (1.0 - loopbackWeight) * productPairWeight(product1, product2) / weightSum; + } + else + { weight = loopbackWeight; + + } + + builder.addTransition(product1, product2, weight); + } + } + + return builder.build(); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovPurchasingModel.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovPurchasingModel.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovPurchasingModel.java index cae8794..8b22660 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovPurchasingModel.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MarkovPurchasingModel.java @@ -27,7 +27,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; -public class MarkovPurchasingModel implements PurchasingModel +public class MarkovPurchasingModel implements PurchasingModel<MarkovModel<Product>> { private static final long serialVersionUID = 3098355461347511619L; @@ -44,6 +44,7 @@ public class MarkovPurchasingModel implements PurchasingModel return productCategoryProfiles.keySet(); } + @Override public MarkovModel<Product> getProfile(String productCategory) { return productCategoryProfiles.get(productCategory); http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModel.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModel.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModel.java new file mode 100644 index 0000000..1adb5bd --- /dev/null +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModel.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; + +import java.util.Map; + +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; +import org.apache.bigtop.bigpetstore.datagenerator.framework.SeedFactory; +import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; +import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.RouletteWheelSampler; +import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +public class MultinomialPurchasingModel implements PurchasingModel<DiscretePDF<Product>> +{ + + private static final long serialVersionUID = 5863830733003282570L; + + private final ImmutableMap<String, DiscretePDF<Product>> productPDFs; + + public MultinomialPurchasingModel(Map<String, DiscretePDF<Product>> productPDFs) + { + this.productPDFs = ImmutableMap.copyOf(productPDFs); + } + + @Override + public ImmutableSet<String> getProductCategories() + { + return productPDFs.keySet(); + } + + @Override + public DiscretePDF<Product> getProfile(String category) + { + return productPDFs.get(category); + } + + @Override + public PurchasingProcesses buildProcesses(SeedFactory seedFactory) + { + Map<String, Sampler<Product>> processes = Maps.newHashMap(); + for(String category : getProductCategories()) + { + DiscretePDF<Product> pdf = productPDFs.get(category); + processes.put(category, RouletteWheelSampler.create(pdf, seedFactory)); + } + + return new PurchasingProcesses(processes); + } + +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModelSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModelSampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModelSampler.java new file mode 100644 index 0000000..55fef69 --- /dev/null +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/MultinomialPurchasingModelSampler.java @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.Vector; + +import org.apache.bigtop.bigpetstore.datagenerator.Constants; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Pair; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; +import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; +import org.apache.bigtop.bigpetstore.datagenerator.framework.SeedFactory; +import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; +import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; +import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.UniformIntSampler; + +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +public class MultinomialPurchasingModelSampler implements Sampler<MultinomialPurchasingModel> +{ + private final SeedFactory seedFactory; + private final Collection<ProductCategory> productCategories; + + public MultinomialPurchasingModelSampler(Collection<ProductCategory> productCategories, SeedFactory seedFactory) + { + this.seedFactory = seedFactory; + this.productCategories = productCategories; + } + + protected void shuffle(Vector<Pair<String, Object>> input) throws Exception + { + for(int i = 0; i < input.size() - 1; i++) + { + int swapIdx = new UniformIntSampler(i, input.size() - 1, seedFactory).sample(); + Pair<String, Object> tmp = input.get(i); + input.set(i, input.get(swapIdx)); + input.set(swapIdx, tmp); + } + } + + protected Map<Pair<String, Object>, Double> generateFieldValueWeights(ProductCategory productCategory) throws Exception + { + Set<Pair<String, Object>> fieldValuesSet = Sets.newHashSet(); + for(String fieldName : productCategory.getFieldNames()) + { + for(Product p : productCategory.getProducts()) + { + Object fieldValue = p.getFieldValue(fieldName); + fieldValuesSet.add(Pair.create(fieldName, fieldValue)); + } + } + + Vector<Pair<String, Object>> fieldValues = new Vector<Pair<String, Object>>(fieldValuesSet); + shuffle(fieldValues); + + int lowerbound = Math.max(Constants.PRODUCT_MULTINOMIAL_MIN_COUNT, + (int) Math.ceil(fieldValues.size() * Constants.PRODUCT_MULTINOMIAL_MIN_PERCENT)); + int upperbound = Math.max(Constants.PRODUCT_MULTINOMIAL_MIN_COUNT + 1, + (int) Math.floor(fieldValues.size() * Constants.PRODUCT_MULTINOMIAL_MAX_PERCENT)); + + Sampler<Integer> countSampler = new UniformIntSampler(lowerbound, upperbound, seedFactory); + int highWeightCount = countSampler.sample(); + int lowWeightCount = countSampler.sample(); + + Map<Pair<String, Object>, Double> fieldValueWeights = Maps.newHashMap(); + for(int i = 0; i < fieldValues.size(); i++) + { + if(i < highWeightCount) + { + fieldValueWeights.put(fieldValues.get(i), Constants.PRODUCT_MULTINOMIAL_HIGH_WEIGHT); + } + else if(i >= highWeightCount && i < (lowWeightCount + highWeightCount)) + { + fieldValueWeights.put(fieldValues.get(i), Constants.PRODUCT_MULTINOMIAL_LOW_WEIGHT); + } + else + { + fieldValueWeights.put(fieldValues.get(i), Constants.PRODUCT_MULTINOMIAL_NEUTRAL_WEIGHT); + } + } + + return fieldValueWeights; + } + + protected Map<Product, Double> generateProductWeights(Map<Pair<String, Object>, Double> fieldValueWeights, + ProductCategory productCategory) throws Exception + { + Map<Product, Double> productWeights = Maps.newHashMap(); + for(Product p : productCategory.getProducts()) + { + double weight = 1.0; + for(String fieldName : productCategory.getFieldNames()) + { + Object fieldValue = p.getFieldValue(fieldName); + Pair<String, Object> key = Pair.create(fieldName, fieldValue); + weight *= fieldValueWeights.get(key); + } + productWeights.put(p, weight); + } + + return productWeights; + } + + public MultinomialPurchasingModel sample() throws Exception + { + Map<String, DiscretePDF<Product>> pdfs = Maps.newHashMap(); + for(ProductCategory productCategory : productCategories) + { + Map<Pair<String, Object>, Double> fieldWeights = this.generateFieldValueWeights(productCategory); + Map<Product, Double> productWeights = this.generateProductWeights(fieldWeights, productCategory); + pdfs.put(productCategory.getCategoryLabel(), new DiscretePDF<Product>(productWeights)); + } + + return new MultinomialPurchasingModel(pdfs); + } +} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryMarkovModelSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryMarkovModelSampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryMarkovModelSampler.java deleted file mode 100644 index 1cc35bf..0000000 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryMarkovModelSampler.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; - -import java.util.Map; - -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; -import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.MarkovModel; -import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.MarkovModelBuilder; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; - -import com.google.common.collect.Maps; - -public class ProductCategoryMarkovModelSampler implements Sampler<MarkovModel<Product>> -{ - final ProductCategory productCategory; - final Sampler<Double> fieldSimilarityWeightSampler; - final Sampler<Double> loopbackWeightSampler; - - final Map<String, Double> fieldWeights; - Map<String, Double> fieldSimilarityWeights; - double loopbackWeight; - - public ProductCategoryMarkovModelSampler(ProductCategory productCategory, - Map<String, Double> fieldWeights, Sampler<Double> fieldSimilarityWeightSampler, - Sampler<Double> loopbackWeightSampler) - { - this.productCategory = productCategory; - - this.fieldSimilarityWeightSampler = fieldSimilarityWeightSampler; - this.fieldWeights = fieldWeights; - this.loopbackWeightSampler = loopbackWeightSampler; - } - - protected void generateWeights() throws Exception - { - fieldSimilarityWeights = Maps.newHashMap(); - - for(String fieldName : productCategory.getFieldNames()) - { - fieldSimilarityWeights.put(fieldName,fieldSimilarityWeightSampler.sample()); - } - - loopbackWeight = loopbackWeightSampler.sample(); - } - - protected double productPairWeight(Product product1, Product product2) - { - double weightSum = 0.0; - for(String fieldName : productCategory.getFieldNames()) - { - double fieldWeight = this.fieldWeights.get(fieldName); - - if(product1.getFieldValue(fieldName).equals(product2.getFieldValue(fieldName))) - { - fieldWeight *= this.fieldSimilarityWeights.get(fieldName); - } - else - { - fieldWeight *= (1.0 - this.fieldSimilarityWeights.get(fieldName)); - } - - weightSum += fieldWeight; - } - return weightSum; - } - - public MarkovModel<Product> sample() throws Exception - { - generateWeights(); - - MarkovModelBuilder<Product> builder = new MarkovModelBuilder<Product>(); - - for(Product product1 : productCategory.getProducts()) - { - builder.addStartState(product1, 1.0); - - double weightSum = 0.0; - for(Product product2 : productCategory.getProducts()) - { - if(!product1.equals(product2)) - { - weightSum += productPairWeight(product1, product2); - } - } - - for(Product product2 : productCategory.getProducts()) - { - double weight = 0.0; - if(!product1.equals(product2)) - { - weight = (1.0 - loopbackWeight) * productPairWeight(product1, product2) / weightSum; - } - else - { weight = loopbackWeight; - - } - - builder.addTransition(product1, product2, weight); - } - } - - return builder.build(); - } -} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryPDFSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryPDFSampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryPDFSampler.java deleted file mode 100644 index b1d40d6..0000000 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/ProductCategoryPDFSampler.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; - -import java.util.Map; -import java.util.Set; - -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; -import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - -public class ProductCategoryPDFSampler implements Sampler<DiscretePDF<Product>> -{ - private final ProductCategory productCategory; - private final Sampler<Double> fieldValueWeightSampler; - private final Map<String, Double> fieldWeights; - - public ProductCategoryPDFSampler(ProductCategory productCategory, - Map<String, Double> fieldWeights, - Sampler<Double> fieldValueWeightSampler) - { - this.productCategory = productCategory; - this.fieldWeights = fieldWeights; - this.fieldValueWeightSampler = fieldValueWeightSampler; - } - - protected <T> Map<T, Double> normalize(Map<T, Double> weights) - { - double weightSum = 0.0; - for(double w : weights.values()) - { - weightSum += w; - } - - Map<T, Double> normalized = Maps.newHashMap(); - for(Map.Entry<T, Double> entry : weights.entrySet()) - { - normalized.put(entry.getKey(), entry.getValue() / weightSum); - } - - return normalized; - } - - protected Map<String, Map<Object, Double>> generateFieldValueWeights() throws Exception - { - Map<String, Set<Object>> allFieldValues = Maps.newHashMap(); - for(String fieldName : productCategory.getFieldNames()) - { - Set<Object> fieldValues = Sets.newHashSet(); - for(Product p : productCategory.getProducts()) - { - Object fieldValue = p.getFieldValue(fieldName); - fieldValues.add(fieldValue); - } - allFieldValues.put(fieldName, fieldValues); - } - - Map<String, Map<Object, Double>> allFieldValueWeights = Maps.newHashMap(); - for(String fieldName : productCategory.getFieldNames()) - { - Map<Object, Double> fieldValueWeights = Maps.newHashMap(); - for(Object fieldValue : allFieldValues.get(fieldName)) - { - double fieldValueWeight = fieldValueWeightSampler.sample(); - fieldValueWeights.put(fieldValue, fieldValueWeight); - } - - allFieldValueWeights.put(fieldName, fieldValueWeights); - } - - return allFieldValueWeights; - } - - protected Map<Product, Double> generateProductWeights() throws Exception - { - Map<String, Map<Object, Double>> allFieldValueWeights = generateFieldValueWeights(); - - Map<Product, Double> productWeights = Maps.newHashMap(); - for(Product p : productCategory.getProducts()) - { - double weight = 0.0; - for(String fieldName : productCategory.getFieldNames()) - { - Object fieldValue = p.getFieldValue(fieldName); - weight += fieldWeights.get(fieldName) * allFieldValueWeights.get(fieldName).get(fieldValue); - } - productWeights.put(p, weight); - } - productWeights = normalize(productWeights); - - return productWeights; - } - - public DiscretePDF<Product> sample() throws Exception - { - Map<Product, Double> probs = generateProductWeights(); - return new DiscretePDF<Product>(probs); - } - -} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModel.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModel.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModel.java index d54ae98..d460c3b 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModel.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModel.java @@ -21,9 +21,11 @@ import org.apache.bigtop.bigpetstore.datagenerator.framework.SeedFactory; import com.google.common.collect.ImmutableSet; -public interface PurchasingModel extends Serializable +public interface PurchasingModel<T> extends Serializable { public ImmutableSet<String> getProductCategories(); + public T getProfile(String category); + public PurchasingProcesses buildProcesses(SeedFactory seedFactory); } http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModelSamplerBuilder.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModelSamplerBuilder.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModelSamplerBuilder.java index a88636a..43808c7 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModelSamplerBuilder.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/PurchasingModelSamplerBuilder.java @@ -25,9 +25,7 @@ import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; import org.apache.bigtop.bigpetstore.datagenerator.framework.SeedFactory; import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.MarkovModel; -import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.BoundedMultiModalGaussianSampler; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.ExponentialSampler; import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; import com.google.common.collect.ImmutableList; @@ -65,48 +63,6 @@ public class PurchasingModelSamplerBuilder return fieldWeights; } - public Sampler<StaticPurchasingModel> buildStaticPurchasingModel() throws Exception - { - Sampler<Double> fieldWeightSampler; - Sampler<Double> fieldValueWeightSampler; - - if(Constants.STATIC_PURCHASING_MODEL_FIELD_WEIGHT_DISTRIBUTION_TYPE.equals(Constants.DistributionType.BOUNDED_MULTIMODAL_GAUSSIAN)) - { - fieldWeightSampler = new BoundedMultiModalGaussianSampler(Constants.STATIC_FIELD_WEIGHT_GAUSSIANS, - Constants.STATIC_FIELD_WEIGHT_LOWERBOUND, - Constants.STATIC_FIELD_WEIGHT_UPPERBOUND, - seedFactory); - } - else - { - fieldWeightSampler = new ExponentialSampler(Constants.STATIC_FIELD_WEIGHT_EXPONENTIAL, seedFactory); - } - - if(Constants.STATIC_PURCHASING_MODEL_FIELD_VALUE_WEIGHT_DISTRIBUTION_TYPE.equals(Constants.DistributionType.BOUNDED_MULTIMODAL_GAUSSIAN)) - { - fieldValueWeightSampler = new BoundedMultiModalGaussianSampler(Constants.STATIC_FIELD_VALUE_WEIGHT_GAUSSIANS, - Constants.STATIC_FIELD_VALUE_WEIGHT_LOWERBOUND, - Constants.STATIC_FIELD_VALUE_WEIGHT_UPPERBOUND, - seedFactory); - } - else - { - fieldValueWeightSampler = new ExponentialSampler(Constants.STATIC_FIELD_VALUE_WEIGHT_EXPONENTIAL, seedFactory); - } - - Map<String, Double> fieldWeights = generateFieldWeights(fieldWeightSampler); - - Map<ProductCategory, Sampler<DiscretePDF<Product>>> categorySamplers = Maps.newHashMap(); - for(ProductCategory productCategory : productCategories) - { - Sampler<DiscretePDF<Product>> sampler = new ProductCategoryPDFSampler(productCategory, - fieldWeights, fieldValueWeightSampler); - categorySamplers.put(productCategory, sampler); - } - - return new StaticPurchasingModelSampler(categorySamplers); - } - public Sampler<MarkovPurchasingModel> buildMarkovPurchasingModel() throws Exception { @@ -130,7 +86,7 @@ public class PurchasingModelSamplerBuilder Map<ProductCategory, Sampler<MarkovModel<Product>>> categorySamplers = Maps.newHashMap(); for(ProductCategory productCategory : productCategories) { - ProductCategoryMarkovModelSampler sampler = new ProductCategoryMarkovModelSampler(productCategory, + MarkovModelProductCategorySampler sampler = new MarkovModelProductCategorySampler(productCategory, fieldWeights, fieldSimilarityWeightSampler, loopbackWeightSampler); categorySamplers.put(productCategory, sampler); } @@ -140,13 +96,13 @@ public class PurchasingModelSamplerBuilder public Sampler<? extends PurchasingModel> build() throws Exception { - if(Constants.PURCHASING_MODEL_TYPE.equals(Constants.PurchasingModelType.DYNAMIC)) + if(Constants.PURCHASING_MODEL_TYPE.equals(Constants.PurchasingModelType.MARKOV)) { return buildMarkovPurchasingModel(); } else { - return buildStaticPurchasingModel(); + return new MultinomialPurchasingModelSampler(productCategories, seedFactory); } } } http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModel.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModel.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModel.java deleted file mode 100644 index e7234ab..0000000 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModel.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; - -import java.util.Map; - -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; -import org.apache.bigtop.bigpetstore.datagenerator.framework.SeedFactory; -import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.RouletteWheelSampler; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; - -public class StaticPurchasingModel implements PurchasingModel -{ - - private static final long serialVersionUID = 5863830733003282570L; - - private final ImmutableMap<String, DiscretePDF<Product>> productPDFs; - - public StaticPurchasingModel(Map<String, DiscretePDF<Product>> productPDFs) - { - this.productPDFs = ImmutableMap.copyOf(productPDFs); - } - - @Override - public ImmutableSet<String> getProductCategories() - { - return productPDFs.keySet(); - } - - @Override - public PurchasingProcesses buildProcesses(SeedFactory seedFactory) - { - Map<String, Sampler<Product>> processes = Maps.newHashMap(); - for(String category : getProductCategories()) - { - DiscretePDF<Product> pdf = productPDFs.get(category); - processes.put(category, RouletteWheelSampler.create(pdf, seedFactory)); - } - - return new PurchasingProcesses(processes); - } - -} http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModelSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModelSampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModelSampler.java deleted file mode 100644 index 0647720..0000000 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/main/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/StaticPurchasingModelSampler.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.bigtop.bigpetstore.datagenerator.generators.purchase; - -import java.util.Map; - -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.Product; -import org.apache.bigtop.bigpetstore.datagenerator.datamodels.inputs.ProductCategory; -import org.apache.bigtop.bigpetstore.datagenerator.framework.pdfs.DiscretePDF; -import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; - -public class StaticPurchasingModelSampler implements Sampler<StaticPurchasingModel> -{ - final ImmutableMap<ProductCategory, Sampler<DiscretePDF<Product>>> categorySamplers; - - public StaticPurchasingModelSampler(Map<ProductCategory, Sampler<DiscretePDF<Product>>> categorySamplers) - { - this.categorySamplers = ImmutableMap.copyOf(categorySamplers); - } - - public StaticPurchasingModel sample() throws Exception - { - Map<String, DiscretePDF<Product>> pdfs = Maps.newHashMap(); - for(ProductCategory productCategory : categorySamplers.keySet()) - { - Sampler<DiscretePDF<Product>> sampler = categorySamplers.get(productCategory); - pdfs.put(productCategory.getCategoryLabel(), sampler.sample()); - } - - return new StaticPurchasingModel(pdfs); - } -} - http://git-wip-us.apache.org/repos/asf/bigtop/blob/259fc42c/bigtop-bigpetstore/bigpetstore-data-generator/src/test/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/TestProductCategoryMarkovModelSampler.java ---------------------------------------------------------------------- diff --git a/bigtop-bigpetstore/bigpetstore-data-generator/src/test/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/TestProductCategoryMarkovModelSampler.java b/bigtop-bigpetstore/bigpetstore-data-generator/src/test/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/TestProductCategoryMarkovModelSampler.java index 89303f6..acb0929 100644 --- a/bigtop-bigpetstore/bigpetstore-data-generator/src/test/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/TestProductCategoryMarkovModelSampler.java +++ b/bigtop-bigpetstore/bigpetstore-data-generator/src/test/java/org/apache/bigtop/bigpetstore/datagenerator/generators/purchase/TestProductCategoryMarkovModelSampler.java @@ -30,7 +30,7 @@ import org.apache.bigtop.bigpetstore.datagenerator.framework.markovmodels.Markov import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.Sampler; import org.apache.bigtop.bigpetstore.datagenerator.framework.samplers.UniformSampler; import org.apache.bigtop.bigpetstore.datagenerator.generators.products.ProductCategoryBuilder; -import org.apache.bigtop.bigpetstore.datagenerator.generators.purchase.ProductCategoryMarkovModelSampler; +import org.apache.bigtop.bigpetstore.datagenerator.generators.purchase.MarkovModelProductCategorySampler; import org.junit.Test; import com.google.common.collect.ImmutableMap; @@ -92,7 +92,7 @@ public class TestProductCategoryMarkovModelSampler fieldWeights.put(fieldName, fieldWeightSampler.sample()); } - ProductCategoryMarkovModelSampler generator = new ProductCategoryMarkovModelSampler(productCategory, + MarkovModelProductCategorySampler generator = new MarkovModelProductCategorySampler(productCategory, fieldWeights, new UniformSampler(seedFactory), new UniformSampler(seedFactory) );
