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

[BUG]: New error with termination with Time #176

Open
kwnspir opened this issue Jan 4, 2025 · 2 comments
Open

[BUG]: New error with termination with Time #176

kwnspir opened this issue Jan 4, 2025 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kwnspir
Copy link

kwnspir commented Jan 4, 2025

Description of the bug

I want to run multiple optimizers for a problem and each to have a time terminator, but when i use term_dict as the examples say most of the time used for the first algorithm and almost nothing for the rest of them.
(Using multiple dicts doesnt help)
image
image
image

Steps To Reproduce

import time
import numpy as np
from mealpy import FloatVar, GA, PSO, DE, AOA

Objective function

def objective_func(solution):
return np.sum(solution**2)

Problem definition

problem_dict = {
"obj_func": objective_func,
"bounds": FloatVar(lb=[-100] * 30, ub=[100] * 30),
"minmax": "min",
}

term_dict = {
"max_time": 10 # seconds to run this algorithm only
}

Iter=1500
Pop=50

Run Genetic Algorithm

start_time = time.time()
optimizer_ga = GA.BaseGA(epoch=Iter, pop_size=Pop, pc=0.85, pm=0.1)
optimizer_ga.solve(problem_dict, termination=term_dict)
ga_runtime = time.time() - start_time
print("\nGenetic Algorithm:")
print(f"Best Solution: {optimizer_ga.g_best.solution}")
print(f"Best Fitness: {optimizer_ga.g_best.target.fitness}")
print(f"Runtime: {ga_runtime:.4f} seconds")

Run Particle Swarm Optimization

start_time = time.time()
optimizer_pso = PSO.OriginalPSO(epoch=Iter, pop_size=Pop)
optimizer_pso.solve(problem_dict, termination=term_dict)
pso_runtime = time.time() - start_time
print("\nParticle Swarm Optimization:")
print(f"Best Solution: {optimizer_pso.g_best.solution}")
print(f"Best Fitness: {optimizer_pso.g_best.target.fitness}")
print(f"Runtime: {pso_runtime:.4f} seconds")

Run Differential Evolution

start_time = time.time()
optimizer_de = AOA.OriginalAOA(epoch=Iter, pop_size=Pop, wf=0.8, cr=0.9)
optimizer_de.solve(problem_dict, termination=term_dict)
de_runtime = time.time() - start_time
print("\nDifferential Evolution:")
print(f"Best Solution: {optimizer_de.g_best.solution}")
print(f"Best Fitness: {optimizer_de.g_best.target.fitness}")
print(f"Runtime: {de_runtime:.4f} seconds")

Additional Information

i use mealpy 3.0.1

@kwnspir kwnspir added the bug Something isn't working label Jan 4, 2025
@thieu1995
Copy link
Owner

Hi @kwnspir,

There is a huge different between using time.time() and time.perf_counter(). We use time.perf_counter() in the search process. So when you use time.time() to wrap the outside computation, it will show the difference. Also, the time.time() will be effect when you are running multiple processes like checking website, opening word, other program.

@kwnspir
Copy link
Author

kwnspir commented Jan 5, 2025

What do you suggest me, i want each algorithm to run no more than a specified amount of time.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants