Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
porink0424 committed May 10, 2024
1 parent 8c1d9f8 commit c432140
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions tslib/storage/test/generate_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ def objective_multi(trial: optuna.Trial) -> tuple[float, float]:

study.optimize(objective_multi, n_trials=50)

# Multi-objective study with constraints
def objective_constraints(trial: optuna.Trial) -> tuple[float, float]:
x = trial.suggest_float("x", -15, 30)
y = trial.suggest_float("y", -15, 30)
c0 = (x - 5) ** 2 + y ** 2 - 25
c1 = -((x - 8) ** 2) - (y + 3) ** 2 + 7.7
trial.set_user_attr("constraint", (c0, c1))
v0 = 4 * x ** 2 + 4 * y ** 2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1

def constraints(trial: optuna.Trial):
return trial.user_attrs["constraint"]

sampler = optuna.samplers.NSGAIISampler(constraints_func=constraints)
study = optuna.create_study(
study_name="multi-objective-constraints",
storage=storage,
sampler=sampler,
directions=["minimize", "minimize"],
)
print(f"Generating {study.study_name} for {type(storage).__name__}...")
study.optimize(objective_constraints, n_trials=32, timeout=600)


if __name__ == "__main__":
remove_assets()
Expand Down
9 changes: 8 additions & 1 deletion tslib/storage/test/journal.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ describe("Test Journal File Storage", async () => {
assert.deepStrictEqual(study.metric_names, ["value1", "value2"])
})

it("Check the study with constraints", () => {
const study = studies.find((s) => s.name === "multi-objective-constraints")
for (const trial of study.trials) {
assert.strictEqual(trial.constraints.length, 2)
}
})

it("Check the number of studies", () => {
const N_STUDIES = 5
const N_STUDIES = 6
assert.strictEqual(studies.length, N_STUDIES)
})
})

0 comments on commit c432140

Please sign in to comment.