Advertisement
Sax

Constraint_satisfaction/Run.java

Sax
Feb 8th, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.97 KB | None | 0 0
  1.  
  2. import CSP.CSP;
  3. import CSP.Solvers.Constructive.ConstructiveSolver;
  4. import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic;
  5. import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic.ValueOrderingHeuristic;
  6. import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic.VariableOrderingHeuristic;
  7. import CSP.CSPSet;
  8. import CSP.CSPSet.Subset;
  9. import CSP.Solvers.HyperHeuristic.CSPEvaluationFunction;
  10. import GA.EvaluationFunction;
  11. import static GA.GeneticAlgorithm.Type.STEADY_STATE;
  12. import GA.SelectionOperator;
  13. import GA.TournamentSelectionOperator;
  14. import HERMES.Exceptions.NoSuchFeatureException;
  15. import HERMES.Selector.GA.RuleBasedHeuristicSelector;
  16. import HERMES.Selector.GA.RuleBasedHeuristicSelectorFramework;
  17. import HERMES.Selector.GA.RuleBasedHeuristicSelectorIndividual;
  18. import HERMES.Selector.HeuristicSelector;
  19. import java.text.DecimalFormat;
  20. import java.util.Arrays;
  21. import java.util.Random;
  22.  
  23. public class Run {
  24.  
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) {        
  29.        
  30.         String set;        
  31.         ConstructiveHeuristic[] heuristics;
  32.         RuleBasedHeuristicSelector selector;
  33.         long seed;
  34.        
  35.         //seed = (new Random()).nextLong();
  36.         seed = 1101813;
  37.         //set = "../Instances/MyTest";
  38.         //set = "../Instances/PATT/bqwh-15-106";
  39.         //set = "../Instances/PATT/bqwh-18-141";
  40.         //set = "../Instances/PATT/coloring";
  41.         //set = "../Instances/QRND/composed-25-1-2";
  42.         set = "../Instances/QRND/ehi-85";
  43.         //set = "../Instances/QRND/geom";
  44.         //set = "../Instances/RAND/rand-2-30-15";
  45.        
  46.         //solve(new CSP(set + "/qcp-15-120-3_ext.xml"), new ConstructiveHeuristic(VariableOrderingHeuristic.WDEG, ValueOrderingHeuristic.MINC), 25000);
  47.         //System.exit(1);
  48.         heuristics = new ConstructiveHeuristic[]{
  49.             new ConstructiveHeuristic(VariableOrderingHeuristic.DOM, ValueOrderingHeuristic.MINC),
  50.             new ConstructiveHeuristic(VariableOrderingHeuristic.DEG, ValueOrderingHeuristic.MINC),
  51.             new ConstructiveHeuristic(VariableOrderingHeuristic.KAPPA, ValueOrderingHeuristic.MINC),
  52.             new ConstructiveHeuristic(VariableOrderingHeuristic.WDEG, ValueOrderingHeuristic.MINC),
  53.             new ConstructiveHeuristic(VariableOrderingHeuristic.DOMDEG, ValueOrderingHeuristic.MINC)
  54.         };        
  55.        
  56.         //generateHeuristicSelector(set, 1.0, seed);
  57.         solve(new CSPSet(set, Subset.TEST, 0.5, seed), heuristics, 20000);
  58.         //extractFeatures(new CSPSet(set, Subset.TEST, 0.5, seed), new String[]{"MEAN_P1", "MEAN_P2", "MEAN_C", "K"});
  59.         System.exit(1);        
  60.         //solve(new CSPSet(set, Subset.TRAIN, 0.05, 12345), heuristics, 2000);
  61.        
  62.         selector = new RuleBasedHeuristicSelector("test.xml");
  63.         solve(new CSPSet(set), selector, 2000);
  64.         //solve(new CSPSet(set, Subset.TRAIN, 0.05, seed), selector, 2000);
  65.         System.out.println("Heuristic use: " + Arrays.toString(selector.getHeuristicUse()));        
  66.         System.out.println("Rule use: " + Arrays.toString(selector.getRuleUse()));
  67.  
  68.     }
  69.  
  70.     /**
  71.      * Extracts the features of a set of CSP instances.
  72.      * <p>
  73.      * @param set The sets of CSP instances to solve.
  74.      * @param features The features to extract from every instance in the set.
  75.      */
  76.     public static void extractFeatures(CSPSet set, String[] features) {
  77.         StringBuilder string;
  78.         ConstructiveSolver solver;
  79.         DecimalFormat format;
  80.         format = new DecimalFormat("0.0000");
  81.         /*
  82.          * Prints the header.
  83.          */
  84.         string = new StringBuilder();
  85.         //string.append("INSTANCE, ");
  86.         string.append("INSTANCE\t ");
  87.         for (String feature : features) {
  88.             //string.append(feature).append(", ");
  89.             string.append(feature).append("\t ");
  90.         }
  91.         string.delete(string.length() - 2, string.length());
  92.         System.out.println(string.toString());
  93.         for (CSP csp : set.getInstances()) {
  94.             string = new StringBuilder();
  95.             //string.append(csp.getId()).append(", ");
  96.             string.append(csp.getId()).append("\t ");
  97.             for (String feature : features) {
  98.                 solver = new ConstructiveSolver(csp);
  99.                 try {
  100.                     //string.append(format.format(solver.getFeature(feature))).append(", ");
  101.                     string.append(format.format(solver.getFeature(feature))).append("\t ");
  102.                 } catch (NoSuchFeatureException e) {
  103.                     System.out.println("Feature \'" + feature + "\' is not recognized by the problem domain.");
  104.                     System.out.println("The system will halt.");
  105.                     System.exit(1);
  106.                 }                
  107.             }
  108.             string.delete(string.length() - 2, string.length());
  109.             System.out.println(string.toString());
  110.         }        
  111.     }
  112.        
  113.     /**
  114.      * Solves a CSP instance by using a specific constructive heuristic.
  115.      * <p>
  116.      * @param csp The CSP instance to solve.
  117.      * @param heuristic The constructive heuristic to use.
  118.      * @param maxTime The maximum time (in milliseconds) the solver is allowed to run.
  119.      */
  120.     public static void solve(CSP csp, ConstructiveHeuristic heuristic, long maxTime) {
  121.         int result;
  122.         ConstructiveSolver solver;
  123.         solver = new ConstructiveSolver(csp);
  124.         result = solver.solve(heuristic, false, maxTime);
  125.         System.out.println("Solutions found: " + solver.getNumberOfSolutions());
  126.         switch (result) {
  127.             case 1:
  128.                 System.out.println("SOLVED.");
  129.                 System.out.println("\t" + Arrays.toString(solver.getSolution()));
  130.                 break;
  131.             case 0:
  132.                 System.out.println("TIME OUT.");
  133.                 break;
  134.             case -1:
  135.                 System.out.println("UNSATISFIABLE.");
  136.                 break;
  137.         }
  138.         System.out.println("\tConsistency checks: " + solver.getConsistencyChecks());
  139.         System.out.println("\tElapsed time: " + solver.getElapsedTime());
  140.     }
  141.  
  142.     /**
  143.      * Solves a CSP instance by using a specific set of constructive heuristics.
  144.      * <p>
  145.      * @param set The sets of problems to solve.
  146.      * @param heuristics A set of constructive heuristics to apply on each instance.
  147.      * @param maxTime The maximum time (in milliseconds) the solver is allowed to run on each
  148.      * instance in the set.
  149.      */
  150.     public static void solve(CSPSet set, ConstructiveHeuristic[] heuristics, long maxTime) {
  151.         StringBuilder string;
  152.         ConstructiveSolver solver;
  153.         /*
  154.          * Prints the header.
  155.          */
  156.         string = new StringBuilder();
  157.         //string.append("INSTANCE, ");
  158.         string.append("INSTANCE\t ");
  159.         for (ConstructiveHeuristic heuristic : heuristics) {
  160.             //string.append(heuristic.toString()).append(", ");
  161.             string.append(heuristic.toString()).append("\t ");
  162.         }
  163.         string.delete(string.length() - 2, string.length());
  164.         System.out.println(string.toString());
  165.         for (CSP csp : set.getInstances()) {
  166.             string = new StringBuilder();
  167.             //string.append(csp.getId()).append(", ");
  168.             string.append(csp.getId()).append("\t ");
  169.             for (ConstructiveHeuristic heuristic : heuristics) {
  170.                 solver = new ConstructiveSolver(csp);
  171.                 solver.solve(heuristic, false, maxTime);
  172.                 //string.append(solver.getElapsedTime()).append(", ");
  173.                 string.append(solver.getElapsedTime()).append("\t ");
  174.             }
  175.             string.delete(string.length() - 2, string.length());
  176.             System.out.println(string.toString());
  177.         }
  178.     }
  179.    
  180.     /**
  181.      * Solves a CSP instance by using a specific set of constructive heuristics.
  182.      * <p>
  183.      * @param set The sets of CSP instances to solve.
  184.      * @param selector A heuristic selector to apply on each instance.
  185.      * @param maxTime The maximum time (in milliseconds) the solver is allowed to run on each
  186.      * instance in the set.
  187.      */
  188.     public static void solve(CSPSet set, HeuristicSelector selector, long maxTime) {
  189.         StringBuilder string;
  190.         ConstructiveSolver solver;
  191.         /*
  192.          * Prints the header.
  193.          */
  194.         System.out.println("INSTANCE, SELECTOR");
  195.         for (CSP csp : set.getInstances()) {
  196.             string = new StringBuilder();
  197.             string.append(csp.getId()).append(", ");            
  198.             solver = new ConstructiveSolver(csp);
  199.             solver.solve(selector, false, maxTime);
  200.             string.append(solver.getElapsedTime());            
  201.             System.out.println(string.toString().trim());
  202.         }
  203.     }
  204.  
  205.     public static void generateHeuristicSelector(String folderName, double splitRate, long seed) {
  206.         Random random;
  207.         String[] featureNames, heuristicNames;
  208.         SelectionOperator selectionOperator;
  209.         EvaluationFunction evaluationFunction;
  210.         HeuristicSelector heuristicSelector;
  211.         random = new Random(seed);        
  212.         //featureNames = new String[]{"MEAN_P1", "MEAN_P2", "MEAN_C", "MEAN_C", "K"};
  213.         featureNames = new String[]{"MEAN_P1", "MEAN_P2"};
  214.         heuristicNames = new String[]{"DOM/MINC", "DEG/MINC", "K/MINC", "WDEG/MINC"};
  215.         evaluationFunction = new CSPEvaluationFunction(CSPEvaluationFunction.Type.RuleBasedHeuristicSelector, folderName, splitRate, seed, 2000);
  216.         selectionOperator = new TournamentSelectionOperator(2, random.nextLong());        
  217.         heuristicSelector = ((RuleBasedHeuristicSelectorIndividual) RuleBasedHeuristicSelectorFramework.run(featureNames, heuristicNames, 5, 5, 1.0, 0.1, selectionOperator, evaluationFunction, STEADY_STATE, true, random.nextLong())).getHeuristicSelector();
  218.         ((RuleBasedHeuristicSelector) heuristicSelector).save("test.xml");
  219.     }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement