HSRAvatar{{ private Config config; /** Constructor to be called during registration where you supply config **/ public HSRAvatar(Config cfg) { config = cfg; } /**proposing random unique claims which are not in the forbidden list**/ /**DataType: Instance * Instance is a, * (int, int) - (n, k) * **/ public List propose(List forbiddenClaims) { List claims = List.create(); SCGConfig scg_cfg = config.getScgCfg(); int maxProposals = scg_cfg.getMaxProposals() -1; for(int i =0; i< maxProposals;i++){ Claim claim = generateRandomClaim(); while(forbiddenClaims.contains(claim) || claims.contains(claim)) { claim = generateRandomClaim(); } claims = claims.append(claim); } return claims; } /**Random oppose method - randomly agrees, refutes or strengthens by a factor of 1 **/ public List oppose(List claimsToBeOpposed) { return claimsToBeOpposed.map(new List.Map() { public OpposeAction map(Claim claim) { HSRInstance instance = ((HSRInstanceSet) claim.getInstanceSet()) .getSingleton(); int n = instance.getN(); double q = claim.getQuality(); try { SolveRequest solveRequest = SolveRequest .parse("solve hsr.HSRInstance {{ " + instance + " }} " + claim); HSRSolution solution = (HSRSolution) solve(solveRequest); int depth = solution.findDepth(); double myQuality = (double) depth / (double) n; if (claim.getProtocol() instanceof ExistsForAll) { if (myQuality <= q) //Decide what is the appropriate action and return an object of its type. return null; else //Decide what is the appropriate action and return an object of its type. return null; } else if (claim.getProtocol() instanceof ForAllExists) { if (myQuality <= q) { //Decide what is the appropriate action and return an object of its type. return null; } else { //Decide what is the appropriate action and return an object of its type. return null; } } } catch (Exception e) { e.printStackTrace(); } return new Refuting(); } }); } /** providing instance - in HSR this is trivial as the instanceSet is singleton**/ public InstanceI provide(Claim claimToBeProvided){ HSRInstanceSet hsrInstanceSet = (HSRInstanceSet) claimToBeProvided.getInstanceSet(); HSRInstance hsrInstance = new HSRInstance(hsrInstanceSet.getSingleton().getN(), hsrInstanceSet.getSingleton().getK()); return hsrInstance; } public SolutionI solve(SolveRequest solveRequest) { HSRInstance hsrInstance = (HSRInstance) solveRequest.getInstance(); int n = hsrInstance.getN(); int k = hsrInstance.getK(); HSRAvatar sa = new HSRAvatar(); HSRSolution s = sa.smartSolve(n, k); System.gc(); return s; } private HSRSolution smartSolve(int n, int k) { int[][] arr = null; int r = 0; //where r = root row, arr is proceed array //initialize root row and proceed array HSRSolution solution = solve(r, n, k, arr); return solution; } /**DataType: Solution * HSRSolution is a, * Simple: (int) - Highest Safe Rung * Compound: (int, HSRSolution, HSRSolution) * **/ private HSRSolution solve(int r, int n, int k, int[][] arr) { HSRSolution leftYes = null, rightNo = null; int question = 0; //code to compute question, leftYes, rightNo return new Compound(question, leftYes, rightNo); } /** Generates random claim with N >=200 * @throws ParseException */ private Claim generateRandomClaim() { Random rand = new Random(); boolean nextBoolean = rand.nextBoolean(); int randN = 1000 + rand.nextInt(500); // check if N value is less than maxN in HSRConfig randN = compareN(rand,randN); int randQ = 1 + rand.nextInt(randN - 1); // generate hsr instance set HSRInstanceSet instanceSet = generatenewinstanceset(Random rand,int randN); // random claim for finding the solution Claim claim = generateclaim(nextBoolean,randQ,randN,instanceSet); return claim; } // check if N value is less than maxN in HSRConfig private int compareN(Random rand,int randN) { HSRConfig hsr_cfg = (HSRConfig) config.getDomainConfig(); if (randN > hsr_cfg.getMaxN()) { randN = 2 + rand.nextInt(hsr_cfg.getMaxN() - 2); } return randN; } private HSRInstanceSet generatenewinstanceset(Random rand,int randN) { int randK = 1 + rand.nextInt(20); int randQ = 1 + rand.nextInt(randN - 1); HSRInstance singleton = new HSRInstance(randN, randK); HSRInstanceSet instanceSet = new HSRInstanceSet(singleton); return instanceSet; } private Claim generateclaim(boolean nextBoolean,int randQ,int randN,HSRInstanceSet instanceSet) { Claim claim = new Claim(instanceSet, nextBoolean ? ForAllExistsMin .getInstance() : ExistsForAll.getInstance(), ((double) randQ) / randN, ((double) randQ) / randN); try { SolveRequest solveRequest = SolveRequest.parse("solve hsr.HSRInstance {{ " + singleton + " }} " + claim); HSRSolution solution = (HSRSolution) solve(solveRequest); // get the optimal solution int depth = solution.findDepth(); // update the quality for a optimal claim double myQuality = (double) depth / (double) randN; claim = new Claim(instanceSet, nextBoolean ? ForAllExistsMin .getInstance() : ExistsForAll.getInstance(), myQuality, 1.0); return claim; } catch (ParseException e) { e.printStackTrace(); } System.gc(); return claim; } }}