Work in progress.
Able to read short survey, recipe book but crashing in Bayes Net code.
This commit is contained in:
@@ -1,61 +1,112 @@
|
||||
package net.woodyfolsom.cs6601.p2;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math3.stat.regression.SimpleRegression;
|
||||
import net.woodyfolsom.cs6601.p2.Ingredient.TYPE;
|
||||
import dkohl.bayes.bayesnet.BayesNet;
|
||||
import dkohl.bayes.builders.FoodNetBuilder;
|
||||
import dkohl.bayes.example.builders.FoodExampleBuilder;
|
||||
import dkohl.bayes.inference.EnumerateAll;
|
||||
import dkohl.bayes.probability.Assignment;
|
||||
import dkohl.bayes.probability.ProbabilityAssignment;
|
||||
import dkohl.bayes.probability.Variable;
|
||||
|
||||
public class BayesChef {
|
||||
|
||||
public static void main(String... args) {
|
||||
System.out.println("Reading recipe book.");
|
||||
RecipeBook recipeBook = RecipeBookReader.readRecipeBook(new File("data/survey_recipes.xml"));
|
||||
|
||||
System.out.println("Read data for " + recipeBook.getSize() + " recipes.");
|
||||
RecipeBook recipeBook = RecipeBookReader.readRecipeBook(new File("data/short_recipebook.xml"));
|
||||
|
||||
System.out.println("Reading survey data.");
|
||||
Survey survey = SurveyReader.readSurvey(new File("data/survey.xml"));
|
||||
Survey survey = SurveyReader.readSurvey(new File("data/short_survey.xml"));
|
||||
|
||||
System.out.println("Read data for " + survey.getDinerCount() + " diner(s).");
|
||||
new BayesChef().reportBestMeal(recipeBook,survey);
|
||||
}
|
||||
|
||||
private void reportBestMeal(RecipeBook recipeBook, Survey survey) {
|
||||
int numRecipes = recipeBook.getSize();
|
||||
System.out.println("Read data for " + numRecipes + " recipes.");
|
||||
|
||||
int numDiners = survey.getDinerCount();
|
||||
System.out.println("Read data for " + numDiners + " diner(s).");
|
||||
|
||||
/*
|
||||
System.out.println("Setting evidence for first 11 recipes.");
|
||||
|
||||
System.out.println("Evaluating preference for remaining recipes.");
|
||||
|
||||
//read survey prefs from survey.xml
|
||||
double[][] surveyPrefs = new double[5][];
|
||||
for (int i = 0; i < 5; i++) {
|
||||
surveyPrefs[i] = new double[11];
|
||||
for (int j = 0; j < 11; j++) {
|
||||
double[][] surveyPrefs = new double[numDiners][];
|
||||
for (int i = 0; i < numDiners; i++) {
|
||||
surveyPrefs[i] = new double[numRecipes/2];
|
||||
for (int j = 0; j < numRecipes/2; j++) {
|
||||
surveyPrefs[i][j] = survey.getDiner(i).getRating(j);
|
||||
}
|
||||
}
|
||||
|
||||
//generating stub evaluated preferences to test RMSE calculation
|
||||
double[][] calculatedPrefs = new double[5][];
|
||||
calculatedPrefs[0] = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
|
||||
calculatedPrefs[1] = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
|
||||
calculatedPrefs[2] = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
|
||||
calculatedPrefs[3] = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
|
||||
calculatedPrefs[4] = new double[] { 10.0, 10.0, 0.0, 10.0, 10.0, 4.0, 10.0, 10.0, 10.0, 10.0, 10.0 };
|
||||
double[][] calculatedPrefs = new double[numDiners][];
|
||||
calculatedPrefs[0] = new double[numRecipes/2];
|
||||
|
||||
System.out.println("RMSE for recipes #12-22 (calculated vs. surveyed preference):");
|
||||
System.out.println("RMSE for recipes #" + numRecipes/2 + "-" + numRecipes +" (calculated vs. surveyed preference):");
|
||||
|
||||
for (int dinerIndex = 0; dinerIndex < survey.getDinerCount(); dinerIndex++) {
|
||||
SimpleRegression simpleRegression = new SimpleRegression();
|
||||
|
||||
for (int i = 0; i < 11; i++) {
|
||||
for (int i = 0; i < numRecipes/2; i++) {
|
||||
simpleRegression.addData(i,calculatedPrefs[dinerIndex][i]);
|
||||
}
|
||||
double calculatedMSE = simpleRegression.getMeanSquareError();
|
||||
simpleRegression.clear();
|
||||
|
||||
for (int i = 0; i < 11; i++) {
|
||||
for (int i = 0; i < numRecipes/2; i++) {
|
||||
simpleRegression.addData(i,surveyPrefs[dinerIndex][i]);
|
||||
}
|
||||
|
||||
double surveyMSE = simpleRegression.getMeanSquareError();
|
||||
|
||||
System.out.println("Diner # " + (dinerIndex + 1) + ": " + calculatedMSE + " vs. "+ surveyMSE);
|
||||
}*/
|
||||
|
||||
BayesNet net = FoodNetBuilder.createDishNet(survey, recipeBook);
|
||||
//BayesNet net = FoodExampleBuilder.dishNet();
|
||||
|
||||
printPreference(net, TYPE.PORK);
|
||||
printPreference(net, TYPE.BEEF);
|
||||
printPreference(net, TYPE.POTATO);
|
||||
printPreference(net, TYPE.TOMATO);
|
||||
}
|
||||
|
||||
void printPreference(BayesNet net, TYPE type) {
|
||||
List<ProbabilityAssignment> probs = EnumerateAll.enumerateAsk(new Variable(
|
||||
FoodNetBuilder.TASTE, FoodNetBuilder.RATING_DOMAIN),
|
||||
net, createQuery(type));
|
||||
double max_val = 0.0;
|
||||
String max_arg = "N/A";
|
||||
for (ProbabilityAssignment p : probs) {
|
||||
if (p.getProbability() > max_val) {
|
||||
max_val = p.getProbability();
|
||||
max_arg = p.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("TASTE " + type + ": " + max_arg + " " + max_val);
|
||||
}
|
||||
|
||||
List<Assignment> createQuery(TYPE type) {
|
||||
List<Assignment> assignment = new LinkedList<Assignment>();
|
||||
switch (type) {
|
||||
case PORK:
|
||||
case BEEF:
|
||||
case POTATO:
|
||||
case TOMATO:
|
||||
assignment.add(new Assignment(new Variable(type.toString(), FoodNetBuilder.DOMAIN),
|
||||
FoodNetBuilder.TRUE_VALUE));
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return assignment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("ing")
|
||||
public class Ingredient {
|
||||
public enum TYPE { ALCOHOL, DAIRY, EGGS, FISH, GLUTEN, GRAIN, NUTS, PORK, POULTRY, RED_MEAT, SHELLFISH, SPICE, SUGAR}
|
||||
public enum TYPE { ALCOHOL, BEEF, DAIRY, EGGS, FISH, GLUTEN, GRAIN, NUTS, PORK, POULTRY, POTATO, SHELLFISH, SPICE, SUGAR, TOMATO}
|
||||
|
||||
private String item;
|
||||
|
||||
@@ -14,20 +14,31 @@ public class Ingredient {
|
||||
|
||||
public boolean isType(TYPE type) {
|
||||
switch (type) {
|
||||
case BEEF :
|
||||
return item.contains("beef");
|
||||
case DAIRY :
|
||||
return item.contains("margarine");
|
||||
case EGGS :
|
||||
return item.equals("egg") || item.equals("eggs");
|
||||
case GLUTEN :
|
||||
return item.contains("flour");
|
||||
case RED_MEAT :
|
||||
return item.contains("beef");
|
||||
case PORK :
|
||||
return item.contains("pork");
|
||||
case POTATO :
|
||||
return item.contains("potato");
|
||||
case SPICE :
|
||||
return item.endsWith("cinnamon") || item.endsWith("nutmeg") || item.endsWith("cloves");
|
||||
case SUGAR :
|
||||
return item.endsWith("sugar");
|
||||
case TOMATO :
|
||||
return item.contains("tomato");
|
||||
default : //unknown ingredient, e.g. coffee, bananas, honey
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Object readResolve() {
|
||||
item = item.toLowerCase();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,16 @@ public class RecipeBook {
|
||||
return recipes.get(index);
|
||||
}
|
||||
|
||||
//TODO build an index of recipes by name?
|
||||
public Recipe getRecipe(String recipeName) {
|
||||
for (Recipe recipe : recipes) {
|
||||
if (recipeName.equalsIgnoreCase(recipe.getHead().getTitle())) {
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return recipes.size();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user