示例#1
0
 def __init__(self, problem, method_class):
     super().__init__(problem, method_class)
     self._factories = []
     self._factories.append(
         IterationPointFactory(
             self.method_class(NIMBUSProblem(self.problem))))
     self._factories.append(
         IterationPointFactory(
             self.method_class(AchievementProblem(self.problem))))
     self._classification = None
     self._problem = problem
示例#2
0
    def __init__(self, method, method_class):
        super().__init__(method, method_class)
        self.user_iters = 5
        self.current_iter = self.user_iters
        self.fh_factory = IterationPointFactory(self.method_class(AchievementProblem(self.problem)))
        self.bounds_factory = BoundsFactory(self.method_class(EpsilonConstraintProblem(self.problem)))
        self.preference = None

        self.fh_lo, self.zh = tuple(self.problem.objective_bounds())
        self.fh = list(self.fh_lo)
        self.zh_prev = list(self.zh)

        self.NsPoints = None
示例#3
0
class NAUTILUS(InteractiveMethod):

    def __init__(self, method, method_class):
        super().__init__(method, method_class)
        self.user_iters = 5
        self.current_iter = self.user_iters
        self.fh_factory = IterationPointFactory(self.method_class(AchievementProblem(self.problem)))
        self.bounds_factory = BoundsFactory(self.method_class(EpsilonConstraintProblem(self.problem)))
        self.preference = None

        self.fh_lo, self.zh = tuple(self.problem.objective_bounds())
        self.fh = list(self.fh_lo)
        self.zh_prev = list(self.zh)

        self.NsPoints = None

    def _initIteration(self, *args, **kwargs):
        pass

    def _nextIteration(self, *args, **kwargs):
        pass

    def _update_fh(self):
        self.fh = list(self.fh_factory.result(self.preference, self.zh_prev))

    def _next_zh(self, term1, term2):
        res = list((self.current_iter - 1) * np.array(term1) / self.current_iter + np.array(term2) / self.current_iter)
        logging.debug("Update zh")
        logging.debug("First term:  %s", term1)
        logging.debug("Second term: %s", term2)
        for i in range(3):
            logging.debug("%i/%i * %f + %f/%i  =%f",
                                                (self.current_iter - 1),
                                                self.   current_iter,
                                                term1[i],
                                                term2[i],
                                                self.current_iter,
                                                res[i])
        return res

    def _update_zh(self, term1, term2):
        self.zh_prev = list(self.zh)

        self.zh = list(self._next_zh(term1, term2))

    def distance(self, where = None, target = None):

        a_nadir = np.array(self.problem.nadir)
        a_ideal = np.array(self.problem.ideal)
        w = a_ideal - a_nadir
        u = np.linalg.norm((np.array(where) - a_nadir) / w)
        l = np.linalg.norm((np.array(target) - a_nadir) / w)
        logging.debug("\nNADIR: %s", self.problem.nadir)
        logging.debug("zh: %s", where)
        logging.debug("PO: %s", target)
        if not l:
            logging.debug("Distance: %s", 0.0)
            return 0.0
        logging.debug("Distance: %s", (u / l) * 100)
        return (u / l) * 100
示例#4
0
    def __init__(self, method, method_class: Type[OptimizationMethod]) -> None:
        super().__init__(method, method_class)
        self.user_iters = 5
        self.current_iter = self.user_iters

        self.fh_factory = IterationPointFactory(
            self.method_class(NautilusAchievementProblem(self.problem)))
        self.upper_bounds_factory = BoundsFactory(
            self.method_class(MaxEpsilonConstraintProblem(self.problem)))
        self.lower_bounds_factory = BoundsFactory(
            self.method_class(EpsilonConstraintProblem(self.problem)))

        self.preference = None

        self.fh_lo, self.zh = tuple(self.problem.objective_bounds())
        self.fh = list(self.fh_lo)
        self.zh_prev = list(self.zh)
示例#5
0
 def __init__(self, problem, method_class):
     super().__init__(problem, method_class)
     self._factories = [
         IterationPointFactory(self.method_class(prob_cls(self.problem)))
         for prob_cls in [
             NIMBUSProblem,
             NIMBUSAchievementProblem,
             NIMBUSGuessProblem,
             NIMBUSStomProblem,
         ]
     ]
     self._classification = None
     self._problem = problem
     self.selected_solution = None
示例#6
0
def new_points(
        factory: IterationPointFactory,
        solution,
        weights: List[List[float]] = None
) -> List[Tuple[np.ndarray, List[float]]]:
    """Generate approximate set of points

    Generate set of Pareto optimal solutions projecting from the Pareto optimal solution
    using weights to determine the direction.


    Parameters
    ----------

    factory:
        IterationPointFactory with suitable optimization problem

    solution:
        Current solution from which new solutions are projected

    weights:
        Direction of the projection, if not given generate with
        :func:random_weights

    """
    from desdeo.preference.direct import DirectSpecification

    points = []
    nof = factory.optimization_method.optimization_problem.problem.nof_objectives(
    )
    if not weights:
        weights = random_weights(nof, 50 * nof)
    for pref in map(
            lambda w: DirectSpecification(factory.optimization_method,
                                          np.array(w)), weights):
        points.append(factory.result(pref, solution))
    return points
示例#7
0
def main(logfile=False):
    """ Solve River Pollution problem with NAUTILUS V1 and E-NAUTILUS Methods
    """

    # Duplicate output to log file

    class NAUTILUSOptionValidator(Validator):
        def validate(self, document):
            if document.text not in "ao":
                raise ValidationError(
                    message=
                    "Please select a for apriori or o for optimization option",
                    cursor_position=0,
                )

    if logfile:
        Tee(logfile)
    first = True
    current_iter = 0
    while first or current_iter:
        # SciPy breaks box constraints

        nautilus_v1 = NAUTILUSv1(RiverPollution(), SciPyDE)
        if not first:
            nautilus_v1.current_iter = current_iter
        first = False
        nadir = nautilus_v1.problem.nadir
        ideal = nautilus_v1.problem.ideal

        solution = tui.iter_nautilus(nautilus_v1)

        current_iter = nautilus_v1.current_iter

        # TODO: Move to tui module
        method_e = None
        if current_iter > 0:
            option = _prompt_wrapper(
                "select a for apriori or o for optimization option: ",
                default="o",
                validator=NAUTILUSOptionValidator(),
            )
            if option.lower() == "a":
                wi = _prompt_wrapper(
                    "Number of PO solutions (10 or 20): ",
                    default="20",
                    validator=tui.NumberValidator(),
                )
                weights = WEIGHTS[wi]

                factory = IterationPointFactory(
                    SciPyDE(NautilusAchievementProblem(RiverPollution())))
                points = misc.new_points(factory, solution, weights=weights)

                method_e = ENAUTILUS(PreGeneratedProblem(points=points),
                                     PointSearch)
                method_e.zh_prev = solution

            else:
                method_e = ENAUTILUS(RiverPollution(), SciPyDE)

            # method_e.zh = solution
            method_e.current_iter = nautilus_v1.current_iter
            method_e.user_iters = nautilus_v1.user_iters

            print("E-NAUTILUS\nselected iteration point: %s:" %
                  ",".join(map(str, solution)))

            while method_e and method_e.current_iter > 0:
                if solution is None:
                    solution = method_e.problem.nadir

                method_e.problem.nadir = nadir
                method_e.problem.ideal = ideal
                cmd = tui.iter_enautilus(method_e,
                                         initial_iterpoint=solution,
                                         initial_bound=method_e.fh_lo)
                if cmd:
                    print(method_e.current_iter)
                    current_iter = method_e.current_iter
                    break
    if tui.HAS_INPUT:
        input("Press ENTER to exit")
示例#8
0
    nadir = method.problem.nadir
    ideal = method.problem.ideal

    solution = tui.iter_nautilus(method)

    ci = method.current_iter
    if ci > 0:
        try:
            from prompt_toolkit import prompt
            weights = prompt(u'Weights (10 or 20): ',
                             default=u"20",
                             validator=tui.NumberValidator())
        except:
            weights = "20"

        factory = IterationPointFactory(
            SciPyDE(AchievementProblem(NaurulaWeistroffer())))
        points = misc.new_points(factory, solution, WEIGHTS[weights])

        method = ENAUTILUS(PreGeneratedProblem(points=points), PointSearch)
        method.current_iter = ci
        method.zh_prev = solution
        print("E-NAUTILUS\nselected iteration point: %s:" %
              ",".join(map(str, method.zh_prev)))

    while method.current_iter > 0:
        if solution is None:
            solution = method.problem.nadir
            # Generate new points
        # print solution

        method.problem.nadir = nadir