예제 #1
0
    def solve(self,
              n_threads: int,
              n_individuals: int,
              topol=topology.unconnected()) -> SortedList:
        solutions = SortedListWithKey(key=Solution.get_cost)

        try:
            iteration = 1
            solution = self._generate_solution(self._start_problem,
                                               n_individuals, n_threads, topol)
            solutions.add(solution)
            self._logger.info("({}) - New solution found.".format(iteration))

            problem, do_iteration = self._iterate(self._probl_factory,
                                                  solution)
            while do_iteration:
                solution = self._generate_solution(problem, n_individuals,
                                                   n_threads, topol)
                solutions.add(solution)
                iteration += 1
                self._logger.info(
                    "({}) - New solution found.".format(iteration))
                problem, do_iteration = self._iterate(self._probl_factory,
                                                      solution)
            self._logger.info("Differential evolution completed.")
        # FIXME - is horrible to have to catch all possible exceptions but
        # requires a bit of time to understand all the possible exceptions
        # that can be thrown.
        except:
            self._logger.exception("Exception occurred during solution...")
            self._logger.error("Returning solutions found so far")
        return solutions
예제 #2
0
def _generic_archi_ctor(self, *args, **kwargs):
    """
	Unnamed arguments (optional):

		#. algorithm
		#. problem
		#. number of islands
		#. number individual in the population

	Keyword arguments:

		* *topology* -- migration topology (defaults to unconnected)
		* *distribution_type* -- distribution_type (defaults to distribution_type.point_to_point)
		* *migration_direction* -- migration_direction (defaults to migration_direction.destination)
	"""

    from PyGMO import topology, algorithm, problem
    if not ((len(args) == 4) or (len(args) == 0)):
        raise ValueError(
            "Unnamed arguments list, when present, must be of length 4, but %d elements were found instead"
            % (len(args), ))

    #Append everything in the same list of constructor arguments
    ctor_args = []
    for i in args:
        ctor_args.append(i)
    ctor_args.append(kwargs.pop(
        'topology', topology.unconnected()))  #unconnected is default
    ctor_args.append(
        kwargs.pop(
            'distribution_type',
            distribution_type.point_to_point))  #point-to-point is default
    ctor_args.append(
        kwargs.pop('migration_direction',
                   migration_direction.destination))  #destination is default

    #Constructs an empty archipelago with no islands using the C++ constructor
    self.__original_init__(*ctor_args[-3:])

    #We now push back the correct island type if required
    if (len(args)) == 4:
        if not isinstance(args[0], algorithm._base):
            raise TypeError("The first unnamed argument must be an algorithm")
        if not isinstance(args[1], problem._base):
            raise TypeError("The second unnamed argument must be a problem")
        if not isinstance(args[2], int):
            raise TypeError(
                "The third unnamed argument must be an integer (i.e. number of islands)"
            )
        if not isinstance(args[3], int):
            raise TypeError(
                "The fourth unnamed argument must be an integer (i.e. population size)"
            )
        for n in range(args[2]):
            self.push_back(island(args[0], args[1], args[3]))
예제 #3
0
def _generic_archi_ctor(self,*args,**kwargs):
	"""
	Unnamed arguments (optional):

		#. algorithm
		#. problem
		#. number of islands
		#. number individual in the population

	Keyword arguments:

		* *topology* -- migration topology (defaults to unconnected)
		* *distribution_type* -- distribution_type (defaults to distribution_type.point_to_point)
		* *migration_direction* -- migration_direction (defaults to migration_direction.destination)
	"""

	from PyGMO import topology, algorithm,problem
	if not((len(args)==4) or (len(args)==0)):
		raise ValueError("Unnamed arguments list, when present, must be of length 4, but %d elements were found instead" % (len(args),))

	#Append everything in the same list of constructor arguments
	ctor_args = []
	for i in args:
		ctor_args.append(i)
	ctor_args.append(kwargs.pop('topology', topology.unconnected())) #unconnected is default
	ctor_args.append(kwargs.pop('distribution_type', distribution_type.point_to_point)) #point-to-point is default
	ctor_args.append(kwargs.pop('migration_direction', migration_direction.destination)) #destination is default

	#Constructs an empty archipelago with no islands using the C++ constructor
	self.__original_init__(*ctor_args[-3:])

	#We now push back the correct island type if required
	if (len(args))==4:
		if not isinstance(args[0],algorithm._base):
			raise TypeError("The first unnamed argument must be an algorithm")
		if not isinstance(args[1],problem._base):
			raise TypeError("The second unnamed argument must be a problem")
		if not isinstance(args[2],int):
			raise TypeError("The third unnamed argument must be an integer (i.e. number of islands)")
		if not isinstance(args[3],int):
			raise TypeError("The fourth unnamed argument must be an integer (i.e. population size)")
		for n in range(args[2]):
			self.push_back(island(args[0],args[1],args[3]))
예제 #4
0
파일: __init__.py 프로젝트: swilli/pagmo
def _generic_archi_ctor(self, *args, **kwargs):
    """
    Unnamed arguments (optional):

            #. algorithm
            #. problem
            #. number of islands
            #. number individual in the population

    Keyword arguments:

            * *topology* -- migration topology (defaults to unconnected)
            * *distribution_type* -- distribution_type (defaults to distribution_type.point_to_point)
            * *migration_direction* -- migration_direction (defaults to migration_direction.destination)
    """

    from PyGMO import topology, algorithm, problem
    from difflib import get_close_matches

    if not((len(args) == 4) or (len(args) == 0)):
        raise ValueError(
            "Unnamed arguments list, when present, must be of length 4, but %d elements were found instead" %
            (len(args),))

    # Append everything in the same list of constructor arguments
    ctor_args = []
    for i in args:
        ctor_args.append(i)

    # Pop all known keywords out of kwargs and add a default value if not
    # provided
    # unconnected is default
    ctor_args.append(kwargs.pop('topology', topology.unconnected()))
    # point-to-point is default
    ctor_args.append(
        kwargs.pop('distribution_type', distribution_type.point_to_point))
    # destination is default
    ctor_args.append(
        kwargs.pop('migration_direction', migration_direction.destination))

    # Check for unknown keywords
    kwlist = ['topology', 'distribution_type', 'migration_direction']
    if kwargs:
        s = "The following unknown keyworded argument was passed to the construtor: "
        for kw in kwargs:
            s += kw
            spam = get_close_matches(kw, kwlist)
            if spam:
                s += " (Did you mean %s?), " % spam[0]
            else:
                s += ", "

        raise ValueError(s[:-2])

    # Constructs an empty archipelago with no islands using the C++ constructor
    self.__original_init__(*ctor_args[-3:])

    # We now push back the correct island type if required
    if (len(args)) == 4:
        if not isinstance(args[0], algorithm._base):
            raise TypeError("The first unnamed argument must be an algorithm")
        if not (isinstance(args[1], problem._base) or isinstance(args[1], problem._base_stochastic)):
            raise TypeError("The second unnamed argument must be a problem")
        if not isinstance(args[2], int):
            raise TypeError(
                "The third unnamed argument must be an integer (i.e. number of islands)")
        if not isinstance(args[3], int):
            raise TypeError(
                "The fourth unnamed argument must be an integer (i.e. population size)")
        for n in range(args[2]):
            self.push_back(island(args[0], args[1], args[3]))