From c6d1f00bf456b843e2a4b0e49425cd62d4c05044 Mon Sep 17 00:00:00 2001 From: Sailesh Date: Wed, 19 Apr 2017 22:02:51 -0400 Subject: [PATCH] Base infrastructure --- src/BaseEvaluator.java | 13 ++++++++++--- src/LearningEvaluator.java | 36 ++++++++++++++++++++++++++++++++++++ src/weights/alpha.csv | 3 +++ src/weights/beta.csv | 1 + 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/LearningEvaluator.java create mode 100644 src/weights/alpha.csv create mode 100644 src/weights/beta.csv diff --git a/src/BaseEvaluator.java b/src/BaseEvaluator.java index ed9c388..dc79bf2 100644 --- a/src/BaseEvaluator.java +++ b/src/BaseEvaluator.java @@ -1,9 +1,12 @@ public class BaseEvaluator implements Evaluator{ - WeightsParser wp; + // meant for beta to use + // when beta should use alpha's weights, have alpha commit to beta.csv and then call refreshWeights() + + protected WeightsParser wp; String file; - double[] weights; + protected double[] weights; public BaseEvaluator(String file){ this.wp = new WeightsParser(); @@ -11,7 +14,7 @@ public BaseEvaluator(String file){ this.weights = this.wp.getWeights(file); } - private double dot(double[] a1, double[] a2){ + private double dot(double[] a1, double[] a2){ // function for dot product double res = 0; for(int i = 0; i < a1.length; i++){ res += (a1[i] * a2[i]); @@ -24,6 +27,10 @@ public double evaluate(CheckersGameState s, int player){ return dot(this.weights, params); } + public void refreshWeights(){ + this.weights = this.wp.getWeights(this.file); + } + } diff --git a/src/LearningEvaluator.java b/src/LearningEvaluator.java new file mode 100644 index 0000000..8f31cac --- /dev/null +++ b/src/LearningEvaluator.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; + +public class LearningEvaluator extends BaseEvaluator{ + + ArrayList params; + ArrayList values; + double alpha; // learning parameter, higher alpha means weights are closer to the regression output + // alpha of 1 is directly setting weights to be regression weights + // ideally we start at 1 and lower alpha to get a convergence + + public LearningEvaluator(String file, double alpha){ + super(file); + params = new ArrayList(); + values = new ArrayList(); + this.alpha = alpha; + + } + + public void setAlpha(double a){ + alpha = a; + } + + public void add_data(double[] features, double value){ + values.add(value); + params.add(features); + } + + public void commitWeights(String path){ + this.wp.writeWeights(path, this.weights); // method to commit weights to beta. provide path to beta csv + } + + + + +} diff --git a/src/weights/alpha.csv b/src/weights/alpha.csv new file mode 100644 index 0000000..16e15a6 --- /dev/null +++ b/src/weights/alpha.csv @@ -0,0 +1,3 @@ +10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 +10.0, 10.0, 10.0, 10.0, 10.0, 75.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 +10.0, 10.0, 10.0, 10.0, 10.0, 75.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 diff --git a/src/weights/beta.csv b/src/weights/beta.csv new file mode 100644 index 0000000..a8af049 --- /dev/null +++ b/src/weights/beta.csv @@ -0,0 +1 @@ +10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10