Platypus
Platypus copied to clipboard
Printing results when a value is an Integer results in a binary string being printed
I use a mix of Integer and Real in the problem types list. (C.f. issue #31.) At the end of a run, when it's time to print the variables from the solution object, I simply issue a Python print. This works fine for Real, but when I use Integer the result is a list of Boolean values that appear to be the binary equivalent of the actual number. Example output:
scores = [471, 1668] low_freq_cutoff = [False, True, False, False, True, True, True, True, False], length_cutoff = 2 min_short_freq = [False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False, False, True, False, True, False, True, True, False, False, True, False, True, True, False, False, False] norm_exp = 0.09633 dict_exp = 0.07681 camel_bias = 0.88487 split_bias = 0.00008352
Best guess: it appears that maybe the definition of Integer in types.py needs a __repr__(), which I started trying to do, but then I couldn't figure out where the object actually stores the current value. I could only find min_value and max_value.
Perhaps someone else can figure out how to fix this?
Hello @mhucka, you can use decode(value) and encode(value) to decode Binary representation [True, False, ....] to Integer; both are included in the class Type of platypus
This is a small example:
first_variable = algorithm.result[0].variables[0]
problem.types[0].decode(first_variable)
If your integer type is: Integer(-1,1), it will be encoded to 2 bits [X,Y], for instance -1, will be encoded : [False, False] , 0 [False True], 1 [True, False]. This is the explanation.
Thanks, that helped! I got it to work now.
Is this approach (using decode) the way it must be done? IMHO it would be more convenient and easier to use if the objects in the result field had Python __repr__ or __str__ functions that did the right thing for printing Integer values. Perhaps this is something the developers could investigate for a future release?
I agree with mhucka. Also because it is not consistent with the representation that we are given when addressing Continuous problems.
@manuparra @mhucka
(all thing is in jupyter) Thank you for suggestion but when I have used
first_variable = algorithm.result[0].variables[0]
Problem.types[0].decode(first_variable)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-145-282c3f73daa6> in <module>
1 first_variable = algorithm.result[0].variables[0]
----> 2 Problem.types[0].decode(first_variable)
AttributeError: type object 'Problem' has no attribute 'types'
And I think it is because of using :
class structureoptimiser(Problem):
def __init__(self):
super(structureoptimiser, self).__init__(…...
Then I have changed it to :
first_variable = algorithm.result[0].variables[0]
structureoptimiser.types[0].decode(first_variable)
but I have gotten:
----------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-138-4dc9e9933591> in <module>
1 first_variable = algorithm.result[0].variables[0]
----> 2 structureoptimiser.types[0].decode(first_variable)
AttributeError: type object 'structureoptimiser' has no attribute 'types'
note :
structureoptimiser.__mro__
(__main__.structureoptimiser, platypus.core.Problem, object)
structureoptimiser.types[0].decode(first_variable) doesn't work because structureoptimizer is the class itself and not an instance of the class.
Try algorithm.problem.types[0].decode(first_variable).
structureoptimiser.types[0].decode(first_variable) doesn't work because structureoptimizer is the class itself and not an instance of the class. Try algorithm.problem.types[0].decode(first_variable).
Thank you very much, you are right. it's worked.(Really thank you for time you spent and time you saved for me)
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.