Skip to content

Commit

Permalink
Behaviour and properties of traversers via plugin
Browse files Browse the repository at this point in the history
- Add additional seekers
  • Loading branch information
martinchapman committed Mar 17, 2023
1 parent 94b035d commit 9abfcaa
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 34 deletions.
31 changes: 27 additions & 4 deletions src/main/java/org/kclhi/hands/Gas.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package org.kclhi.hands;

import org.kclhi.hands.utility.Utils;

import org.json.JSONObject;

public class Gas {

public static final double HIGH_GAS_PROPORTION = 1.0;
public static final double MEDIUM_GAS_PROPORTION = 2.0;
public static final double LOW_GAS_PROPORTION = 3.0;

public static final double DEFAULT_HIGH_GAS_PROPORTION = 1.0;
public static final double DEFAULT_MEDIUM_GAS_PROPORTION = 2.0;
public static final double DEFAULT_LOW_GAS_PROPORTION = 3.0;

public static final double DEFAULT_USAGE_UPPER = 0.6;

public static double getHighGasProportion() {
JSONObject plugin = Utils.getPlugin();
if(plugin!=null) return plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("properties").getJSONObject("HighGas").getDouble("gasProportion");
else return Gas.DEFAULT_HIGH_GAS_PROPORTION;
}

public static double getMediumGasProportion() {
JSONObject plugin = Utils.getPlugin();
if(plugin!=null) return plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("properties").getJSONObject("MediumGas").getDouble("gasProportion");
else return Gas.DEFAULT_MEDIUM_GAS_PROPORTION;
}

public static double getLowGasProportion() {
JSONObject plugin = Utils.getPlugin();
if(plugin!=null) return plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("properties").getJSONObject("LowGas").getDouble("gasProportion");
else return Gas.DEFAULT_LOW_GAS_PROPORTION;
}
public interface GasGraphTraverser {

public boolean useGas();
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/kclhi/hands/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@
import org.kclhi.hands.seeker.singleshot.random.FixedStartRandomWalk;
import org.kclhi.hands.seeker.singleshot.random.RandomWalk;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkHighGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkHighGasResourceVulnerable;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkHighGasResourceImmune;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkHighGasVariableGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkHighGasVariableImmune;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkLowGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkLowGasResourceImmune;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkLowGasVariableImmune;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkLowGasVariableGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkMediumGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkMediumGasResourceImmune;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkMediumGasVariableGas;
import org.kclhi.hands.seeker.singleshot.random.RandomWalkMediumGasVariableImmune;
import org.kclhi.hands.seeker.singleshot.random.SelfAvoidingRandomWalk;
import org.kclhi.hands.seeker.singleshot.random.SelfAvoidingRandomWalkGreedy;
Expand Down Expand Up @@ -957,14 +959,17 @@ private List<Seeker> initSeekers(String agentList, boolean mixSeekers) {
if (seekerName.contains("RandomWalk")) {

allSeekingAgents.add(
seekerName.contains("HighGasVariableImmune") ? new RandomWalkHighGasVariableImmune(graphController) :
seekerName.contains("HighGasResourceImmune") ? new RandomWalkHighGasResourceVulnerable(graphController) :
seekerName.contains("HighGasVariableImmune") ? new RandomWalkHighGasVariableImmune(graphController) :
seekerName.contains("HighGasResourceImmune") ? new RandomWalkHighGasResourceImmune(graphController) :
seekerName.contains("HighGasVariableGas") ? new RandomWalkHighGasVariableGas(graphController) :
seekerName.contains("HighGas") ? new RandomWalkHighGas(graphController) :
seekerName.contains("MediumGasVariableImmune") ? new RandomWalkMediumGasVariableImmune(graphController) :
seekerName.contains("MediumGasResourceImmune") ? new RandomWalkMediumGasResourceImmune(graphController) :
seekerName.contains("MediumGasVariableGas") ? new RandomWalkMediumGasVariableGas(graphController) :
seekerName.contains("MediumGas") ? new RandomWalkMediumGas(graphController) :
seekerName.contains("LowGasVariableImmune") ? new RandomWalkLowGasVariableImmune(graphController) :
seekerName.contains("LowGasResourceImmune") ? new RandomWalkLowGasResourceImmune(graphController) :
seekerName.contains("LowGasVariableGas") ? new RandomWalkLowGasVariableGas(graphController) :
seekerName.contains("LowGas") ? new RandomWalkLowGas(graphController) :
new RandomWalk(graphController)
);
Expand Down Expand Up @@ -1193,13 +1198,15 @@ protected double confidenceLevel() {

strategyPortfolioRandomSelection.clear();

strategyPortfolioRandomSelection.add(new Pair<AdaptiveSeeker, Double>(new RandomWalkAdaptable(graphController), 0.5));

strategyPortfolioRandomSelection.add(new Pair<AdaptiveSeeker, Double>(new MaxDistanceAdaptable(graphController, numberOfHideLocations), 0.5));
double leverageMaxDistanceProbability = Utils.getPlugin().getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("behaviour").getJSONObject("MetaRandom").getDouble("leverageProbability");

strategyPortfolioRandomSelection.add(new Pair<AdaptiveSeeker, Double>(new MaxDistanceAdaptable(graphController, numberOfHideLocations), leverageMaxDistanceProbability));

strategyPortfolioRandomSelection.add(new Pair<AdaptiveSeeker, Double>(new RandomWalkAdaptable(graphController), 1 - leverageMaxDistanceProbability));

allSeekingAgents.add(new AdaptiveSeekingAgent<AdaptiveSeeker>(graphController, "MetaRandom", strategyPortfolioRandomSelection, totalRounds, 1, false) {

/* ~MDC Should be moved into the actual strategy
/*
* (non-Javadoc)
* @see HideAndSeek.AdaptiveGraphTraversingAgent#confidenceLevel()
*/
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/kclhi/hands/Success.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

import java.util.HashMap;

import org.json.JSONObject;
import org.kclhi.hands.utility.Utils;

public class Success {

public static double BASE_NON_RESOURCE_IMMUNITY = 0.5;
public static double BASE_RESOURCE_IMMUNITY = 0.95;
// For seekers that aren't immune from poor payoff due to failed games, what is the extent of this lack of immunity (resp. for seekers that are immune)
public static double BASE_NON_RESOURCE_IMMUNITY = 0.0;
public static double BASE_RESOURCE_IMMUNITY = 1.0;

public static double DEFAULT_LEVERAGE_PROBABILITY = 0.6;

public interface ResourceImmuneTraverser {}
public interface VariableImmuneTraverser {}

// ~MDC Ideally this would be held within each immune seeker instance, like variable gas usage (as opposed to centrally)
public static boolean LEVERAGE_IMMUNITY(String traverserType) {

JSONObject plugin = Utils.getPlugin();
double leverageProbability = plugin!=null ? plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("behaviour").getJSONObject("VariableImmune").getDouble("leverageProbability") : DEFAULT_LEVERAGE_PROBABILITY;
HashMap<String, Double> usageUppers = new HashMap<String, Double>() {{
put("sRandomWalkLowGasVariableImmune", 0.6);
put("sRandomWalkMediumGasVariableImmune", 0.6);
put("sRandomWalkHighGasVariableImmune", 0.6);
put("sRandomWalkLowGasVariableImmune", leverageProbability);
put("sRandomWalkMediumGasVariableImmune", leverageProbability);
put("sRandomWalkHighGasVariableImmune", leverageProbability);
}};

if( usageUppers.keySet().contains(traverserType) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kclhi/hands/graph/HiddenObjectGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ private void resetTraversingAgent(GraphTraverser traverser) {

}

if(traverser instanceof GasGraphTraverser) traverserGas.put(traverser, (baseGasProportion * this.totalEdgeCosts(traverser)) + (traverser instanceof HighGasGraphTraverser ? this.totalEdgeCosts(traverser) / Gas.HIGH_GAS_PROPORTION : traverser instanceof MediumGasGraphTraverser ? this.totalEdgeCosts(traverser) / Gas.MEDIUM_GAS_PROPORTION : this.totalEdgeCosts(traverser) / Gas.LOW_GAS_PROPORTION));
if(traverser instanceof GasGraphTraverser) traverserGas.put(traverser, (baseGasProportion * this.totalEdgeCosts(traverser)) + (traverser instanceof HighGasGraphTraverser ? Gas.getHighGasProportion() * this.totalEdgeCosts(traverser) : traverser instanceof MediumGasGraphTraverser ? Gas.getMediumGasProportion() * this.totalEdgeCosts(traverser) : Gas.getLowGasProportion() * this.totalEdgeCosts(traverser)));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
* @author Martin
*
*/
public class RandomWalkHighGasResourceVulnerable extends RandomWalk implements HighGasGraphTraverser, ResourceImmuneTraverser {
public class RandomWalkHighGasResourceImmune extends RandomWalk implements HighGasGraphTraverser, ResourceImmuneTraverser {

public RandomWalkHighGasResourceVulnerable(GraphController<StringVertex, StringEdge> graphController) {
public RandomWalkHighGasResourceImmune(GraphController<StringVertex, StringEdge> graphController) {
super(graphController);
}

@Override
public boolean useGas() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.kclhi.hands.seeker.singleshot.random;

import org.json.JSONObject;
import org.kclhi.hands.Gas;
import org.kclhi.hands.Gas.HighGasGraphTraverser;
import org.kclhi.hands.graph.GraphController;
import org.kclhi.hands.graph.StringEdge;
import org.kclhi.hands.graph.StringVertex;
import org.kclhi.hands.utility.Utils;

/**
*
*
* @author Martin
*
*/
public class RandomWalkHighGasVariableGas extends RandomWalk implements HighGasGraphTraverser {

public RandomWalkHighGasVariableGas(GraphController<StringVertex, StringEdge> graphController) {
super(graphController);
}

@Override
public boolean useGas() {
JSONObject plugin = Utils.getPlugin();
double usageUpper = plugin!=null ? plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("behaviour").getJSONObject("Gas").getDouble("leverageProbability") : Gas.DEFAULT_USAGE_UPPER;
return Math.random() < usageUpper;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.kclhi.hands.seeker.singleshot.random;

import org.json.JSONObject;
import org.kclhi.hands.Gas;
import org.kclhi.hands.Gas.LowGasGraphTraverser;
import org.kclhi.hands.graph.GraphController;
import org.kclhi.hands.graph.StringEdge;
import org.kclhi.hands.graph.StringVertex;
import org.kclhi.hands.utility.Utils;

/**
*
Expand All @@ -13,15 +16,15 @@
*/
public class RandomWalkLowGasVariableGas extends RandomWalk implements LowGasGraphTraverser {

private double USAGE_UPPER = 0.6;

public RandomWalkLowGasVariableGas(GraphController<StringVertex, StringEdge> graphController) {
super(graphController);
}

@Override
public boolean useGas() {
return Math.random() < USAGE_UPPER;
JSONObject plugin = Utils.getPlugin();
double usageUpper = plugin!=null ? plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("behaviour").getJSONObject("Gas").getDouble("leverageProbability") : Gas.DEFAULT_USAGE_UPPER;
return Math.random() < usageUpper;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.kclhi.hands.seeker.singleshot.random;

import org.json.JSONObject;
import org.kclhi.hands.Gas;
import org.kclhi.hands.Gas.MediumGasGraphTraverser;
import org.kclhi.hands.graph.GraphController;
import org.kclhi.hands.graph.StringEdge;
import org.kclhi.hands.graph.StringVertex;
import org.kclhi.hands.utility.Utils;

/**
*
*
* @author Martin
*
*/
public class RandomWalkMediumGasVariableGas extends RandomWalk implements MediumGasGraphTraverser {

public RandomWalkMediumGasVariableGas(GraphController<StringVertex, StringEdge> graphController) {
super(graphController);
}

@Override
public boolean useGas() {
JSONObject plugin = Utils.getPlugin();
double usageUpper = plugin!=null ? plugin.getJSONObject("seekers").getJSONObject("variablesByType").getJSONObject("behaviour").getJSONObject("Gas").getDouble("leverageProbability") : Gas.DEFAULT_USAGE_UPPER;
return Math.random() < usageUpper;
}

}
15 changes: 15 additions & 0 deletions src/main/java/org/kclhi/hands/utility/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
Expand All @@ -48,6 +49,7 @@
import org.jgrapht.alg.FloydWarshallShortestPaths;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jibble.epsgraphics.EpsGraphics2D;
import org.json.JSONObject;
import org.mapdb.BTreeMap;
import org.mapdb.DB;
import org.mapdb.DBMaker;
Expand Down Expand Up @@ -93,6 +95,19 @@ public class Utils {
*/
public static String KEY = "trFdcuAh";

public static JSONObject getPlugin() {
Properties config = new Properties();
try (FileInputStream configInputStream = new FileInputStream("output/config.config")) {
config.load(configInputStream);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {}

if(config.getProperty("app.plugin") != null) {
return new JSONObject(String.join("\n", Utils.readFromFile(System.getProperty("user.dir") + "/plugins/" + config.getProperty("app.plugin") + ".json")));
}
return null;
}

/**
* @param cacheSizeInGB
* @return
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/kclhi/hands/utility/output/OutputManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,7 @@ public OutputManager(String dataInput, boolean recursiveSub) {

cache = new ArrayList<ArrayList<HiderRecord>>();

Properties config = new Properties();
try (FileInputStream configInputStream = new FileInputStream("output/config.config")) {
config.load(configInputStream);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {}

if(config.getProperty("app.plugin") != null) {
plugin = new JSONObject(String.join("\n", Utils.readFromFile(System.getProperty("user.dir") + "/plugins/" + config.getProperty("app.plugin") + ".json")));
}
plugin = Utils.getPlugin();

}

Expand Down Expand Up @@ -1318,15 +1310,15 @@ public int compare(TraverserRecord o1, TraverserRecord o2) {

String traverserName = storedTraverserAndData.getKey();
try {
traverserName = plugin == null ? storedTraverserAndData.getKey() : plugin.getJSONObject(storedTraverserAndData.getKey().startsWith("h") ? "hiders" : "seekers").getString(storedTraverserAndData.getKey());
traverserName = plugin == null ? storedTraverserAndData.getKey() : plugin.getJSONObject(storedTraverserAndData.getKey().startsWith("h") ? "hiders" : "seekers").getJSONObject("mapping").getString(storedTraverserAndData.getKey());
} catch(JSONException e) {
System.out.println("WARN: Plugin file incomplete: " + e.getMessage());
}

for( Entry<TraverserRecord, Double> matchedTraverserAndData : storedTraverserAndData.getValue() ) {

try {
matchedTraverserAndData.getKey().setTraverser(plugin == null ? matchedTraverserAndData.getKey().getTraverser() : plugin.getJSONObject(matchedTraverserAndData.getKey().getTraverser().startsWith("h") ? "hiders" : "seekers").getString(matchedTraverserAndData.getKey().getTraverser()));
matchedTraverserAndData.getKey().setTraverser(plugin == null ? matchedTraverserAndData.getKey().getTraverser() : plugin.getJSONObject(matchedTraverserAndData.getKey().getTraverser().startsWith("h") ? "hiders" : "seekers").getJSONObject("mapping").getString(matchedTraverserAndData.getKey().getTraverser()));
} catch(JSONException e) {
System.out.println("WARN: Plugin file incomplete: " + e.getMessage());
}
Expand Down

0 comments on commit 9abfcaa

Please sign in to comment.