Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import CSP.CSP;
- import CSP.Solvers.Constructive.ConstructiveSolver;
- import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic;
- import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic.ValueOrderingHeuristic;
- import CSP.Solvers.Constructive.Heuristics.ConstructiveHeuristic.VariableOrderingHeuristic;
- import CSP.CSPSet;
- import CSP.CSPSet.Subset;
- import CSP.Solvers.HyperHeuristic.CSPEvaluationFunction;
- import GA.EvaluationFunction;
- import static GA.GeneticAlgorithm.Type.STEADY_STATE;
- import GA.SelectionOperator;
- import GA.TournamentSelectionOperator;
- import HERMES.Exceptions.NoSuchFeatureException;
- import HERMES.Selector.GA.RuleBasedHeuristicSelector;
- import HERMES.Selector.GA.RuleBasedHeuristicSelectorFramework;
- import HERMES.Selector.GA.RuleBasedHeuristicSelectorIndividual;
- import HERMES.Selector.HeuristicSelector;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import java.util.Random;
- public class Run {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- String set;
- ConstructiveHeuristic[] heuristics;
- RuleBasedHeuristicSelector selector;
- long seed;
- //seed = (new Random()).nextLong();
- seed = 1101813;
- //set = "../Instances/MyTest";
- //set = "../Instances/PATT/bqwh-15-106";
- //set = "../Instances/PATT/bqwh-18-141";
- //set = "../Instances/PATT/coloring";
- //set = "../Instances/QRND/composed-25-1-2";
- set = "../Instances/QRND/ehi-85";
- //set = "../Instances/QRND/geom";
- //set = "../Instances/RAND/rand-2-30-15";
- //solve(new CSP(set + "/qcp-15-120-3_ext.xml"), new ConstructiveHeuristic(VariableOrderingHeuristic.WDEG, ValueOrderingHeuristic.MINC), 25000);
- //System.exit(1);
- heuristics = new ConstructiveHeuristic[]{
- new ConstructiveHeuristic(VariableOrderingHeuristic.DOM, ValueOrderingHeuristic.MINC),
- new ConstructiveHeuristic(VariableOrderingHeuristic.DEG, ValueOrderingHeuristic.MINC),
- new ConstructiveHeuristic(VariableOrderingHeuristic.KAPPA, ValueOrderingHeuristic.MINC),
- new ConstructiveHeuristic(VariableOrderingHeuristic.WDEG, ValueOrderingHeuristic.MINC),
- new ConstructiveHeuristic(VariableOrderingHeuristic.DOMDEG, ValueOrderingHeuristic.MINC)
- };
- //generateHeuristicSelector(set, 1.0, seed);
- solve(new CSPSet(set, Subset.TEST, 0.5, seed), heuristics, 20000);
- //extractFeatures(new CSPSet(set, Subset.TEST, 0.5, seed), new String[]{"MEAN_P1", "MEAN_P2", "MEAN_C", "K"});
- System.exit(1);
- //solve(new CSPSet(set, Subset.TRAIN, 0.05, 12345), heuristics, 2000);
- selector = new RuleBasedHeuristicSelector("test.xml");
- solve(new CSPSet(set), selector, 2000);
- //solve(new CSPSet(set, Subset.TRAIN, 0.05, seed), selector, 2000);
- System.out.println("Heuristic use: " + Arrays.toString(selector.getHeuristicUse()));
- System.out.println("Rule use: " + Arrays.toString(selector.getRuleUse()));
- }
- /**
- * Extracts the features of a set of CSP instances.
- * <p>
- * @param set The sets of CSP instances to solve.
- * @param features The features to extract from every instance in the set.
- */
- public static void extractFeatures(CSPSet set, String[] features) {
- StringBuilder string;
- ConstructiveSolver solver;
- DecimalFormat format;
- format = new DecimalFormat("0.0000");
- /*
- * Prints the header.
- */
- string = new StringBuilder();
- //string.append("INSTANCE, ");
- string.append("INSTANCE\t ");
- for (String feature : features) {
- //string.append(feature).append(", ");
- string.append(feature).append("\t ");
- }
- string.delete(string.length() - 2, string.length());
- System.out.println(string.toString());
- for (CSP csp : set.getInstances()) {
- string = new StringBuilder();
- //string.append(csp.getId()).append(", ");
- string.append(csp.getId()).append("\t ");
- for (String feature : features) {
- solver = new ConstructiveSolver(csp);
- try {
- //string.append(format.format(solver.getFeature(feature))).append(", ");
- string.append(format.format(solver.getFeature(feature))).append("\t ");
- } catch (NoSuchFeatureException e) {
- System.out.println("Feature \'" + feature + "\' is not recognized by the problem domain.");
- System.out.println("The system will halt.");
- System.exit(1);
- }
- }
- string.delete(string.length() - 2, string.length());
- System.out.println(string.toString());
- }
- }
- /**
- * Solves a CSP instance by using a specific constructive heuristic.
- * <p>
- * @param csp The CSP instance to solve.
- * @param heuristic The constructive heuristic to use.
- * @param maxTime The maximum time (in milliseconds) the solver is allowed to run.
- */
- public static void solve(CSP csp, ConstructiveHeuristic heuristic, long maxTime) {
- int result;
- ConstructiveSolver solver;
- solver = new ConstructiveSolver(csp);
- result = solver.solve(heuristic, false, maxTime);
- System.out.println("Solutions found: " + solver.getNumberOfSolutions());
- switch (result) {
- case 1:
- System.out.println("SOLVED.");
- System.out.println("\t" + Arrays.toString(solver.getSolution()));
- break;
- case 0:
- System.out.println("TIME OUT.");
- break;
- case -1:
- System.out.println("UNSATISFIABLE.");
- break;
- }
- System.out.println("\tConsistency checks: " + solver.getConsistencyChecks());
- System.out.println("\tElapsed time: " + solver.getElapsedTime());
- }
- /**
- * Solves a CSP instance by using a specific set of constructive heuristics.
- * <p>
- * @param set The sets of problems to solve.
- * @param heuristics A set of constructive heuristics to apply on each instance.
- * @param maxTime The maximum time (in milliseconds) the solver is allowed to run on each
- * instance in the set.
- */
- public static void solve(CSPSet set, ConstructiveHeuristic[] heuristics, long maxTime) {
- StringBuilder string;
- ConstructiveSolver solver;
- /*
- * Prints the header.
- */
- string = new StringBuilder();
- //string.append("INSTANCE, ");
- string.append("INSTANCE\t ");
- for (ConstructiveHeuristic heuristic : heuristics) {
- //string.append(heuristic.toString()).append(", ");
- string.append(heuristic.toString()).append("\t ");
- }
- string.delete(string.length() - 2, string.length());
- System.out.println(string.toString());
- for (CSP csp : set.getInstances()) {
- string = new StringBuilder();
- //string.append(csp.getId()).append(", ");
- string.append(csp.getId()).append("\t ");
- for (ConstructiveHeuristic heuristic : heuristics) {
- solver = new ConstructiveSolver(csp);
- solver.solve(heuristic, false, maxTime);
- //string.append(solver.getElapsedTime()).append(", ");
- string.append(solver.getElapsedTime()).append("\t ");
- }
- string.delete(string.length() - 2, string.length());
- System.out.println(string.toString());
- }
- }
- /**
- * Solves a CSP instance by using a specific set of constructive heuristics.
- * <p>
- * @param set The sets of CSP instances to solve.
- * @param selector A heuristic selector to apply on each instance.
- * @param maxTime The maximum time (in milliseconds) the solver is allowed to run on each
- * instance in the set.
- */
- public static void solve(CSPSet set, HeuristicSelector selector, long maxTime) {
- StringBuilder string;
- ConstructiveSolver solver;
- /*
- * Prints the header.
- */
- System.out.println("INSTANCE, SELECTOR");
- for (CSP csp : set.getInstances()) {
- string = new StringBuilder();
- string.append(csp.getId()).append(", ");
- solver = new ConstructiveSolver(csp);
- solver.solve(selector, false, maxTime);
- string.append(solver.getElapsedTime());
- System.out.println(string.toString().trim());
- }
- }
- public static void generateHeuristicSelector(String folderName, double splitRate, long seed) {
- Random random;
- String[] featureNames, heuristicNames;
- SelectionOperator selectionOperator;
- EvaluationFunction evaluationFunction;
- HeuristicSelector heuristicSelector;
- random = new Random(seed);
- //featureNames = new String[]{"MEAN_P1", "MEAN_P2", "MEAN_C", "MEAN_C", "K"};
- featureNames = new String[]{"MEAN_P1", "MEAN_P2"};
- heuristicNames = new String[]{"DOM/MINC", "DEG/MINC", "K/MINC", "WDEG/MINC"};
- evaluationFunction = new CSPEvaluationFunction(CSPEvaluationFunction.Type.RuleBasedHeuristicSelector, folderName, splitRate, seed, 2000);
- selectionOperator = new TournamentSelectionOperator(2, random.nextLong());
- heuristicSelector = ((RuleBasedHeuristicSelectorIndividual) RuleBasedHeuristicSelectorFramework.run(featureNames, heuristicNames, 5, 5, 1.0, 0.1, selectionOperator, evaluationFunction, STEADY_STATE, true, random.nextLong())).getHeuristicSelector();
- ((RuleBasedHeuristicSelector) heuristicSelector).save("test.xml");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement