Platypus icon indicating copy to clipboard operation
Platypus copied to clipboard

Can't fix the value of a variable

Open PastelBelem8 opened this issue 6 years ago • 1 comments

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.

PastelBelem8 avatar Dec 01 '18 17:12 PastelBelem8

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.

dhadka avatar Dec 04 '18 21:12 dhadka

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.

github-actions[bot] avatar Nov 12 '22 12:11 github-actions[bot]