gugarosa / opytimizer

🐦 Opytimizer is a Python library consisting of meta-heuristic optimization algorithms.
https://opytimizer.readthedocs.io
Apache License 2.0
599 stars 40 forks source link

[BUG] #24

Closed liuzd2017 closed 2 years ago

liuzd2017 commented 2 years ago

I just cannot find the optimization process except for the log. I want to know the API to see the optimization process,

gugarosa commented 2 years ago

Could you please provide what happened?

liuzd2017 commented 2 years ago

Sorry, I just cannot find the optimization process except for the log. I want to know the API to see the optimization process, such as fitness.---- Replied Message ----FromGustavo de @.>Date11/08/2021 22:24 @.> @.**@.>SubjectRe: [gugarosa/opytimizer] [BUG] (Issue #24) Could you please provide what happened?

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android.

[ { @.": "http://schema.org", @.": "EmailMessage", "potentialAction": { @.": "ViewAction", "target": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963211401", "url": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963211401", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { @.": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

liuzd2017 commented 2 years ago
        I delete my comment right away.---- Replied Message ***@***.***>Date11/08/2021 22:48 ***@***.***>SubjectRe: [gugarosa/opytimizer] [BUG] (Issue #24)
    Sorry, I just cannot find the optimization process except for the log. I want to know the API to see the optimization process, such as fitness.---- Replied Message ----FromGustavo de ***@***.***>Date11/08/2021 22:24 ***@***.***> ***@***.******@***.***>SubjectRe: [gugarosa/opytimizer] [BUG] (Issue #24)

Could you please provide what happened?

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android.

[ { @.": "http://schema.org", @.": "EmailMessage", "potentialAction": { @.": "ViewAction", "target": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963211401", "url": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963211401", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { @.": "Organization", "name": "GitHub", "url": "https://github.com" } } ]

gugarosa commented 2 years ago

There are two possible ways to check the outcome of the optimization process, let's dive into them. After conducting the optimization task, you could inspect the search space itself to retrieve the values that you want. Although this is not the most straightforward way because there is no history being saved, it is fairly easy and might give you some information right away:

import numpy as np
from opytimark.markers.n_dimensional import Sphere

from opytimizer import Opytimizer
from opytimizer.core import Function
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Random seed for experimental consistency
np.random.seed(0)

# Number of agents and decision variables
n_agents = 3
n_variables = 2

# Lower and upper bounds (has to be the same size as `n_variables`)
lower_bound = [-10, -10]
upper_bound = [10, 10]

# Creates the space, optimizer and function
space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound)
optimizer = PSO()
function = Function(Sphere())

# Bundles every piece into Opytimizer class
opt = Opytimizer(space, optimizer, function, save_agents=False)

# Runs the optimization task
opt.start(n_iterations=10)

# Inspects the optimization task inner objects
print(f'Best agent (position, fit): ({opt.space.best_agent.position}, {opt.space.best_agent.fit})')
for i, agent in enumerate(opt.space.agents):
    print(f'Agent[{i}] (position, fit): ({agent.position}, {agent.fit})')
gugarosa commented 2 years ago

On the other hand, it is possible to save the whole optimization task into a history object, which is saved as a pickle file into the disk. With the file in hands, you can inspect the convergence of the best agent, and every other aspect that was dumped throughout the optimization task:

import numpy as np
from opytimark.markers.n_dimensional import Sphere

from opytimizer import Opytimizer
from opytimizer.core import Function
from opytimizer.optimizers.swarm import PSO
from opytimizer.spaces import SearchSpace

# Random seed for experimental consistency
np.random.seed(0)

# Number of agents and decision variables
n_agents = 3
n_variables = 2

# Lower and upper bounds (has to be the same size as `n_variables`)
lower_bound = [-10, -10]
upper_bound = [10, 10]

# Creates the space, optimizer and function
space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound)
optimizer = PSO()
function = Function(Sphere())

# Bundles every piece into Opytimizer class
opt = Opytimizer(space, optimizer, function, save_agents=True)

# Runs the optimization task
opt.start(n_iterations=10)

# Saves the optimization task
opt.save('opt_task.pkl')

# One can load the optimization task from disk or work directly with the attribute that is saved
# History keys are saved as lists, where the last dimension stands for their iteration
# opt = Opytimizer.load('opt_task.pkl')

# Prints the last iteration best agent and checks that it matches the best agent in space
# Also prints a random iteration best agent
best_agent_pos, best_agent_fit = opt.history.get_convergence('best_agent')
print(f'Best agent (position, fit): ({best_agent_pos[:, -1]}, {best_agent_fit[-1]})')
print(f'Best agent (position, fit): ({opt.space.best_agent.position}, {opt.space.best_agent.fit})')
print(f'Iter 4 - Best agent (position, fit): ({best_agent_pos[:, 3]}, {best_agent_fit[3]})')

# As `save_agents` was passed as True to Opytimizer(),
# we can also inspect the convergence of the agents itself
agent_0_pos, agent_0_fit = opt.history.get_convergence('agents', index=0)
print(f'Agent[0] (position, fit): ({agent_0_pos[:, -1]}, {agent_0_fit[-1]})')
print(f'Iter 4 - Agent[0] (position, fit): ({agent_0_pos[:, 3]}, {agent_0_fit[3]})')
liuzd2017 commented 2 years ago
        Thank you very much---- Replied Message ----FromGustavo de ***@***.***>Date11/08/2021 22:52 ***@***.***> ***@***.******@***.***>SubjectRe: [gugarosa/opytimizer] [BUG] (Issue #24)

On the other hand, it is possible to save the whole optimization task into a history object, which is saved as a pickle file into the disk. With the file in hands, you can inspect the convergence of the best agent, and every other aspect that was dumped throughout the optimization task: import numpy as np from opytimark.markers.n_dimensional import Sphere

from opytimizer import Opytimizer from opytimizer.core import Function from opytimizer.optimizers.swarm import PSO from opytimizer.spaces import SearchSpace

Random seed for experimental consistency

np.random.seed(0)

Number of agents and decision variables

n_agents = 3 n_variables = 2

Lower and upper bounds (has to be the same size as n_variables)

lower_bound = [-10, -10] upper_bound = [10, 10]

Creates the space, optimizer and function

space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound) optimizer = PSO() function = Function(Sphere())

Bundles every piece into Opytimizer class

opt = Opytimizer(space, optimizer, function, save_agents=True)

Runs the optimization task

opt.start(n_iterations=10)

Saves the optimization task

opt.save('opt_task.pkl')

One can load the optimization task from disk or work directly with the attribute that is saved

History keys are saved as lists, where the last dimension stands for their iteration

opt = Opytimizer.load('opt_task.pkl')

Prints the last iteration best agent and checks that it matches the best agent in space

Also prints a random iteration best agent

best_agent_pos, best_agent_fit = opt.history.get_convergence('best_agent') print(f'Best agent (position, fit): ({best_agent_pos[:, -1]}, {best_agent_fit[-1]})') print(f'Best agent (position, fit): ({opt.space.best_agent.position}, {opt.space.best_agent.fit})') print(f'Iter 4 - Best agent (position, fit): ({best_agent_pos[:, 3]}, {best_agent_fit[3]})')

As save_agents was passed as True to Opytimizer(),

we can also inspect the convergence of the agents itself

agent_0_pos, agent_0_fit = opt.history.get_convergence('agents', index=0) print(f'Agent[0] (position, fit): ({agent_0_pos[:, -1]}, {agent_0_fit[-1]})') print(f'Iter 4 - Agent[0] (position, fit): ({agent_0_pos[:, 3]}, {agent_0_fit[3]})')

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android.

[ { @.": "http://schema.org", @.": "EmailMessage", "potentialAction": { @.": "ViewAction", "target": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963237059", "url": "https://github.com/gugarosa/opytimizer/issues/24#issuecomment-963237059", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { @.": "Organization", "name": "GitHub", "url": "https://github.com" } } ]