Permalink
Browse files

Remove redundant AI code

  • Loading branch information...
1 parent 2d519bb commit 178e5b71ff74f06f15b0b4df0173a7f8c6b173a0 @mikera committed Dec 6, 2012
@@ -1,6 +0,0 @@
-package mikera.ai;
-
-public abstract class PriorityFunction<T, P> {
-
- public abstract double getPriority(T actor, P param);
-}
@@ -1,20 +0,0 @@
-package mikera.ai;
-
-public interface Sensor {
- /**
- * Returns the output length of the sensor (number of double values)
- * @return
- */
- public int getOutputLength();
-
- /**
- * Sense a given object
- * @param a
- * @return a new double[] array representing the sense data
- */
- public double[] sense(Object a);
-
- public void sense(Object a, double[] dest);
-
- public void sense(Object a, double[] dest, int offset);
-}
@@ -1,23 +0,0 @@
-package mikera.ai;
-
-import mikera.annotations.Immutable;
-
-/**
- * Generic AI task representation
- *
- * Intended to be parameterised by actor and
- * a task-specific parameter
- *
- * Generate through Tasks.* static methods
- *
- * @author Mike Anderson
- *
- * @param <T> Actor type
- * @param <P> Parameter type
- * @param <R> Result type
- */
-
-@Immutable
-public abstract class Task<T,P,R> {
- public abstract R run(T actor, P param);
-}
@@ -1,169 +0,0 @@
-package mikera.ai;
-
-import mikera.persistent.ListFactory;
-import mikera.persistent.PersistentList;
-import mikera.persistent.PersistentMap;
-
-/**
- * Generic generator functions for tasks
- *
- * Tasks should be persistent and immutable
- * to allow concurrent sharing by actors
- *
- * @author Mike Anderson
- *
- */
-public class Tasks {
- @SuppressWarnings("rawtypes")
- private static final Task<?,?,?> NULL_TASK = new Task() {
- @Override
- public Object run(Object actor, Object param) {
- return null;
- }
- };
-
- public static <T,P,R> Task<T,P,R> select(
- final PersistentList<Task<T,P,R>> ts)
- {
- return new Task<T,P,R>() {
-
- @Override
- public R run(T actor, P param) {
- int n=ts.size();
- for (int i=0; i<n; i++) {
- R result=ts.get(i).run(actor, param);
- if (result!=null) return result;
- }
- return null;
- }
-
- };
- }
-
- public static <T,P,R> Task<T,P,R> lookup(
- final PersistentMap<P,Task<T,P,R>> ts)
- {
- return new Task<T,P,R>() {
- @Override
- public R run(T actor, P param) {
- Task<T,P,R> task=ts.get(param);
- return task.run(actor, param);
- }
- };
- }
-
- public static <T,P,R> Task<T,P,R> prioritise(
- final PersistentList<PriorityFunction<T,P>> ps,
- final PersistentList<Task<T,P,R>> ts)
- {
- if (ps.size()!=ts.size()) throw new Error("Wrong size: priorities="+ps.size()+" tasks="+ts.size());
- return new Task<T,P,R>() {
- private final int n=ts.size();
-
- @Override
- public R run(T actor, P param) {
- Task<T,P,R> bestTask=null;
- double bestPriority=-Double.MAX_VALUE;
- for (int i=0; i<n; i++) {
- double p=ps.get(i).getPriority(actor, param);
- if (p>bestPriority) {
- bestTask=ts.get(i);
- bestPriority=p;
- }
- }
- if (bestTask==null) return null;
- return bestTask.run(actor, param);
- }
-
- };
- }
-
- @SuppressWarnings("unchecked")
- public static <T,P,R> Task<T,P,R> nullTask() {
- return (Task<T,P,R>) NULL_TASK;
- }
-
- public static <T,P,R> Task<T,P,R> select(
- final Task<T,P,R> a, Task<T,P,R> b) {
- PersistentList<Task<T,P,R>> ts=ListFactory.create(a,b);
- return select(ts);
- }
-
- public static <T,P,R> Task<T,P,R> sequence(
- final PersistentList<Task<T,P,R>> ts)
- {
- return new Task<T,P,R>() {
- @Override
- public R run(T actor, P param) {
- int n=ts.size();
- R result=null;
- for (int i=0; i<n; i++) {
- result=ts.get(i).run(actor, param);
- if (result==null) return null;
- }
- return result;
- }
- };
- }
-
- public static <T,P,R> Task<T,P,R> sequence(
- final Task<T,P,R> a, Task<T,P,R> b) {
- PersistentList<Task<T,P,R>> ts=ListFactory.create(a,b);
- return sequence(ts);
- }
-
- public static <T,PR> Task<T,PR,PR> chain(
- final PersistentList<Task<T,PR,PR>> ts)
- {
- return new Task<T,PR,PR>() {
-
- @Override
- public PR run(T actor, PR param) {
- int n=ts.size();
- for (int i=0; i<n; i++) {
- param=ts.get(i).run(actor, param);
- }
- return param;
- }
- };
- }
-
- public static <T,P,P2,R> Task<T,P,R> chain(
- final Task<T,P,P2> t1,
- final Task<T,P2,R> t2)
- {
- return new Task<T,P,R>() {
-
- @Override
- public R run(T actor, P param) {
- P2 p2=t1.run(actor,param);
- return t2.run(actor, p2);
- }
- };
- }
-
- @SuppressWarnings("unchecked")
- public static <T,P,R1,R2> Task<T,P,R2> test(
- final Task<T,P,R1> cond,
- Task<T,P,R2> ta,
- Task<T,P,R2> tb)
- {
- // allow for null parameters
- final Task<T,P,R2> a=(ta==null)?(Task<T,P,R2>)NULL_TASK:ta;
- final Task<T,P,R2> b=(tb==null)?(Task<T,P,R2>)NULL_TASK:tb;
-
- return new Task<T,P,R2>() {
- @Override
- public R2 run(T actor, P param) {
- R1 test=cond.run(actor, param);
- if (test!=null) {
- return a.run(actor, param);
- }
- return b.run(actor, param);
- }
- };
- }
-
-
-
-}
@@ -1,19 +0,0 @@
-package mikera.ai.sensor;
-
-import mikera.ai.Sensor;
-
-public abstract class AbstractSensor implements Sensor {
- public double[] sense(Object a) {
- double[] output=new double[getOutputLength()];
- sense(a,output,0);
- return output;
- }
-
- public void sense(Object a, double[] dest) {
- sense(a,dest,0);
- }
-
-
-}
-
-
@@ -1,24 +0,0 @@
-package mikera.ai.sensor;
-
-
-public final class CharSensor extends AbstractSensor {
-
- public int getOutputLength() {
- return 8;
- }
-
- public void sense(Object a, double[] dest, int offset) {
- Character ch=(Character)a;
-
- int c=ch;
-
- sense8BitChar(c,dest,offset);
- }
-
- public static final void sense8BitChar(int c, double[] dest, int offset) {
- for (int i=0; i<8; i++) {
- dest[i+offset]=c&1;
- c>>=1;
- }
- }
-}
@@ -1,40 +0,0 @@
- package mikera.ai.sensor;
-
-import mikera.util.Maths;
-
-public final class FixedStringSensor extends AbstractSensor {
-
- private final int length;
-
- public static FixedStringSensor create(int fixedLength) {
- return new FixedStringSensor(fixedLength);
- }
-
- private FixedStringSensor(int fixedLength) {
- length=fixedLength;
- }
-
- public int getOutputLength() {
- return length*8;
- }
-
- public void sense(Object a, double[] dest, int offset) {
- String s=(String)a;
-
- int stringLength=s.length();
-
- int scanLength=Maths.min(length, stringLength);
-
- for (int i=0; i<scanLength; i++) {
- int c=s.charAt(i);
-
- CharSensor.sense8BitChar(c,dest,offset+i*8);
- }
-
- for (int di=scanLength*8; di<length*8; di++) {
- dest[offset+di]=0;
- }
- }
-
-
-}
@@ -1,32 +0,0 @@
-package mikera.ai.sensor;
-
-import java.util.Arrays;
-
-import mikera.ai.sensor.AbstractSensor;
-import mikera.ai.Sensor;
-
-public class MaybeSensor extends AbstractSensor {
- final Sensor source;
- final int length;
-
- public MaybeSensor(Sensor source) {
- this.source=source;
- this.length=source.getOutputLength()+1;
- }
-
- public int getOutputLength() {
- // TODO Auto-generated method stub
- return 0;
- }
-
- public void sense(Object a, double[] dest, int offset) {
- if (a==null) {
- dest[0]=1;
- Arrays.fill(dest, 1, length, 0);
- } else {
- dest[0]=0;
- source.sense(a,dest,offset+1);
- }
- }
-
-}
@@ -1,32 +0,0 @@
-package mikera.ai.sensor;
-
-/**
- * Sensor that scales a double input
- * @author Mike
- *
- */
-public final class ScaleSensor extends AbstractSensor {
- private final double mean;
- private final double invSD;
-
- public static ScaleSensor create(double mean, double sd) {
- return new ScaleSensor(mean,sd);
- }
-
- private ScaleSensor(double mean, double sd) {
- this.mean=mean;
- this.invSD=1.0/sd;
- }
-
-
- public int getOutputLength() {
- return 1;
- }
-
- public void sense(Object a, double[] dest, int offset) {
- double d=(Double)a;
-
- dest[offset]=(d-mean)*invSD;
- }
-
-}
Oops, something went wrong.

0 comments on commit 178e5b7

Please sign in to comment.