Class ParallelPortfolio


  • public class ParallelPortfolio
    extends Object

    A Portfolio helper.

    The ParallelPortfolio resolution of a problem is made of four steps:

    1. adding models to be run in parallel,
    2. running resolution in parallel,
    3. getting the model which finds a solution (or the best one), if any.
    Each of the four steps is needed and the order is imposed too. In particular, in step 1. each model should be populated individually with a model of the problem (presumably the same model, but not required). Populating model is not managed by this class and should be done before applying step 2., with a dedicated method for instance.
    Note also that there should not be pending resolution process in any models. Otherwise, unexpected behaviors may occur.

    The resolution process is synchronized. As soon as one model ends (naturally or by hitting a limit) the other ones are eagerly stopped. Moreover, when dealing with an optimization problem, cut on the objective variable's value is propagated to all models on solution. It is essential to eagerly declare the objective variable(s) with Model.setObjective(boolean, Variable).

    Note that the similarity of the models declared is not required. However, when dealing with an optimization problem, keep in mind that the cut on the objective variable's value is propagated among all models, so different objectives may lead to wrong results.

    Since there is no condition on the similarity of the models, once the resolution ends, the model which finds the (best) solution is internally stored.

    Example of use.

     ParallelPortfolio pares = new ParallelPortfolio();
     int n = 4; // number of models to use
     for (int i = 0; i < n; i++) {
          pares.addModel(modeller());
     }
     pares.solve();
     IOutputFactory.printSolutions(pares.getBestModel());
     
     

    This class uses Java 8 streaming feature, and may be not compliant with older versions.

    Project: choco.

    Since:
    23/12/2015.
    Author:
    Charles Prud'homme, Jean-Guillaume Fages
    • Constructor Detail

      • ParallelPortfolio

        public ParallelPortfolio​(boolean searchAutoConf)
        Creates a new ParallelPortfolio This class stores the models to be executed in parallel in a ArrayList initially empty.
        Parameters:
        searchAutoConf - changes the search heuristics of the different solvers, except the first one (true by default). Must be set to false if search heuristics of the different threads are specified manually, so that they are not erased
      • ParallelPortfolio

        public ParallelPortfolio()
        Creates a new ParallelPortfolio This class stores the models to be executed in parallel in a ArrayList initially empty. Search heuristics will be changed automatically (except for the first thread that will remain in the same configuration).
    • Method Detail

      • addModel

        public void addModel​(Model model)

        Adds a model to the list of models to run in parallel. The model can either be a fresh one, ready for populating, or a populated one.

        Important:

        • the populating process is not managed by this ParallelPortfolio and should be done externally, with a dedicated method for example.
        • when dealing with optimization problems, the objective variables HAVE to be declared eagerly with Model.setObjective(boolean, Variable).

        Parameters:
        model - a model to add
      • solve

        public boolean solve()
        Run the solve() instruction of every model of the portfolio in parallel.

        Note that a call to getBestModel() returns a model which has found the best solution.

        Returns:
        true if and only if at least one new solution has been found.
        Throws:
        SolverException - if no model or only model has been added.
      • getBestModel

        public Model getBestModel()
        Returns the first model from the list which, either :
        • finds a solution when dealing with a satisfaction problem,
        • or finds (and possibly proves) the best solution when dealing with an optimization problem.
        or null if no such model exists. Note that there can be more than one "finder" in the list, yet, this method returns the index of the first one.
        Returns:
        the first model which finds a solution (or the best one) or null if no such model exists.
      • getModels

        public List<Model> getModels()
        Returns:
        the (mutable!) list of models used in this ParallelPortfolio
      • streamSolutions

        public Stream<Solution> streamSolutions()
        Attempts to find all solutions of the declared problem.
        • If the method returns an empty list:
          • either a stop criterion (e.g., a time limit) stops the search before any solution has been found,
          • or no solution exists for the problem (i.e., over-constrained).
        • if the method returns a list with at least one element in it:
          • either the resolution stops eagerly du to a stop criterion before finding all solutions,
          • or all solutions have been found.

        Note that all variables will be recorded

        Returns:
        a list that contained the found solutions.
      • prepare

        public void prepare()