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

Problem running nsgaii with initial population #24

Closed edanweis closed 1 year ago

edanweis commented 6 years ago

I'm getting an error, or I am confused about how the nsga-ii algorithm works.

Why do results generate points in objective space which are not in the original CSV, when Problem functions are simply referring to their original values? See below:

result

And why can't I assign Integer type to the first variable? That seems to throw an error: IndexError: invalid index to scalar variable..

See the Gist and csv here

First I define the problem:

class BestActivity(Problem):
    def __init__(self):
        super(BestActivity, self).__init__(4, 2, 0)
        self.types[:] = [Real(0,570), Real(20, 4830), Real(1, 5), Real(0.7046248913, 562.812972)]
        self.directions[0] = Problem.MINIMIZE
        self.directions[1] = Problem.MAXIMIZE

    def evaluate(self, solution):
        convenience = solution.variables[1]
        desirability = solution.variables[2]
        solution.objectives[:] = [convenience, desirability]
        print(solution.variables)

Then a custom Generator with data from a csv file using list(df.itertuples().next()) from Pandas dataframe:

csv = pd.read_csv('nsga.csv', index_col=False, dtype={'key': np.int32, 'travel': np.int32, 'rating':np.float64, 'elevation': np.float64}).itertuples()

class MyGenerator(Generator):
    def __init__(self):
        super(MyGenerator, self).__init__()

    def generate(self, problem):
        solution = Solution(BestActivity())
        solution.variables= list(csv.next())[1:]
        return solution

Run and plot results:

algorithm = NSGAII(problem = BestActivity(), population_size = 542, generator = MyGenerator(), variator=None)
algorithm.run(10000)

x = [s.objectives[0] for s in algorithm.result]
y = [s.objectives[1] for s in algorithm.result]

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

plt.scatter(x,y)
plt.show()
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.