Project-Platypus / Platypus

A Free and Open Source Python Library for Multiobjective Optimization
GNU General Public License v3.0
569 stars 153 forks source link

Can't fix the value of a variable #82

Closed PastelBelem8 closed 1 year ago

PastelBelem8 commented 5 years ago

It is not possible to fix the value of a variable.

Integer(0,0)

It would be useful in the context of multiple iterations in a optimization process where I keep narrowing the solution space, focusing on the best solutions.

dhadka commented 5 years ago

One option is to define a custom type for constants:


class Constant(Type):
    def __init__(self, value):
        super(Constant, self).__init__()
        self.value = value

    def rand(self):
        return self.value

    def __str__(self):
        return "Constant(%s)" % (self.value)

The only issue with this is you must explicitly tell Platypus what variation operators to use. It does not have default operators for mixed types.


from platypus import *

def example(x):
    return [x[0], x[1]]

problem = Problem(2, 2)
problem.types[0] = Real(-10, 10)
problem.types[1] = Constant(5)
problem.function = example

algorithm = NSGAII(problem, variator=CompoundOperator(SBX(), PM()))
algorithm.run(10000)
print(algorithm.result)

The mutation and crossover operators will only be applied to the Real variable(s) and will skip the Constant variable.

github-actions[bot] commented 1 year ago

This issue is stale and will be closed soon. If you feel this issue is still relevant, please comment to keep it active. Please also consider working on a fix and submitting a PR.