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

Update best trials when state is updated #376

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions optuna_dashboard/ts/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
artifactIsAvailable,
reloadIntervalState,
} from "./state"
import { getDominatedTrials } from "./dominatedTrials"

const localStorageGraphVisibility = "graphVisibility"
const localStorageReloadInterval = "reloadInterval"
Expand Down Expand Up @@ -119,6 +120,34 @@ export const actionCreator = () => {
newTrials[index] = newTrial
const newStudy: StudyDetail = Object.assign({}, studyDetails[studyId])
newStudy.trials = newTrials

// Update Best Trials
if (state === "Complete" && newStudy.directions.length === 1) {
// Single objective optimization
const bestValue = newStudy.best_trials.at(0)?.values?.at(0)
const currentValue = values?.at(0)
if (newStudy.best_trials.length === 0) {
newStudy.best_trials.push(newTrial)
} else if (bestValue !== undefined && currentValue !== undefined) {
if (newStudy.directions[0] === "minimize" && currentValue < bestValue) {
newStudy.best_trials = [newTrial]
} else if (
newStudy.directions[0] === "maximize" &&
currentValue > bestValue
) {
newStudy.best_trials = [newTrial]
} else if (currentValue == bestValue) {
newStudy.best_trials.push(newTrial)
}
}
} else if (state === "Complete") {
// Multi objective optimization
newStudy.best_trials = getDominatedTrials(
newStudy.trials,
newStudy.directions
)
}

setStudyDetailState(studyId, newStudy)
}

Expand Down
40 changes: 40 additions & 0 deletions optuna_dashboard/ts/dominatedTrials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const filterFunc = (trial: Trial, directions: StudyDirection[]): boolean => {
return (
trial.state === "Complete" &&
trial.values !== undefined &&
trial.values.length === directions.length &&
trial.values.every((v) => v !== "inf" && v !== "-inf")
)
}

export const getDominatedTrials = (
trials: Trial[],
directions: StudyDirection[]
): Trial[] => {
// TODO(c-bata): Use log-linear algorithm like Optuna.
// TODO(c-bata): Use this function at GraphParetoFront.
const filteredTrials = trials.filter((t: Trial) => filterFunc(t, directions))

const normalizedValues: number[][] = []
filteredTrials.forEach((t) => {
if (t.values && t.values.length === directions.length) {
const trialValues = t.values.map((v, i) => {
return directions[i] === "minimize" ? (v as number) : (-v as number)
})
normalizedValues.push(trialValues)
}
})
const dominatedTrials: boolean[] = []
normalizedValues.forEach((values0: number[], i: number) => {
const dominated = normalizedValues.some((values1: number[], j: number) => {
if (i === j) {
return false
}
return values0.every((value0: number, k: number) => {
return values1[k] <= value0
})
})
dominatedTrials.push(dominated)
})
return filteredTrials.filter((_, i) => !dominatedTrials.at(i))
}