Skip to content

Commit

Permalink
Merge pull request #52 from dityas/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dityas authored Jan 21, 2020
2 parents 7614c58 + 0e3e92b commit a86ba82
Show file tree
Hide file tree
Showing 19 changed files with 1,084 additions and 84 deletions.
Binary file modified Protos/build/Protos.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions Protos/domains/tiger.L1multiple_new_parser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ tolerance 0.001
action listen
tiger-location (SAMEtiger-location)
observe
growl (tiger-location' (tiger-left (growl' (growl-left (0.85))
(growl-right (0.15))))
(tiger-right (growl' (growl-left (0.15))
(growl-right (0.85)))))
growl (tiger-location' (tiger-left (growl' (growl-left (0.75))
(growl-right (0.25))))
(tiger-right (growl' (growl-left (0.25))
(growl-right (0.75)))))
endobserve
cost (1)
endaction
Expand Down
68 changes: 68 additions & 0 deletions Protos/src/thinclab/belief/IBeliefOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,74 @@ public DD beliefUpdate(
return OP.div(nextBelief, norm);
}


public DD beliefUpdate(
DD belief, String action, int[][] obsVals) throws ZeroProbabilityObsException {

/*
* For use with sampling API that returns int[][] obsVals instead of
* String array of observations
*
* Level 1 belief update
*
* P(S, Mj| Oi'=o) =
* norm x Sumout[S, Mj, Thetaj, Aj]
* f(S, Mj) x f(Thetaj, Mj) x f(Aj, Mj)
* x f(S', S, Aj) x f(Oi'=o, S', Aj)
* x Sumout[Oj']
* f(Oj', Aj, Thetaj, S') x f(Mj', Mj, Aj, Oj')
*/

IPOMDP DPRef = this.getIPOMDP();

/* Restrict Oi */
DD[] restrictedOi =
OP.restrictN(
DPRef.currentOi.get(action),
obsVals);

/* Collect f1 = P(S, Mj) */
DD f1 = belief;

/* Collect f2 = P(Aj | Mj) x P(Thetaj| Mj) x P(Oi'=o, S', Aj) x P (S', Aj, S) */
DD[] f2 =
ArrayUtils.addAll(
ArrayUtils.addAll(
DPRef.currentTi.get(action),
new DD[] {DPRef.currentAjGivenMj, DPRef.currentThetajGivenMj}),
restrictedOi);

/* Get TAU */
DD tau = DPRef.currentTau;

/* Perform the sum out */
DD nextBelief =
OP.addMultVarElim(
ArrayUtils.add(
ArrayUtils.addAll(f2, f1),
tau),
DPRef.stateVarIndices);

/* Shift indices */
nextBelief = OP.primeVars(nextBelief, -(DPRef.S.size() + DPRef.Omega.size()));

/* compute normalization factor */
DD norm =
OP.addMultVarElim(
nextBelief,
ArrayUtils.subarray(
DPRef.stateVarIndices,
0,
DPRef.thetaVarPosition));

if (norm.getVal() < 1e-8)
throw new ZeroProbabilityObsException(
"Observation " + Arrays.deepToString(obsVals)
+ " not possible at belief " + belief);

return OP.div(nextBelief, norm);
}

@Override
public DD getObsDist(DD belief, String action) {
/*
Expand Down
54 changes: 46 additions & 8 deletions Protos/src/thinclab/belief/SSGABeliefExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.apache.log4j.Logger;

import thinclab.decisionprocesses.IPOMDP;
import thinclab.decisionprocesses.POMDP;
import thinclab.exceptions.ZeroProbabilityObsException;
import thinclab.legacy.DD;
Expand Down Expand Up @@ -83,6 +84,25 @@ public SSGABeliefExpansion(POMDP p, int maxDepth, int iterations) {
logger.debug("SSGA expansion search initialized");
}

public SSGABeliefExpansion(IPOMDP ip, int iterations) {
/*
* Constructor for IPOMDPs
*/
super(ip.mjLookAhead);

this.setFramework(ip);

this.nIterations = iterations;
this.allPossibleObs = this.f.getAllPossibleObservations();

/* initialize with initial belief of IPOMDP */
this.initialBeliefs = new ArrayList<DD>();
this.initialBeliefs.addAll(this.f.getInitialBeliefs());

this.exploredBeliefs = new HashSet<DD>();
this.exploredBeliefs.addAll(this.initialBeliefs);
}

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

public void setRecentPolicy(DD[] aVectors, int[] policy) {
Expand Down Expand Up @@ -122,24 +142,42 @@ public void expand() {
/* action sampling */
int act;

if (usePolicy == 0)
if (usePolicy == 0) {
act =
p.getActions().indexOf(
this.f.getActions().indexOf(
POMDP.getActionFromPolicy(
p, belief, this.alphaVectors, this.policy));
this.f, belief, this.alphaVectors, this.policy));
}

else act = Global.random.nextInt(this.f.getActions().size());

DD obsDist = this.f.getObsDist(belief, this.f.getActions().get(act));

int[][] obsConfig = OP.sampleMultinomial(obsDist, p.primeObsIndices);
int[][] obsConfig = null;

if (this.f.getType().contentEquals("POMDP"))
obsConfig = OP.sampleMultinomial(obsDist, p.primeObsIndices);

else
obsConfig =
OP.sampleMultinomial(obsDist, ((IPOMDP) this.f).obsIVarPrimeIndices);

/* Get next belief */
try {

DD nextBelief = ((BeliefOps) p.bOPs).beliefUpdate(belief,
p.getActions().get(act),
obsConfig);

DD nextBelief = null;

if (this.f.getType().contentEquals("POMDP")) {
nextBelief = ((BeliefOps) this.f.bOPs).beliefUpdate(belief,
p.getActions().get(act),
obsConfig);
}

else {
nextBelief = ((IBeliefOps) this.f.bOPs).beliefUpdate(belief,
this.f.getActions().get(act),
obsConfig);
}

/* Add belief point if it doesn't already exist */
if (!this.exploredBeliefs.contains(nextBelief))
Expand Down
127 changes: 127 additions & 0 deletions Protos/src/thinclab/belief/SparseFullBeliefExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* THINC Lab at UGA | Cyber Deception Group
*
* Author: Aditya Shinde
*
* email: shinde.aditya386@gmail.com
*/
package thinclab.belief;

import java.util.ArrayList;
import java.util.HashSet;

import org.apache.log4j.Logger;

import thinclab.decisionprocesses.IPOMDP;
import thinclab.decisionprocesses.POMDP;
import thinclab.exceptions.ZeroProbabilityObsException;
import thinclab.legacy.DD;
import thinclab.legacy.OP;

/*
* @author adityas
*
*/
public class SparseFullBeliefExpansion extends FullBeliefExpansion {

/*
* Do sparse belief tree exploration
*
* Expand for all actions but sample observations
*/

private int nIters = 30;

private static final long serialVersionUID = 1571517162981801344L;

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

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

public SparseFullBeliefExpansion(IPOMDP ip, int iterations) {
super(ip);
this.nIters = iterations;
}

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

@Override
public void expand() {

for (int s = 0; s < this.nIters; s++) {

LOGGER.debug("Starting new sampling iteration");

this.leaves.clear();
this.leaves.add(this.f.getCurrentBelief());

/* H - 1 expansions to let the Solver reach H during solving */
for (int i = 0; i < this.getHBound() - 1; i++)
this.expandSingleStep();
}
}

public void expandSingleStep() {
/*
* Runs one step of expansion from leaves and adds to exploredBeliefs
*/

LOGGER.debug("Beginning expansion from " + this.leaves.size() + " beliefs.");

HashSet<DD> newLeaves = new HashSet<DD>();

/* Expand from all current leaves */
for (DD leaf : this.leaves) {

/* For all actions */
for (String action : this.f.getActions()) {

/*
* Sample observations here
*/
DD obsDist = this.f.getObsDist(leaf, action);
int[][] obsConfig = null;
DD nextBelief = null;

try {
if (this.f.getType().contentEquals("POMDP")) {
obsConfig = OP.sampleMultinomial(obsDist, ((POMDP) this.f).primeObsIndices);
nextBelief =
((BeliefOps) this.f.bOPs).beliefUpdate(
leaf, action, obsConfig);
}

else {
obsConfig =
OP.sampleMultinomial(obsDist, ((IPOMDP) this.f).obsIVarPrimeIndices);
nextBelief =
((IBeliefOps) this.f.bOPs).beliefUpdate(
leaf, action, obsConfig);
}
}

catch (ZeroProbabilityObsException e) {
}

catch (Exception e) {
LOGGER.error("While doing belief update: " + e.getMessage());
e.printStackTrace();
System.exit(-1);
}

/* continue if observation probability was 0 */
if (nextBelief == null) continue;

if (!this.exploredBeliefs.contains(nextBelief)) {
this.exploredBeliefs.add(nextBelief);
newLeaves.add(nextBelief);
}

} /* for nActions */
} /* while leafIterator */

LOGGER.debug("Found " + newLeaves.size() + " more beliefs in the expansion phase.");
LOGGER.debug(this.exploredBeliefs.size() + " points explored in the belief space.");
this.leaves = new ArrayList<DD>(newLeaves);
}
}
Loading

0 comments on commit a86ba82

Please sign in to comment.