2

Based on a HyperVolume function found here (direct download link), I am trying to return hypervolume metrics for my algorithm. However, not even the test code works:

from hv import HyperVolume
referencePoint = [2, 2, 2]
hyperVolume = HyperVolume(referencePoint)
front = [[1, 0, 1], [0, 1, 0]]
result = hyperVolume.compute(front)

I'm getting errors which seem to be related to the fact that I'm using Python 3, and the function itself is using Python 2. Is there a way around that? Is there a similar function implemented in Python 3?

I've also looked at this one in deap, but it seems the have the same issue.

LE: I've been asked for specific errors, so I'll keep a log of them as they're being solved

  1. name xrange is not defined - fixed by replacing with range
  2. TypeError: unorderable types: Node() < Node()
Silviu Tofan
  • 379
  • 1
  • 3
  • 17
  • *I'm getting errors* Please [edit] your question and include them. –  Jun 27 '16 at 15:21
  • I can do, but it'll just be error after error, and I'll only be able to post them 1 at a time as they're being solved... – Silviu Tofan Jun 27 '16 at 15:26

2 Answers2

1

Use 2to3 to convert hv.py to hv3.py:

$ 2to3 hv.py > hv3.py.patch
$ cp hv.py hv3.py
$ patch < hv3.py.patch

Then change your import line to

from hv3 import HyperVolume

If you want 2to3 to change the modify the original file, you need to pass the -w option, as shown in the documentation

$ 2to3 -w hv.py
  • I tried applying 2to3 in place (hopefully I did it right), https://gyazo.com/bf0816763e9d62a9bccef088bb3a3b69 However, I'm still getting the same errors when I run my main function. – Silviu Tofan Jun 27 '16 at 15:56
  • @SilviuTofan: If you do not pass the `-w` switch to `2to3` it will not modify the file. See [the documentation](https://docs.python.org/2/library/2to3.html#using-2to3): "Writing the changes back is enabled with the -w flag". –  Jun 27 '16 at 17:03
  • Oh, sorry about that. I did it passing -w and still same result. However when I modified `range` back into `xrange` manually, it did modify that. But the function itself is still working. Could there be something wrong with the function itself? That's highly unlikely though, as the reference seems to be credible enough. – Silviu Tofan Jun 27 '16 at 18:38
  • I get no errors when using `hv3` with `python3`. It sounds like you are still having problems with `2to3`. –  Jun 27 '16 at 18:42
  • I just tried it again with the original example, and it does indeed work now. It's just not working for my existing algorithm, so I'll guess I'll go and figure that one out. Well, try at least. Thanks a bunch for your help! – Silviu Tofan Jun 27 '16 at 18:50
0

Use 2to3 and replace the following line of hv.py to fix TypeError:

decorated.sort()

to

sorted(decorated, key=lambda n: n[0])

It should work like python2.

JJ LIN
  • 3
  • 4