Skip to content

Commit

Permalink
Merge pull request #41 from dityas/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dityas authored Jan 7, 2020
2 parents 840e373 + 7046187 commit aa1f77f
Show file tree
Hide file tree
Showing 11 changed files with 603 additions and 8 deletions.
Binary file modified Protos/build/Protos.jar
Binary file not shown.
224 changes: 224 additions & 0 deletions Protos/src/thinclab/executables/CyberDeceptionAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* THINC Lab at UGA | Cyber Deception Group
*
* Author: Aditya Shinde
*
* email: shinde.aditya386@gmail.com
*/
package thinclab.executables;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.log4j.Logger;

import thinclab.belief.FullBeliefExpansion;
import thinclab.decisionprocesses.IPOMDP;
import thinclab.decisionprocesses.POMDP;
import thinclab.parsers.IPOMDPParser;
import thinclab.representations.conditionalplans.ConditionalPlanTree;
import thinclab.representations.policyrepresentations.PolicyGraph;
import thinclab.simulations.CyberDeceptionSimulation;
import thinclab.solvers.BaseSolver;
import thinclab.solvers.OfflinePBVISolver;
import thinclab.solvers.OfflineSymbolicPerseus;
import thinclab.solvers.OnlineInteractiveSymbolicPerseus;
import thinclab.utils.CustomConfigurationFactory;

/*
* @author adityas
*
*/
public class CyberDeceptionAgent extends Executable {

/*
* Defender agent for the cyber deception project
*/

private static Logger LOGGER;

// ------------------------------------------------------------------------------------

public static void main(String[] args) {

/* Parse CMD args */
CommandLineParser cliParser = new DefaultParser();
Options opt = new Options();

/* domain file */
opt.addOption("d", true, "path to the domain file");

/* log file */
opt.addOption("l", false, "log to file in results dir?");

/* backup iterations */
opt.addOption("b", true, "number of backups in each round");

/* solver rounds */
opt.addOption("r", true, "number of symbolic perseus rounds (always 1 for IPOMDPs)");

/* look ahead or search depth */
opt.addOption("n", true, "look ahead for IPOMDPs / SSGA depth for POMDPs");

/* IP address and port of env */
opt.addOption("a", "ip", true, "IP address for the env server");
opt.addOption("t", "port", true, "port for the env server");

/* simulation switch */
opt.addOption(
"x",
"interactions",
true,
"run these many iterations");

/* results storage directory */
opt.addOption(
"s",
"output",
true,
"directory where result files are to be stored. (Should be an existing dir)");

/* POMDP or IPOMDP switch */
opt.addOption(
"p",
"pomdp",
false,
"set if the domain is a POMDP domain");

opt.addOption(
"i",
"ipomdp",
false,
"set if the domain is a IPOMDP domain");

CommandLine line = null;

try {
line = cliParser.parse(opt, args);

/* set CLI args */

/* output directory */
String storageDir = line.getOptionValue("s");

/* if log file is given, initialize logging accordingly */
if (line.hasOption("l"))
CustomConfigurationFactory.setLogFileName(storageDir + "/solver.log");

CustomConfigurationFactory.initializeLogging();
LOGGER = Logger.getLogger(RunSimulations.class);

/* set domain file */
String domainFile = line.getOptionValue("d");

/* set look ahead */
int backups = new Integer(line.getOptionValue("b"));

/* set look ahead */
int lookAhead = new Integer(line.getOptionValue("n"));

/* get IP address and port of the env server */
String envIP = line.getOptionValue("a");
int envPort = new Integer(line.getOptionValue("t"));

/* conditional plan and policy graph */
if (line.hasOption("p")) {
LOGGER.info("Simulating POMDP...");

int rounds = new Integer(line.getOptionValue("r"));
int simLength = new Integer(line.getOptionValue("x"));

LOGGER.info("Starting simulation");

POMDP pomdp = new POMDP(domainFile);
OfflineSymbolicPerseus solver =
OfflineSymbolicPerseus.createSolverWithSSGAExpansion(
pomdp, lookAhead, 2, rounds, backups);

solver.solve();

CyberDeceptionSimulation cyberDecSim =
new CyberDeceptionSimulation(solver, simLength, envIP, envPort);

cyberDecSim.runSimulation();
cyberDecSim.logToFile(storageDir + "/" + "interaction.json");
cyberDecSim.writeDotFile(storageDir, "interaction");

}

/* for IPOMDPs */
else if (line.hasOption("i")) {

LOGGER.info("Simulating IPOMDP...");
int simLength = new Integer(line.getOptionValue("x"));

LOGGER.info("Starting simulation");

/* initialize IPOMDP */
IPOMDPParser parser = new IPOMDPParser(domainFile);
parser.parseDomain();

IPOMDP ipomdp = new IPOMDP(parser, lookAhead, simLength * 2);

for (BaseSolver solver : ipomdp.lowerLevelSolutions) {

/* set context */
solver.f.setGlobals();

/* make policy graph */
PolicyGraph pg = new PolicyGraph((OfflinePBVISolver) solver);
pg.makeGraph();

/* store policy graph solution */
pg.writeDotFile(
storageDir,
"policy_graph_frame_" + pg.solver.f.frameID);

pg.writeJSONFile(
storageDir,
"policy_graph_frame_" + pg.solver.f.frameID);

/* make conditional plan */
ConditionalPlanTree T = new ConditionalPlanTree(solver, simLength);
T.buildTree();

/* Store conditional Plan */
T.writeDotFile(
storageDir,
"plan_frame_" + T.f.frameID);

T.writeJSONFile(
storageDir,
"plan_frame_" + T.f.frameID);
}

ipomdp.clearLowerLevelSolutions();

/* set context back to IPOMDP */
ipomdp.setGlobals();

OnlineInteractiveSymbolicPerseus solver =
new OnlineInteractiveSymbolicPerseus(
ipomdp,
new FullBeliefExpansion(ipomdp), 1, backups);

CyberDeceptionSimulation cyberDecSim =
new CyberDeceptionSimulation(solver, simLength, envIP, envPort);
cyberDecSim.runSimulation();

cyberDecSim.logToFile(storageDir + "/" + "interaction.json");
cyberDecSim.writeDotFile(storageDir, "interaction");
}

}

catch (Exception e) {
System.out.println("While parsing args: " + e.getMessage());
Executable.printHelp(opt);
e.printStackTrace();
System.exit(-1);
}

}
}
2 changes: 1 addition & 1 deletion Protos/src/thinclab/executables/IPOMDPSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static void main(String[] args) {
solver.makeConditionalPlan(planDir);
}

/* conditional plan and policy graph */
/* sim */
if (line.hasOption("t")) {
int simIters = Integer.parseInt(line.getOptionValue("t"));
solver.initializeIPOMDP(simIters * 2);
Expand Down
2 changes: 1 addition & 1 deletion Protos/src/thinclab/executables/RunSimulations.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ else if (line.hasOption("i")) {
StochasticSimulation ss = new StochasticSimulation(solver, simLength);
ss.runSimulation();

ss.logToFile(storageDir + "/" + "sim" + i + ".csv");
ss.logToFile(storageDir + "/" + "sim" + i + ".json");
ss.writeDotFile(storageDir, "sim" + i);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* THINC Lab at UGA | Cyber Deception Group
*
* Author: Aditya Shinde
*
* email: shinde.aditya386@gmail.com
*/
package thinclab.simulations;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

/*
* @author adityas
*
*/
public class CyberDeceptionEnvironmentConnector extends NetworkedEnvironmentConnector {

/*
* Defines the network connector for the cyber deception domain
*/

private static final Logger LOGGER =
Logger.getLogger(CyberDeceptionEnvironmentConnector.class);

// -----------------------------------------------------------------------------------

public CyberDeceptionEnvironmentConnector(String envIP, int envPort) {

super(envIP, envPort);
LOGGER.info("Initialized CyberDeceptionEnvironmentConnector");
}

// -----------------------------------------------------------------------------------

@Override
public boolean syncEstablished(BufferedReader envInStream) {

String hello = "";

try {
hello = envInStream.readLine().trim();
}

catch (IOException e) {
LOGGER.error("While reading from socket's instream: " + e.getMessage());
e.printStackTrace();
System.exit(-1);
}

/* defender agent should try to handshake with a 'hello' */
if (hello.contains("hello")) return true;
else {
LOGGER.debug("Expected 'hello', got " + hello);
return false;
}
}

@Override
public void waitForAck(BufferedReader envInStream) {

while (true) {

String ack = "";

try {
ack = envInStream.readLine().trim();
}

catch (Exception e) {
LOGGER.error("While reading stream for ack: " + e.getMessage());
e.printStackTrace();
System.exit(-1);
}

if (ack.contains("done")) break;

else {

LOGGER.warn("Did not get ack");
LOGGER.debug("Retrying in 1 second");

try {
TimeUnit.SECONDS.sleep(1);
}

catch (InterruptedException e) {
LOGGER.error("While sleeping");
e.printStackTrace();
System.exit(-1);
}
}
}

}

@Override
public void sendAck(PrintWriter envOutStream) {
envOutStream.println("done");
}

@Override
public String[] handleObservation(String obs) {
return obs.split(";");
}

}
Loading

0 comments on commit aa1f77f

Please sign in to comment.