HSRInstance {{ public double valid(SolutionI solution, Config config){ HSRSolution hsrSolution = (HSRSolution) solution; boolean isValid = lessThanEqualKBranches(hsrSolution, 0) && areNodesValid(hsrSolution); if(isValid) return 1; else return 0; } private boolean lessThanEqualKBranches(HSRSolution s, int jarsBroken){ if(jarsBroken > k) return false; else{ if(s instanceof Compound){ return lessThanEqualKBranches(((Compound) s).getYes(), jarsBroken +1) && lessThanEqualKBranches(((Compound) s).getNo(), jarsBroken); }else{ return true; } } } private boolean areNodesValid(HSRSolution s){ java.util.List internalNodes = buildIntList(getN()); internalNodes.remove(0); java.util.List leaves = buildIntList(getN()); return nodesValid(s, internalNodes, leaves) && internalNodes.isEmpty() && leaves.isEmpty(); } private java.util.List buildIntList(int bound){ java.util.List intList = new java.util.ArrayList(); for(int i=0; i internalNodes, java.util.List leaves){ if(s instanceof Compound){ Integer question = new Integer(((Compound) s).getQuestion()); if(internalNodes.contains(question)){ internalNodes.remove(question); return nodesValid(((Compound) s).getYes(), internalNodes, leaves) & nodesValid(((Compound) s).getNo(), internalNodes, leaves); }else{ return false; } }else{ Integer hsr = new Integer(((Simple) s).getHighest_safe_rung()); if(leaves.contains(hsr)){ leaves.remove(hsr); return true; }else{ return false; } } } public double quality(SolutionI solution){ HSRSolution hsrSolution = (HSRSolution) solution; if(n >0){ return (double)hsrSolution.findDepth()/n; } return 0; } }} HSRSolution {{ /** Find the depth of the Binary tree **/ public int findDepth(){ int depth = 0; if(this instanceof Compound){ depth = Math.max(((Compound)this).getYes().findDepth(), ((Compound)this).getNo().findDepth()) +1; } return depth; } }} HSRInstanceSet {{ /** Is this a valid/well-formed Instance of the given instance? */ public Option belongsTo(InstanceI instance){ HSRInstance i = (HSRInstance)instance; if(!i.equals(getSingleton())){ return new Some("The instance " + i.print() + " is different from " + getSingleton().print() + "."); } return new None(); } /** Is this a valid/well-formed InstanceSet? */ public Option valid(Config config){ HSRInstance i = getSingleton(); int n = i.getN(); int k = i.getK(); if(n <= 0){ return new Some("The Instance set has a n <= 0"); }else if(k<=0){ return new Some("The Instance set has a k <= 0"); } return new None(); } }} HSRConfig {{ private static HSRConfig DEFAULT_HSR_CONFIG; static{ try{ DEFAULT_HSR_CONFIG = HSRConfig.parse( "hsr_config[\n" + "maxN: 10000\n" + "]" ); }catch(Exception ex){ ex.printStackTrace(); } } public static HSRConfig getDefaultDomainConfig(){ return HSRConfig.DEFAULT_HSR_CONFIG; } public static Config getDefaultConfig(){ return new Config(SCGConfig.getDefaultSCGConfig(), getDefaultDomainConfig()); } }}