package objects; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.math.*; import com.sun.org.apache.bcel.internal.Constants; import utilities.*; public class Agent { boolean deceiver = false; private double[][] train_story; private double[][] testTrue_story; private double[][] testFalse_story; private double[][] shift_story; private double[][] random_story; private double[][] wrong_story; private double[][] change_story; private double[][] true_story; private double[][][] rv_dependency; private double[][] rv_correlation; Graph priorknowledge; //String bkb_name; String name; public int goal_state; public int goal_handle; boolean storyinited = false; String graph_name; double binary_threshold; double uncertaintyThreshold; double irrelevancyThreshold; int[] commonality; int[] uncommonality; String contribution_f; public Agent(boolean deceiver, String graph_n, String name) { this.deceiver = deceiver; priorknowledge = new Graph(); System.err.println("reading "+graph_n); priorknowledge.readNetFile(graph_n); this.graph_name = graph_n; this.name = name; //bkb_name = BKB_engine.loadBN(graph_n); //initStories(runs); } /*public void setBKBName(String name){ bkb_name = name; }*/ public void setUncertaintyThreshold(double value) { uncertaintyThreshold = value; } public void setIrrelevancyThreshold(double value) { irrelevancyThreshold = value; } public void setCommonality(int[] value) { commonality = value.clone(); } public void setUncommonality(int[] value) { uncommonality = value.clone(); } public void setContributionFilesName(String name) { contribution_f = name; } public String getName() { return name; } public boolean isDeceiver() { if (this.deceiver) return true; else return false; } public void initStories(int repeat, String goal) { int states = priorknowledge.getNumberOfStates(); train_story = new double[states][repeat]; testTrue_story = new double[states][repeat]; testFalse_story = new double[states][repeat]; if (this.deceiver) { shift_story = new double[states][repeat]; true_story = new double[states][repeat]; random_story = new double[states][repeat]; wrong_story = new double[states][repeat]; change_story = new double[states][repeat]; rv_dependency = new double[states][states][4]; } int handle = priorknowledge.getNetwork().getFirstNode(); int st = 0; while (handle!=constant.DSL_OUT_OF_RANGE) { if (priorknowledge.getRvName(handle).trim().equals(goal.trim())) { this.goal_state = st; this.goal_handle = handle; break; } st=st+priorknowledge.getNumberOfStates(handle); handle = priorknowledge.getNetwork().getNextNode(handle); } storyinited=true; } public void retrieveDeceiverValues(double[][] story, String file, Evidence[] evi, int agent) { try{ FileInputStream input2 = new FileInputStream(file); DataInputStream in2 = new DataInputStream(input2); BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); String strLine2; int idx=0; while((strLine2=br2.readLine())!=null&&idx=0){// && i!=this.goal_state) { double rand = utilities.RandomNumber.getRandomNumber(constant.random_random_lower, constant.random_random_upper);//Math.random()*(constant.random_upper- constant.random_lower)+constant.random_lower; values[i] = testTrue_story[i][repeat]+rand; values[i+1] = testTrue_story[i+1][repeat]-rand; /*if (values[i]>1) values[i] = 1; else if (values[i]<0) values[i] = 0; if (values[i+1]>1) values[i+1] = 1; else if (values[i+1]<0) values[i+1] =0; */ if (testTrue_story[i][repeat]+rand>1) {values[i] = 1;values[i+1] = 0;} else if (testTrue_story[i][repeat]+rand<0) {values[i] = 0;values[i+1] = 1;} else if (rand>=0.5) {values[i] = 0.5; values[i+1] = 0.5;} else {values[i] = testTrue_story[i][repeat]; values[i+1] = testTrue_story[i+1][repeat];} } else { values[i+1] = testTrue_story[i+1][repeat]; values[i] = testTrue_story[i][repeat]; } //yuki //} } // double[] polarities = utility.convert2polarities2 (this, values, goal_handle, irrelevancyThreshold, evi); // double[] polarities = utility.convert2polarities (evi, this.uncertaintyThreshold, this.commonality, this.uncommonality, values, this.goal_handle, // this.irrelevancyThreshold, this.name.substring(this.name.indexOf("_")+1)+"_"+contribution_f, this.priorknowledge); for (int i=0; i=0) { count++; } } String[] evi_name = new String[count]; String[] evi_state = new String[count]; int idx=0; for (int i=0; i=0) { evi_name[idx] = priorknowledge.getRvId(evi[i].gethandle()); evi_state[idx] = priorknowledge.getStateId(evi[i].gethandle(), evi[i].getstate()); idx++; } } //return BKB_engine.inferBKB(this.priorknowledge, this.bkb_name, evi_name, evi_state); return utilities.MPE_engine.doMPE(this.priorknowledge, this.graph_name, evi_name, evi_state); } public void convert2Binary(double[][] result, double[] exact_result, int repeat, int mode) { for (int i=0; i1-constant.BINARY_THRESHOLD_GOAL) || this.binary_threshold!=0 && (exact_result[i]1-binary_threshold)) { result[i][repeat] = 0.5; result[i+1][repeat] = 0.5; } else { result[i][repeat] = Math.floor(exact_result[i]/0.50001); result[i+1][repeat] = Math.floor(exact_result[i+1]/0.50001); } } else { if (this.binary_threshold==0 && (exact_result[i]1-constant.BINARY_THRESHOLD) || this.binary_threshold!=0 && (exact_result[i]1-binary_threshold)) { result[i][repeat] = 0.5; result[i+1][repeat] = 0.5; } else { result[i][repeat] = Math.floor(exact_result[i]/0.50001); result[i+1][repeat] = Math.floor(exact_result[i+1]/0.50001); } } } else if (mode==constant.IMPLICIT_IRRELEVENT) { if (exact_result[i]==0.5) { result[i][repeat] = 0.5; result[i+1][repeat] = 0.5; } else { result[i][repeat] = Math.floor(exact_result[i]/0.50001); result[i+1][repeat] = Math.floor(exact_result[i+1]/0.50001); } } else if (mode==constant.IMPLICIT_COMMONSENSE) { } } } public void calculateDependency() { for (int i=0; i