Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimenter using own problem #145

Closed
sabrinadraude opened this issue May 12, 2020 · 12 comments
Closed

Experimenter using own problem #145

sabrinadraude opened this issue May 12, 2020 · 12 comments

Comments

@sabrinadraude
Copy link

Hi there,

I was just wondering how to use the experimenter using ones own problem rather than a predefined problem?

Run the algorithm

N = 2000
problem = Problem(multiple_days_assigned_size, 2, 2)
problem.directions[1] = Problem.MINIMIZE
problem.directions[0] = Problem.MAXIMIZE
problem.constraints[:] = ">=0"
problem.types[:] = Integer(0, my_counter_again - 1)
problem.function = lambda x: evaluation_function2(x, jobs, c_time_added_up, total_allowed_time_all_jobs,
extra_time)
algorithm = NSGAII(problem)
algorithm.run(N)
feasible_solutions_total = [s for s in algorithm.result if s.feasible]
Nondominated_solutions_total = nondominated(algorithm.result)

calculate the difference in models nsga2 and nsga3

if name == "main":
algorithms = [NSGAII, (NSGAIII, {"divisions_outer":12})]
problems = [Problem(multiple_days_assigned_size, 2, 2)] #what would I put in here?

# run the experiment
results = experiment(algorithms, problems, nfe=10000, seeds=10)

Any tips would be greatly appreciated!

Thanks

@grafkevin
Copy link

I am having the same problem.

@sabrinadraude
Copy link
Author

Sorry I have not yet received a reply.

@jetuk
Copy link
Contributor

jetuk commented May 19, 2020

What error message are you getting?

@sabrinadraude
Copy link
Author

solution.variables = [x.rand() for x in problem.types]
AttributeError: 'NoneType' object has no attribute 'rand'

My question is how to set out the code for using your own problem in the experimenter generator rather than a predefined problem such as DTLZ2 or Belegundu.

N = 2 problem = Problem(multiple_days_assigned_size, 2, 2) problem.directions[1] = Problem.MINIMIZE problem.directions[0] = Problem.MAXIMIZE problem.constraints[:] = ">=0" problem.types[:] = Integer(0, my_counter_again - 1) problem.function = lambda x: evaluation_function2(x, jobs, c_time_added_up, total_allowed_time_all_jobs, extra_time)

# calculate the difference in models nsga2 and nsga3 if __name__ == "__main__": algorithms = [NSGAII, (NSGAIII, {"divisions_outer":12})] problems = [Problem(multiple_days_assigned_size, 2, 2)]

# run the experiment
results = experiment(algorithms, problems, nfe=10000, seeds=10) `

# calculate the hypervolume indicator between two algorithms hyp = Hypervolume(minimum=[min_pri, min_cost], maximum=[max_pri, max_cost]) hyp_result = calculate(results, hyp) display(hyp_result, ndigits=3)

@jetuk
Copy link
Contributor

jetuk commented May 19, 2020

I think the issue is that you are redefining the problem on this line:

problems = [Problem(multiple_days_assigned_size, 2, 2)]

I.e. the problem here is not the same Problem instances as is assigned to the problem variable at the start of the script. Consequently this new instance has no types, etc. defined, hence the error about None.

I think you need to use the reference/variable to the problem you have setup at the start of the script, and pass that to experiment. Something like this:

N = 2
problem = Problem(multiple_days_assigned_size, 2, 2)
problem.directions[1] = Problem.MINIMIZE
problem.directions[0] = Problem.MAXIMIZE
problem.constraints[:] = ">=0"
problem.types[:] = Integer(0, my_counter_again - 1)
problem.function = lambda x: evaluation_function2(x, jobs, c_time_added_up, total_allowed_time_all_jobs, extra_time)

# calculate the difference in models nsga2 and nsga3
if __name__ == "__main__": 
    algorithms = [NSGAII, (NSGAIII, {"divisions_outer":12})]

    # run the experiment
    results = experiment(algorithms, [problem], nfe=10000, seeds=10) `

    # calculate the hypervolume indicator between two algorithms
    hyp = Hypervolume(minimum=[min_pri, min_cost], maximum=[max_pri, max_cost]) 
    hyp_result = calculate(results, hyp) display(hyp_result, ndigits=3)

PS if you put your code between three back-ticks it'll format correctly and be easier to read. See: https://guides.github.com/features/mastering-markdown/

@grafkevin
Copy link

Hi Jetuk, thanks for your help!

I achieved to get passed the AttributeError: 'NoneType' object has no attribute 'rand' error. But now I get this error: AttributeError: 'NSGAII' object has no attribute '__name__'

Here's my code:

# Custom experimenter
from platypus import *

problem = Problem(2, 2)
problem.types[0:] = Permutation(range(len(carbon_bus)))
problem.types[1:] = Binary(len(carbon_bus))
problem.directions[:] = Problem.MINIMIZE
problem.function = two_opt

if __name__ == "__main__":
    algorithms = [NSGAII(problem,variator=PMX()), (NSGAIII, {"divisions_outer":12})]
    
    # run the experiment
    results = experiment(algorithms, problem, seeds=5, nfe=1000)

    # calculate the hypervolume indicator 
    #hyp = Hypervolume(minimum=[0, 0, 0], maximum=[1, 1, 1])
    #hyp_result = calculate(results, hyp)
    #display(hyp_result, ndigits=3)

I am working on a bi-objective TSP.

@jetuk
Copy link
Contributor

jetuk commented May 20, 2020

Can you paste the full traceback?

@grafkevin
Copy link

Sure, sorry for the confusion!

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-91-4e87550fe452> in <module>
     12 
     13     # run the experiment
---> 14     results = experiment(algorithms, problem, seeds=5, nfe=1000)
     15 
     16     # calculate the hypervolume indicator

c:\users\kevin\appdata\local\programs\python\python37\lib\site-packages\platypus\experimenter.py in experiment(algorithms, problems, seeds, nfe, evaluator, display_stats)
    179         evaluator = PlatypusConfig.default_evaluator
    180 
--> 181     job_results = evaluator.evaluate_all(generator)
    182 
    183     # convert results to structured format

c:\users\kevin\appdata\local\programs\python\python37\lib\site-packages\platypus\evaluator.py in evaluate_all(self, jobs, **kwargs)
     86 
     87         if log_frequency is None:
---> 88             return list(self.map_func(run_job, jobs))
     89         else:
     90             result = []

c:\users\kevin\appdata\local\programs\python\python37\lib\site-packages\platypus\experimenter.py in evaluate_job_generator(algorithms, problems, seeds, nfe, display_stats)
     89         else:
     90             algorithm = algorithms[i]
---> 91             algorithm_name = algorithm.__name__
     92             kwargs = {}
     93 

AttributeError: 'NSGAII' object has no attribute '__name__'

@jetuk
Copy link
Contributor

jetuk commented May 20, 2020

I think the experimenter is expecting algorithm classes, not instances. Try this:

# Custom experimenter
from platypus import *

problem = Problem(2, 2)
problem.types[0:] = Permutation(range(len(carbon_bus)))
problem.types[1:] = Binary(len(carbon_bus))
problem.directions[:] = Problem.MINIMIZE
problem.function = two_opt

if __name__ == "__main__":
    algorithms = [(NSGAII, {"variator": PMX()}), (NSGAIII, {"divisions_outer":12})]
    
    # run the experiment
    results = experiment(algorithms, problem, seeds=5, nfe=1000)

    # calculate the hypervolume indicator 
    #hyp = Hypervolume(minimum=[0, 0, 0], maximum=[1, 1, 1])
    #hyp_result = calculate(results, hyp)
    #display(hyp_result, ndigits=3)

@sabrinadraude
Copy link
Author

Hi there,

Thank you for your help.

I think the new code worked however I got the issue:

raise PlatypusError("NSGAIII currently only works with minimization problems")
platypus.core.PlatypusError: NSGAIII currently only works with minimization problems

Do any of the other GAs work with maximization problems or is it just NSGAII?

Cheers,

Sabrina

@jetuk
Copy link
Contributor

jetuk commented May 21, 2020

I am not sure, you'll have to check the code. A work around is to use minimisation and return the negative of the value for objectives you wish to maximise.

@sabrinadraude
Copy link
Author

Thanks the code works for minimisation.

However when I try and run the code using the evaluator it hasn't finished, even though the code has been running for three hours now:

N = 10
problem = Problem(multiple_days_assigned_size, 2, 2)
problem.directions[1] = Problem.MINIMIZE
problem.directions[0] = Problem.MINIMIZE
problem.constraints[:] = ">=0"
problem.types[:] = Integer(0, my_counter_again - 1)
problem.function = lambda x: evaluation_function2(x, jobs, c_time_added_up, total_allowed_time_all_jobs,
                                                  extra_time)
algorithm = NSGAII(problem)
algorithm.run(N)

# Calculate the hypervolume using assigned objective max and mins for comparing nsga2
min_pri = 0
min_cost = multiple_days_assigned_size * 30
max_pri = multiple_days_assigned_size * 6.53
max_cost = multiple_days_assigned_size * 130
hyp = Hypervolume(minimum=[min_pri, min_cost], maximum=[max_pri, max_cost])

if __name__ == "__main__":
    algorithms = [NSGAII, (NSGAIII, {"divisions_outer": 12})]

    with ProcessPoolEvaluator(2) as evaluator:
        results = experiment(algorithms, [problem], nfe=100, evaluator=evaluator)
        hyp = Hypervolume(minimum=[min_pri, min_cost], maximum=[max_pri, max_cost])
        hyp_result = calculate(results, hyp, evaluator=evaluator)
        display(hyp_result, ndigits=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants