facebook / Ax

Adaptive Experimentation Platform
https://ax.dev
MIT License
2.35k stars 303 forks source link

Constraints on FixedParameters are not allowed #2455

Closed Fa20 closed 4 months ago

Fa20 commented 4 months ago

ax_client.create_experiment( name="multi_objective_optimization", parameters=[{"name": "x11", "type": "fixed", "value": 500, "value_type": "int"}, {"name": "x1", "type": "range", "bounds": [-1, 3], "value_type": "int"}, {"name": "x2", "type": "range", "bounds": [-2, 2], "value_type": "int"}, {"name": "x3", "type": "range", "bounds": [-2.0, 2.0], "value_type": "float"}, {"name": "x4", "type": "range", "bounds": [-20, 20], "value_type": "int"} ], objectives={ "objective_1": ObjectiveProperties(minimize=True),
"objective_2": ObjectiveProperties(minimize=False), }, parameter_constraints=[ "x4 - 1.5x3 >= 0", # Constraint involving x4 and x3 "x2 - 5x1 <= 0", # Constraint involving x2 and x1 "x11 - x3 >= 0", ]. How can I fix this problem in case that I want to keep this constarined "x11 - x3 >= 0" and x11 has a fixed value

another question: does AX support complex constarined like : dist = 100 / x4 length = ((x2 / np.tan(np.radians(x3))) / (x11 math.pi) 100)

constarined= (length / dist) >= 3

bernardbeckerman commented 4 months ago

Hi @Fa20, Can you manually input the fixed bavlue of x11 into your constraint? So your example becomes:

ax_client.create_experiment(
    name="multi_objective_optimization",
    parameters=[
        {"name": "x11", "type": "fixed", "value": 500, "value_type": "int"},
        {"name": "x1", "type": "range", "bounds": [-1, 3], "value_type": "int"},
        {"name": "x2", "type": "range", "bounds": [-2, 2], "value_type": "int"},
        {"name": "x3", "type": "range", "bounds": [-2.0, 2.0], "value_type": "float"},
        {"name": "x4", "type": "range", "bounds": [-20, 20], "value_type": "int"}
    ],
    objectives={
        "objective_1": ObjectiveProperties(minimize=True),
        "objective_2": ObjectiveProperties(minimize=False),
    },
    parameter_constraints=[
        "x4 - 1.5*x3 >= 0", # Constraint involving x4 and x3
        "x2 - 5*x1 <= 0", # Constraint involving x2 and x1
        "x3 <= 500", # This line is updated to use the fixed value of x11
    ],
)

another question: does AX support complex constarined like :

Ax only supports linear constraints, since those are easily passed down to the modeling layer and incorporated into the optimization. One imperfect-but-potentially-useful way to incorporate these constraints is as OutcomeConstraints, i.e., supply the scalar you'd like to constrain as an outcome of your experiment, and then set constraints on that scalar's value via outcome constraints (pointer). Hope that helps!

Fa20 commented 4 months ago

@bernardbeckerman

Hi @Fa20, Can you manually input the fixed bavlue of x11 into your constraint? So your example becomes:

ax_client.create_experiment(
    name="multi_objective_optimization",
    parameters=[
        {"name": "x11", "type": "fixed", "value": 500, "value_type": "int"},
        {"name": "x1", "type": "range", "bounds": [-1, 3], "value_type": "int"},
        {"name": "x2", "type": "range", "bounds": [-2, 2], "value_type": "int"},
        {"name": "x3", "type": "range", "bounds": [-2.0, 2.0], "value_type": "float"},
        {"name": "x4", "type": "range", "bounds": [-20, 20], "value_type": "int"}
    ],
    objectives={
        "objective_1": ObjectiveProperties(minimize=True),
        "objective_2": ObjectiveProperties(minimize=False),
    },
    parameter_constraints=[
        "x4 - 1.5*x3 >= 0", # Constraint involving x4 and x3
        "x2 - 5*x1 <= 0", # Constraint involving x2 and x1
        "x3 <= 500", # This line is updated to use the fixed value of x11
    ],
)

another question: does AX support complex constarined like :

Ax only supports linear constraints, since those are easily passed down to the modeling layer and incorporated into the optimization. One imperfect-but-potentially-useful way to incorporate these constraints is as OutcomeConstraints, i.e., supply the scalar you'd like to constrain as an outcome of your experiment, and then set constraints on that scalar's value via outcome constraints (pointer). Hope that helps!

@bernardbeckerman thanks for your answer . how can I convert the constarined to scaler and it is depend on the value of x1,x2 etc. which has range between two values? as wellas I have 3 constatins not just the above one?

bernardbeckerman commented 4 months ago

Following the example in this tutorial, you can do this by adding an outcome_constraint to the ax_client.create_experiment call, e.g.,

ax_client.create_experiment(
    ...
    outcome_constraints=["length_over_dist >= 3"]
)

and then adding the calculation of the desired quantity to the evaluate function and including it in the output dictionary, e.g.,

def evaluate(parameterization):
    x = np.array([parameterization.get(f"x{i+1}") for i in range(6)])
    dist = 100 / x4
    length = ((x2 / np.tan(np.radians(x3))) / (x11 * math.pi) * 100
    # In our case, standard error is 0, since we are computing a synthetic function.
    return {
        "hartmann6": (hartmann6(x), 0.0), 
        "l2norm": (np.sqrt((x**2).sum()), 0.0), 
        "length_over_dist": (length/dist, 0.0)
    }

Hope that helps!

Fa20 commented 4 months ago

Does this the same like if we set the constrained on parameters?does this mean that the suggested values of parameters will satisfy this condition. Because I need the values of the parameters to simulate the objective functions after each trial in order to get the value of the objective functions

Fa20 commented 4 months ago

and I have MOO problem

bernardbeckerman commented 4 months ago

If you follow my recommendation here (link) to avoid computation of the objective(s) if the (nonlinear) constraint is violated, then any trial with status COMPLETED should satisfy the constraint. This should work with constrained or non-constrained optimization. Does that make sense?

bernardbeckerman commented 4 months ago

@Fa20 have you had luck with the above suggestion? Closing this out for now but please feel free to comment/reopen with any further questions.

Fa20 commented 4 months ago

ok thank you very much