CSPInstance {{ public double valid(SolutionI solution, Config config){ CSPSolution cspSolution = (CSPSolution) solution; if(cspSolution.getAssign().size() != getVars().length()) return 0; boolean isValid = true; for(Iterator vars = getVars().iterator(); vars.hasNext();){ isValid &= cspSolution.getAssign().containsKey(vars.next()); } if(isValid) return 1; else return 0; } public double quality(SolutionI solution){ CSPSolution cspSolution = (CSPSolution) solution; List> s = cspSolution.getAssign().toList(); List list = getClauses(); List reduced = s.fold(new List.Fold,List>(){ public List fold(Entry a, List p){ return reduce(p, a.getKey(), a.getVal()); } },list); return satisfiedRatio(reduced); } /** Reduce a given Problem based on a single Literal assignment */ public static List reduce(List clauses, final Var var, final boolean val){ return clauses.map(new List.Map() { public Clause map(Clause c){ int i = c.getVars().index(var); if(i < 0 || (c.getRelnum() == 255))return c; return new Clause(new Relation(3, c.getRelnum()) .reduce(i, val?1:0),1, c.getVars()); } }); } /** Calculate the satifaction ratio of a (reduced) Problem */ public static double satisfiedRatio(List clauses){ return clauses.fold(new List.Fold(){ public R fold(Clause c, R r) { return r.add((c.getRelnum() == 255)?c.getWeight():0, c.getWeight()); } }, new R(0,0)).result(); } /** Accumulation for the satisfaction ratio */ static class R{ double top, bot; R(double t, double b){ top = t; bot = b; } R add(int t, int b){ return new R(top+t, bot+b); } double result(){ return bot==0.0?1.0:top/(double)bot; } } }} CSPInstanceSet {{ /** Is this a valid/well-formed Instance of the given instance? */ public Option belongsTo(InstanceI instance){ //check if relnum in each clause is present in InstanceSet CSPInstance i = (CSPInstance)instance; boolean valid = true; for(Iterator clauses = i.getClauses().iterator(); clauses.hasNext();){ Clause clause = clauses.next(); valid &= getType().contains(clause.getRelnum()); } if(!valid){ return new Some("The instance " + i.print() + " is different from " + this.print() + "."); } return new None(); } /** Is this a valid/well-formed InstanceSet? */ public Option valid(Config config){ //Semantic checks for(Iterator relnums = getType().iterator(); relnums.hasNext();){ int relnum = relnums.next(); if(relnum < 0){ return new Some("The Instance set has a relnum < 0"); }else if(relnum > 255){ return new Some("The Instance set has a relnum > 255"); } } return new None(); } }} Var {{ public int compareTo(Var v) { return id.compareTo(v.id); } }} CSPConfig {{ private static CSPConfig DEFAULT_CSP_CONFIG; static{ try{ DEFAULT_CSP_CONFIG = CSPConfig.parse( "csp_config[\n" + "maxRelNum: 255\n" + "maxVariables: 10\n" + "]" ); }catch(Exception ex){ ex.printStackTrace(); } } public static CSPConfig getDefaultDomainConfig(){ return CSPConfig.DEFAULT_CSP_CONFIG; } public static Config getDefaultConfig(){ return new Config(SCGConfig.getDefaultSCGConfig(), getDefaultDomainConfig()); } }}