CSPAvatar{{ private Config config; /** Constructor to be called during registration where you supply config */ public CSPAvatar(Config cfg){ config = cfg; } /** proposing random unique claims which are not in the forbidden list */ public ProposeResponse propose(ProposeRequest pr){ List claims = List.create(); for(Claim c: pr.getForbidden()){ c.finishParse(); } for(int i=0;i() { public OpposeAction map(Claim claim){ Random rand = new Random(); int randOppose = rand.nextInt(3); if(randOppose == 0) return new NoOpposition(); else if(randOppose == 1){ double q = claim.getQuality(); SCGConfig scg_cfg = config.getScgCfg(); if (claim.getProtocol() instanceof PositiveSecret){ if(q > scg_cfg.getMinStrengthening()) return new Strengthening(claim.getQuality() - scg_cfg.getMinStrengthening()); else return new Refuting(); }else{ if(q < scg_cfg.getMinStrengthening()){ return new Strengthening(claim.getQuality() + scg_cfg.getMinStrengthening()); }else{ return new Refuting(); } } } else return new Refuting(); } })); } /** providing instance method - provides symmetric Instance of given instanceSet */ public ProvideResponse provide(ProvideRequest pr){ pr.getClaim().getInstanceSet().finishParse(); CSPInstanceSet cspInstanceSet = (CSPInstanceSet) pr.getClaim().getInstanceSet().getInstanceSet(); config.getDomainCfg().finishParse(); CSPConfig cfg = (CSPConfig) config.getDomainCfg().getConfig(); int numVars = cfg.getMaxVariables(); List vars = List.create(); for(int i =0; i< numVars;i++){ vars.append(new Var(new ident("" + i)));} CSPInstance cspInstance = new CSPInstance(vars, symmetric(cspInstanceSet.getType().toList(), numVars, 3)); Instance instance = null; try { instance = new Instance(FullyQualifiedClassName.parse("csp.CSPInstance") , cspInstance, new verbatim(cspInstance.print())); } catch (Exception e) { } return new ProvideResponse(instance); } /** solve using random assignment of values to all the variables */ public SolveResponse solve(SolveRequest sr){ CSPInstance i = (CSPInstance)sr.getInstance().getInstance(); Random rand = new Random(); CSPSolution solution = new CSPSolution(new ListMap(randomAssign(i.getVars(), rand.nextDouble()))); Solution s = null; try{ s= new Solution(FullyQualifiedClassName.parse("csp.CSPSolution") , solution, new verbatim(solution.print())); }catch(scg.ParseException pe){ } return new SolveResponse(s); } /** Random Assignment to each of the variables */ private List> randomAssign(List vs, final double bias){ return vs.map(new List.Map>() { public Entry map(Var v){ return Entry.create(v, Util.coinFlip(bias)); } }); } private Claim generateRandomClaim() { Claim claim = null; Random rand = new Random(); boolean nextBoolean = rand.nextBoolean(); ListSet type = ListSet.create(); config.getDomainCfg().finishParse(); CSPConfig cfg = (CSPConfig) config.getDomainCfg().getConfig(); if(cfg.getMaxRelNum() >= 127) type = type.add(127); type = type.add(rand.nextInt(cfg.getMaxRelNum())); CSPInstanceSet instanceSet = new CSPInstanceSet(type); try { InstanceSet instance = new InstanceSet(FullyQualifiedClassName.parse("csp.CSPInstanceSet"), instanceSet , new verbatim(instanceSet.print())); if(nextBoolean) claim = new Claim(instance, FullyQualifiedClassName.parse("csp.protocol.PositiveSecret"), new PositiveSecret(), 1); else claim = new Claim(instance, FullyQualifiedClassName.parse("hsr.protocol.NegativeSecret"), new NegativeSecret(), 1); } catch (Exception e) { } return claim; } /** Create a symmetric formula using the given Relation numbers */ public static Cons symmetric(List rs, int numVars, int rank){ final List> vars = combs(numVars, rank, new List.Build(){ public Var build(int n){ return new Var(new ident("v"+n)); } }); //** Create a list of Clauses for-all Relations with each the variable lists return (Cons) rs.fold(new List.Fold>(){ public List fold(final Integer r, final List rst){ return vars.fold(new List.Fold,List>(){ public List fold(List vs, List othrs){ return othrs.push(new Clause(r, 1, vs)); } }, rst); } }, List.create()); } /** N choose K, the dynamic programming way! ;) */ public static List> combs(int n, int k, List.Build b){ List>[][] tbl = new List[n+1][k+1]; for(int ik = 0; ik <= k; ik++) for(int in = 0; in <= n; in++){ List> newlst; if(ik == 0)newlst = List.>create(List.create()); else if(in < ik)newlst = List.create(); else newlst = tbl[in-1][ik].append(tbl[in-1][ik-1].map(new PushN(b.build(in)))); tbl[in][ik] = newlst; } return tbl[n][k]; } /** Sort the relations to find the most important one... */ public static int importantRelation(CSPInstanceSet t){ int rel = t.getType().toList().sort(new List.Comp(){ public boolean comp(Integer ra, Integer rb){ return (// Implied ((ra & rb) == ra) || // A is even, B is odd (ra%2 == 0 && rb%2 == 1) || // Both even or both odd ((ra%2 == rb%2) && bitsSet(ra) < bitsSet(rb))); } }).top(); return rel; } /** Determin the number of bits set in the given relation number */ public static int bitsSet(int i){ int c = 0; while(i > 0){ if((i&1) > 0)c++; i >>= 1; } return c; } /** Push the given number onto the front of each list */ private static class PushN extends List.Map,List>{ X x; public PushN(X xx){ x = xx; } public List map(List l){ return l.push(x); } } }}