NWFAvatar {{ private Config config; /** Constructor to be called during registration where you supply config */ public NWFAvatar(Config cfg){ config = cfg; } /** proposing random unique claims which are not in the forbidden list */ public List propose(List forbiddenClaims){ List claims = List.create(); SCGConfig scg_cfg = config.getScgCfg(); int maxProposals = scg_cfg.getMaxProposals() - 1; Claim claim = null; for (int i = 0; i < maxProposals; i++) { claim = generateRandomClaim(); while (forbiddenClaims.contains(claim) || claims.contains(claim)) { claim = generateRandomClaim(); } claims = claims.append(claim); } return claims; } /** * Generates random claim * * The random graph would be a tree, which only has * one path from source to sink. The capacity for each * edge is a constant 10. The depth is decided by the * a random integer. * * Basically this graph at least has a source and a sink. private Claim generateRandomClaim() { Random rand = new Random(); boolean nextBoolean = rand.nextBoolean(); NWFConfig nwf_cfg = (NWFConfig)config.getDomainConfig(); int maxN = nwf_cfg.getMaxN(); int randN = rand.nextInt(maxN); // from 0 to maxN - 1; if (randN < 2) { randN = 2; } String instanceBody = ""; String inpt = ""; // build the graph for (int i = 1; i <= randN-1 ; i++ ) { int j = i + 1; instanceBody += "\"" + i + "\"" + "successors" + "(\"" + j + "\"" + "c 10 f 0" + ")"; } inpt = "instance " + instanceBody + "source \"" + 1 + "\" " + "sink \"" + randN + "\""; NWFInstance singleton = null; try { singleton = NWFInstance.parse(inpt); } catch (nwf.ParseException e) { e.printStackTrace(); } NWFInstanceSet instanceSet = new NWFInstanceSet(singleton); return new Claim(instanceSet, nextBoolean?ForAllExists.getInstance(): ExistsForAll.getInstance(), 0.5, 0.5); } */ private Claim generateRandomClaim() { String inpt = "instance " + "\"1\" successors (\"2\" c 10 f 0)" + "source \"1\"" + "sink \"2\""; NWFInstance singleton = null; try { singleton = NWFInstance.parse(inpt); } catch (nwf.ParseException e) { e.printStackTrace(); } NWFInstanceSet instanceSet = new NWFInstanceSet(singleton); return new Claim(instanceSet, ForAllExists.getInstance(), 0.5, 0.5); } /** Random oppose method - randomly agrees, refutes or strengthens */ public List oppose(List claimsToBeOpposed){ final Random r = new Random(); return claimsToBeOpposed.map(new List.Map() { public OpposeAction map(Claim claim){ Random rand = new Random(); int randOppose = rand.nextInt(3); if(randOppose == 0) return new Agreement(); else if(randOppose == 1){ NWFInstanceSet is = (NWFInstanceSet)claim.getInstanceSet(); SCGConfig scg_cfg = config.getScgCfg(); if (claim.getProtocol() instanceof ForAllExists){ return new Strengthening(claim.getQuality() - scg_cfg.getMinStrengthening()); }else{ return new Strengthening(claim.getQuality() + scg_cfg.getMinStrengthening()); } }else return new Refuting(); } }); } /** providing instance method - provides symmetric Instance of given instanceSet */ public InstanceI provide(Claim claimToBeProvided){ NWFInstanceSet nwfInstanceSet = (NWFInstanceSet)claimToBeProvided.getInstanceSet(); NWFInstance nwfInstance = new NWFInstance(nwfInstanceSet.getSingleton().getG(), nwfInstanceSet.getSingleton().getSource(), nwfInstanceSet.getSingleton().getSink()); return nwfInstance; } /** solve using random assignment of values to all the variables */ public SolutionI solve(SolveRequest solveRequest){ NWFInstance nwfInstance = (NWFInstance)solveRequest.getInstance(); NWFSolution solution = solve(nwfInstance); return solution; } /** * the simple answer for a networkflow is neteworkflow with all its * edge has zero flow value. * * @param nwfInstance * @return */ private NWFSolution solve(NWFInstance nwfInstance) { return new NWFSolution(nwfInstance.getG(), nwfInstance.getSource(), nwfInstance.getSink()); } }}