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

#59 changed obj function to only return float #60

Merged
merged 4 commits into from
Oct 2, 2024

Conversation

jkriwet
Copy link
Contributor

@jkriwet jkriwet commented Apr 15, 2024

The objective functions of the calibrator returns a tuple: (total_res, unweighted objective).

ebcpy needs the obj function to return only a float, because the optimization algorithms need this.

Interestingly, this is almost not noticable. See the following code as an example:

from scipy.optimize import differential_evolution

def black_box_function(x, *args):
    """Function with unknown internals we wish to maximize.

    This is just serving as an example, for all intents and
    purposes think of the internals of this function, i.e.: the process
    which generates its output values, as unknown.
    """
    obj = -x[0] ** 2 - (x[1] - 1) ** 2 + 1
    print(f"Objective Value: {obj}")
    return (obj, 'Test')


# Bounded region of parameter space
pbounds = [(2, 4), (-3, 3)]

res = differential_evolution(
            func=black_box_function,
            bounds=pbounds,
            strategy="best1bin")

print(res)

The "black_box_function" is the equivalent of the obj function. When beeing evaluated during the optimization, this evaluates correctly (Observable prints of the values). Only at a specific time (in this case when a new population is to be created) there will be an error:

RuntimeError: func(x, *args) must return a scalar value

It is though noticable when implementing different optimization algorithms, which immediately cant handle tuple returns of the obj function. Therefore this has to be changed.

Closes #59

@jkriwet jkriwet linked an issue Apr 15, 2024 that may be closed by this pull request
@FWuellhorst
Copy link
Collaborator

Thanks for noticing. You need to change the validation part as well. Maybe call the obj function using an arg with a new parameter verbose with the default being False? If this does not work maybe a class instance argument

@jkriwet
Copy link
Contributor Author

jkriwet commented Apr 15, 2024

Yes, this works.

…g_of_cd_to_working_directory_by_ebcpy' into 59-correct-obj-function
@jkriwet jkriwet merged commit cc4a464 into master Oct 2, 2024
@jkriwet jkriwet deleted the 59-correct-obj-function branch October 2, 2024 10:24
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

Successfully merging this pull request may close these issues.

correct obj function
2 participants