CSPAvatar {{ private Config config; private Random random = new Random(); /** 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 List propose(List forbiddenClaims){ List claims = List.create(); for(int i=0;i oppose(List claimsToBeOpposed){ 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){ 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 InstanceI provide(Claim claimToBeProvided){ CSPInstanceSet cspInstanceSet = (CSPInstanceSet) claimToBeProvided.getInstanceSet(); CSPConfig cfg = (CSPConfig) config.getDomainConfig(); int numVars = cfg.getMaxVariables(); List vars = List.create(); /**bug found here, fixed by vars = vars.append... and ident("v"... */ for(int i = 1; i <= numVars;i++){ vars = vars.append(new Var(new ident("v" + i)));} CSPInstance cspInstance = new CSPInstance(vars, symmetric(cspInstanceSet.getType().toList(), numVars, 3)); return cspInstance; } /** solve using random assignment of values to all the variables */ public SolutionI solve(SolveRequest solveRequest){ CSPInstance i = (CSPInstance)solveRequest.getInstance(); Random rand = new Random(); CSPSolution solution = new CSPSolution(new ListMap(randomAssign(i.getVars(), rand.nextDouble()))); return solution; } /** 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, random.nextDouble() > bias); } }); } private Claim generateRandomClaim() { Random rand = new Random(); ListSet type = ListSet.create(); CSPConfig cfg = (CSPConfig) config.getDomainConfig(); if(cfg.getMaxRelNum() >= 127) type = type.add(127); type = type.add(rand.nextInt(cfg.getMaxRelNum())); CSPInstanceSet instanceSet = new CSPInstanceSet(type); SCGConfig scgConfig = config.getScgCfg(); // To Change: The protocol instance must be one of the // allowed protocols mentioned in SCGConfig Cons protocolsAllowed = scgConfig.getProtocols(); ProtocolI protocol = generateRandomAllowedProtocol(protocolsAllowed); return new Claim(instanceSet, protocol,1,1); } /** generates a random protocol instance from the given protocolsAllowed */ private ProtocolI generateRandomAllowedProtocol(Cons protocolsAllowed){ Random rand = new Random(); FullyQualifiedClassName randProtocol = protocolsAllowed.lookup(rand.nextInt(protocolsAllowed.length())); ProtocolI protocol = null; try{ Class protocolClass = Class.forName(randProtocol.print().trim()); Method instance = protocolClass.getMethod("parse", String.class); protocol = (ProtocolI) instance.invoke(null, randProtocol.print().trim()); }catch(Exception ex){ ex.printStackTrace(); } return protocol; } /** 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); } } }}