Configuring Planners by Programming

There are two ways to run a existing planner by programming: by direct manipulating or using the class PlannerConfiguration.

Pre-requisite Installations

For this tutorial you need:

In the following, we will give the commands line so that the tutorial can be done independently of any IDE.

Step 1. Create a simple Java project with PDDL4J

First, open a terminal and create your development directory PlannerConfigurationExamples

mkdir PlannerConfigurationExamples

Then, create the sub-directories of your project

cd PlannerConfigurationExamples
mkdir -p src/fr/uga/pddl4j/examples/
mkdir classes
mkdir lib

Finally, get the last binary of PDDL4J and save it in the lib directory

wget http://pddl4j.imag.fr/repository/pddl4j/binaries/pddl4j-4.0.0.jar
mv pddl4j-4.0.0.jar lib/pddl4j-4.0.0.jar

You are now ready to configure an existing planner of the library by programming.

Step 2. By directly manipulating

Create and edit a file called DirectPlannerConfigurationExample.java in the directory src/fr/uga/pddl4j/examples/. The skeleton of this class is given bellow:

package fr.uga.pddl4j.examples;

import fr.uga.pddl4j.heuristics.state.StateHeuristic;
import fr.uga.pddl4j.planners.InvalidConfigurationException;
import fr.uga.pddl4j.planners.LogLevel;
import fr.uga.pddl4j.planners.statespace.HSP;

import java.io.FileNotFoundException;

/**
 * The class is an example. It shows how to create a planner by programming and running it.
 *
 * @author D. Pellier
 * @version 4.0 - 30.11.2021
 */
public class DirectPlannerConfigurationExample {

    /**
     * The main method of the class.
     *
     * @param args the command line arguments. No argument is used.
     */
    public static void main(String[] args) {

        // The path to the benchmarks directory
        final String benchmarks = "src/test/resources/benchmarks/pddl/ipc2002/depots/strips-automatic/";

        // Creates the planner
        HSP planner = new HSP();
        // Sets the domain of the problem to solve
        planner.setDomain(benchmarks + "domain.pddl");
        // Sets the problem to solve
        planner.setProblem(benchmarks + "p01.pddl");
        // Sets the timeout of the search in seconds
        planner.setTimeout(1000);
        // Sets log level
        planner.setLogLevel(LogLevel.INFO);
        // Selects the heuristic to use
        planner.setHeuristic(StateHeuristic.Name.MAX);
        // Sets the weight of the heuristic
        planner.setHeuristicWeight(1.2);

        // Solve and print the result
        try {
            planner.solve();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }

    }
}

The code above configures and run the planner called HSP on the first problem of the depot domain from IPC 2002.

To test the above code use the following command line to compile the example:

javac -d classes -cp classes:lib/pddl4j-4.0.0.jar src/fr/uga/pddl4j/examples/DirectPlannerConfigurationExample.java

and then the following command line to run the example:

java -cp classes:lib/pddl4j-4.0.0.jar fr.uga.pddl4j.examples.DirectPlannerConfigurationExample

Step 3. Using the class planner configuration

Create and edit a file called PlannerConfigurationExample.java in the directory src/fr/uga/pddl4j/examples/. The skeleton of this class is given bellow:

package fr.uga.pddl4j.examples;

import fr.uga.pddl4j.heuristics.state.StateHeuristic;
import fr.uga.pddl4j.planners.InvalidConfigurationException;
import fr.uga.pddl4j.planners.LogLevel;
import fr.uga.pddl4j.planners.Planner;
import fr.uga.pddl4j.planners.statespace.HSP;

import java.io.FileNotFoundException;

/**
 * The class is an example. It shows how to create a HSP planner by programming and running it using the class
 * {@code PlannerConfiguration}.
 *
 * @author D. Pellier
 * @version 4.0 - 30.11.2021
 */
public class PlannerConfigurationExample {

    /**
     * The main method of the class.
     *
     * @param args the command line arguments. No argument is used.
     */
    public static void main(String[] args) {

        // The path to the benchmarks directory
        final String benchmarks = "src/test/resources/benchmarks/pddl/ipc2002/depots/strips-automatic/";

        // Gets the default configuration from the planner
        fr.uga.pddl4j.planners.PlannerConfiguration config = HSP.getDefaultConfiguration();
        // Sets the domain of the problem to solve
        config.setProperty(HSP.DOMAIN_SETTING, benchmarks + "domain.pddl");
        // Sets the problem to solve
        config.setProperty(HSP.PROBLEM_SETTING, benchmarks + "p01.pddl");
        // Sets the timeout allocated to the search.
        config.setProperty(HSP.TIME_OUT_SETTING, 1000);
        // Sets the log level
        config.setProperty(HSP.LOG_LEVEL_SETTING, LogLevel.INFO);
        // Sets the heuristic used to search
        config.setProperty(HSP.HEURISTIC_SETTING, StateHeuristic.Name.MAX);
        // Sets the weight of the heuristic
        config.setProperty(HSP.WEIGHT_HEURISTIC_SETTING, 1.2);

        // Creates an instance of the HSP planner with the specified configuration
        Planner planner = Planner.getInstance(Planner.Name.HSP, config);

        // Runs the planner and print the solution
        try {
            planner.solve();
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }

    }
}

The above code configures and runs same HSP planner with the same configuration as by direct manipulating.

Note

The advantage of using the PlannerConfiguration object is that you can save or load specific configurations using the store() and load() methods of the class in XML format

To test the above code use the following command line to compile the example:

javac -d classes -cp classes:lib/pddl4j-4.0.0.jar src/fr/uga/pddl4j/examples/PlannerConfigurationExample.java

and then the following command line to run the example:

java -cp classes:lib/pddl4j-4.0.0.jar fr.uga.pddl4j.examples.PlannerConfigurationExample