package master; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import agents.*; /** * Constructs products and sells them. * * @author mbilgic * */ public class LearningAgentSimulator { //The cap on the maximum value of the product private final static double MAXIMUMVALUE = 100000; //The initial money the agent has private final static double INITIALMONEY = 1000; //The amount the agent earns daily from other sources private final static double DAILYEARNINGS = 100; //To see the full trace, set it true static boolean debug = false; /** * @param args */ public static void main(String[] args) { //TODO Change this to the last four digits of your A#. int lastfourdigits = 1234; int[] seeds = {0, 1, 2, 3, lastfourdigits}; if(args.length<1) { System.err.println("Usage: java LearningAgentSimulator "); System.exit(1); } String instancesFile = args[0]; ArrayList> allInstances = LearningAgentSimulator.readInstances(instancesFile); Collections.shuffle(allInstances, new Random(0)); int foldSize = allInstances.size()/seeds.length; double[] average = new double[foldSize]; for(int s=0; s> testInstances = new ArrayList>(); ArrayList> trainInstances = new ArrayList>(); for(int i=0; i= foldSize*s && i> trainInstances, ArrayList> testInstances, int seed) { Bank bank = new Bank(); agent.setBank(bank); //deposit the initial money bank.deposit(agent, LearningAgentSimulator.INITIALMONEY); Random rand = new Random(seed); double[] dailyBalance = new double[testInstances.size()]; NumberFormat nf = NumberFormat.getCurrencyInstance(); LearningAgentSimulator.print("\nSeed="+seed); LearningAgentSimulator.print("Agent is learning the concept."); agent.learn(trainInstances); LearningAgentSimulator.print("Agent completed the learning process."); for(int p=0;p prodFeatures = testInstances.get(p); String workingCondition = prodFeatures.get(prodFeatures.size()-1); boolean productWorking = workingCondition.equalsIgnoreCase("G"); //Remove the class prodFeatures.remove(prodFeatures.size()-1); boolean willBuy = agent.willBuy(prod, prodFeatures); prodFeatures.add(workingCondition); if(willBuy) { LearningAgentSimulator.print("Agent "+agent+" decides to buy it."); //withdraw the product's price from the agent's account bank.withdraw(agent, prod.getPrice()); if(productWorking) // the product is in working condition { LearningAgentSimulator.print("Good call: the product is in working condition."); LearningAgentSimulator.print("The agent's profit is "+ nf.format((prod.getValue()-prod.getPrice())) +"."); //deposit the product's value to the agent's account bank.deposit(agent, prod.getValue()); } else { LearningAgentSimulator.print("Bad call: the product is faulty."); LearningAgentSimulator.print("The agent loses "+nf.format(prod.getPrice()) +"."); //Do not deposit anything } } else { LearningAgentSimulator.print("Agent "+agent+" decides not to buy it."); if(productWorking) { LearningAgentSimulator.print("Missed opportunity: the product was in working condition."); } else { LearningAgentSimulator.print("Good call: the product was faulty."); } } bank.deposit(agent, LearningAgentSimulator.DAILYEARNINGS); dailyBalance[p] = bank.balance(agent); LearningAgentSimulator.print((p+1) + "\t" + nf.format(dailyBalance[p])); } return dailyBalance; } private static void print(String text) { if(LearningAgentSimulator.debug) System.out.println(text); } /** * Read instances from a csv file, where the first line is header information. * @param file * @return */ private static ArrayList> readInstances(String file) { ArrayList> instances = new ArrayList>(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line = br.readLine();//Header String[] fields = line.split(","); int numFields = fields.length; // number of attributes + class while((line = br.readLine()) != null) { ArrayList instance = new ArrayList(numFields); fields = line.split(","); for(int i=0;i