add basic core structure and first parts of antGame implementation

This commit is contained in:
2019-12-07 22:05:11 +01:00
parent 66ee33b77f
commit 87f435c65a
19 changed files with 347 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
package core;
public interface Action {
int getIndex();
String toString();
int hashCode();
boolean equals(Object obj);
}
+9
View File
@@ -0,0 +1,9 @@
package core;
import java.util.Set;
public interface ActionSpace<A extends Enum> {
Set<Action> getAllActions();
int getNumberOfAction();
void addAction(DiscreteAction<A> a);
}
+36
View File
@@ -0,0 +1,36 @@
package core;
public class DiscreteAction<A extends Enum> implements Action{
private A action;
public DiscreteAction(A action){
this.action = action;
}
@Override
public int getIndex(){
return action.ordinal();
}
@Override
public String toString(){
return action.toString();
}
@Override
public int hashCode() {
return getIndex();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
return getIndex() == ((DiscreteAction) obj).getIndex();
}
}
@@ -0,0 +1,28 @@
package core;
import java.util.HashSet;
import java.util.Set;
public class DiscreteActionSpace<A extends Enum> implements ActionSpace<A>{
private Set<Action> actions;
public DiscreteActionSpace(){
actions = new HashSet<>();
}
@Override
public void addAction(DiscreteAction<A> action){
actions.add(action);
}
@Override
public int getNumberOfAction(){
return actions.size();
}
@Override
public Set<Action> getAllActions(){
return actions;
}
}
+4
View File
@@ -0,0 +1,4 @@
package core;
public interface Observation {
}
+19
View File
@@ -0,0 +1,19 @@
package core;
import java.util.Random;
public class RNG {
private static final Random rng;
private static final int SEED = 123;
static {
rng = new Random(SEED);
}
public static Random getRandom() {
return rng;
}
public static void reseed(){
rng.setSeed(SEED);
}
}
+7
View File
@@ -0,0 +1,7 @@
package core;
public interface State {
String toString();
int hashCode();
boolean equals(Object obj);
}
@@ -0,0 +1,66 @@
package core;
import java.util.HashMap;
import java.util.Map;
/**
* Premise: All states have the complete action space
*/
public class StateActionHashTable<A extends Enum> implements StateActionTable {
private final Map<State, Map<Action, Double>> table;
private ActionSpace<A> actionSpace;
public StateActionHashTable(ActionSpace<A> actionSpace){
table = new HashMap<>();
this.actionSpace = actionSpace;
}
/*
If the state is not present in the table at the time of
calling this method the DEFAULT_VALUE gets returned BUT
no the missing state is not inserted into the table!
Inserting of missing states is ONLY done in "setValue()"
method.
*/
@Override
public double getValue(State state, Action action) {
final Map<Action, Double> actionValues = table.get(state);
if (actionValues != null) {
return actionValues.get(action);
}
return DEFAULT_VALUE;
}
/*
Update the value of an action for a specific state.
If the state is not present in the table yet,
it will get stored in combination with every action
from the action space initialized with the default value.
*/
@Override
public void setValue(State state, Action action, double value) {
final Map<Action, Double> actionValues;
if (table.containsKey(state)) {
actionValues = table.get(state);
} else {
actionValues = createDefaultActionValues();
table.put(state, actionValues);
}
actionValues.put(action, value);
}
@Override
public Map<Action, Double> getActionValues(State state) {
return null;
}
private Map<Action, Double> createDefaultActionValues(){
final Map<Action, Double> defaultActionValues = new HashMap<>();
for(Action action: actionSpace.getAllActions()){
defaultActionValues.put(action, DEFAULT_VALUE);
}
return defaultActionValues;
}
}
+12
View File
@@ -0,0 +1,12 @@
package core;
import java.util.Map;
public interface StateActionTable {
double DEFAULT_VALUE = 0.0;
double getValue(State state, Action action);
void setValue(State state, Action action, double value);
Map<Action, Double> getActionValues(State state);
}
+15
View File
@@ -0,0 +1,15 @@
package core;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class StepResult {
private Observation observation;
private double reward;
private boolean done;
private String info;
}