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

Ts/3630 interventions for calibrate #3677

Merged
merged 29 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8a0071c
calibrate interventions from model config
Tom-Szendrey May 21, 2024
bd1599d
OptimizedIntervention -> s
Tom-Szendrey May 22, 2024
bbb756a
rename for clarity, add Intervention to optimize
Tom-Szendrey May 22, 2024
4e79f65
adding fixed interventions for optimize
Tom-Szendrey May 22, 2024
75ded59
adding multiselect back as its a list
Tom-Szendrey May 22, 2024
9127565
removing console.log
Tom-Szendrey May 22, 2024
8f66d17
chore: lint server codebase
Tom-Szendrey May 22, 2024
3a50eff
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 22, 2024
8986821
Merge branch 'TS/3630-interventions-for-calibrate' of https://github.…
Tom-Szendrey May 22, 2024
0931eab
project Id fixes?
Tom-Szendrey May 22, 2024
c6ed514
chore: lint server codebase
Tom-Szendrey May 22, 2024
022cb62
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 22, 2024
a6a0cef
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 23, 2024
dec0c91
multiselect
Tom-Szendrey May 23, 2024
b80f77e
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 27, 2024
cb8b9a9
minor correction to comment
Tom-Szendrey May 27, 2024
3f9e616
pass on any existing interventions
Tom-Szendrey May 27, 2024
a4884aa
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 27, 2024
7bf859f
removing project ID from simulation request calls
Tom-Szendrey May 27, 2024
7017143
Rename to PolicyInterventions for clarity
Tom-Szendrey May 28, 2024
2d15849
chore: lint server codebase
Tom-Szendrey May 28, 2024
42d7c59
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 28, 2024
c079873
Merge branch 'TS/3630-interventions-for-calibrate' of https://github.…
Tom-Szendrey May 28, 2024
a71267d
removing no longer required empty check
Tom-Szendrey May 28, 2024
66036d8
adding back in the empty checks - misunderstood
Tom-Szendrey May 28, 2024
8de8fa4
use e instead of custom error message in catch
Tom-Szendrey May 28, 2024
a6b3529
revert
Tom-Szendrey May 28, 2024
20df3f9
tough luck buddy error message
Tom-Szendrey May 28, 2024
53ab1cd
Merge branch 'main' of https://github.com/DARPA-ASKEM/TERArium into T…
Tom-Szendrey May 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ watch(
console.log('dill URL is', dillURL);

const forecastResponse = await makeForecastJobCiemss({
projectId: '',
modelConfigId: modelConfigId.value as string,
timespan: {
start: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ export async function getOptimizedInterventions(optimizeRunId: string) {
// Get the interventionPolicyGroups from the simulation object.
// This will prevent any inconsistencies being passed via knobs or state when matching with result file.
const simulation = await getSimulation(optimizeRunId);
const interventions = simulation?.executionPayload?.interventions;
const interventionType = interventions.selection ?? '';
const paramNames: string[] = interventions.param_names ?? [];
const paramValue: number[] = interventions.param_values ?? [];
const startTime: number[] = interventions.start_time ?? [];
const simulationIntervetions: SimulationIntervention[] =
simulation?.executionPayload.fixed_static_parameter_interventions ?? [];
const policyInterventions = simulation?.executionPayload?.policy_interventions;
const interventionType = policyInterventions.selection ?? '';
const paramNames: string[] = policyInterventions.param_names ?? [];
const paramValue: number[] = policyInterventions.param_values ?? [];
const startTime: number[] = policyInterventions.start_time ?? [];

const policyResult = await getRunResult(optimizeRunId, 'policy.json');
const simulationIntervetions: SimulationIntervention[] = [];

if (interventionType === InterventionTypes.paramValue && startTime.length !== 0) {
// intervention type == parameter value
for (let i = 0; i < paramNames.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const pollResult = async (runId: string) => {

const startForecast = async (simulationIntervetions) => {
const simulationPayload: SimulationRequest = {
projectId: '',
modelConfigId: modelConfigId.value as string,
timespan: {
start: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<div class="constraint-row">
<div class="label-and-input">
<label>Target-variable</label>
<Dropdown
<MultiSelect
class="p-inputtext-sm"
:options="modelStateAndObsOptions"
v-model="knobs.targetVariables"
Expand Down Expand Up @@ -296,6 +296,7 @@ import InputText from 'primevue/inputtext';
import InputNumber from 'primevue/inputnumber';
import SelectButton from 'primevue/selectbutton';
import Dialog from 'primevue/dialog';
import MultiSelect from 'primevue/multiselect';
import TeraOptimizeChart from '@/components/workflow/tera-optimize-chart.vue';
import TeraSimulateChart from '@/components/workflow/tera-simulate-chart.vue';
import TeraDatasetDatatable from '@/components/dataset/tera-dataset-datatable.vue';
Expand Down Expand Up @@ -323,7 +324,7 @@ import {
ModelParameter,
OptimizeRequestCiemss,
CsvAsset,
OptimizedIntervention
OptimizedInterventions
} from '@/types/Types';
import { logger } from '@/utils/logger';
import { chartActionsProxy, drilldownChartSize } from '@/components/workflow/util';
Expand Down Expand Up @@ -545,7 +546,7 @@ const runOptimize = async () => {
listBoundsInterventions.push([ele.upperBound]);
});

const optimizeInterventions: OptimizedIntervention = {
const optimizeInterventions: OptimizedInterventions = {
selection: knobs.value.interventionType,
paramNames,
startTime,
Expand All @@ -560,7 +561,7 @@ const runOptimize = async () => {
start: 0,
end: knobs.value.endTime
},
interventions: optimizeInterventions,
policyInterventions: optimizeInterventions,
qoi: {
contexts: knobs.value.targetVariables,
method: knobs.value.qoiMethod
Expand All @@ -581,7 +582,6 @@ const runOptimize = async () => {
if (inferredParameters.value) {
optimizePayload.extra.inferredParameters = inferredParameters.value[0];
}

const optResult = await makeOptimizeJobCiemss(optimizePayload);
const state = _.cloneDeep(props.node.state);
state.inProgressOptimizeId = optResult.simulationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ const makeForecastRequest = async () => {
const state = props.node.state;

const payload: SimulationRequest = {
projectId: '',
modelConfigId,
timespan: {
start: state.currentTimespan.start,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ const makeForecastRequest = async (): Promise<string> => {

const state = props.node.state;
const payload: SimulationRequest = {
projectId: useProjects().activeProject.value?.id as string,
modelConfigId: configId,
timespan: {
start: state.currentTimespan.start,
Expand Down
7 changes: 4 additions & 3 deletions packages/client/hmi-client/src/types/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ export interface CalibrationRequestCiemss {
modelConfigId: string;
extra: any;
timespan?: TimeSpan;
interventions?: Intervention[];
dataset: DatasetLocation;
engine: string;
}
Expand Down Expand Up @@ -686,7 +687,8 @@ export interface EnsembleSimulationCiemssRequest {
export interface OptimizeRequestCiemss {
modelConfigId: string;
timespan: TimeSpan;
interventions?: OptimizedIntervention;
policyInterventions?: OptimizedInterventions;
fixedStaticParameterInterventions?: Intervention[];
mwdchang marked this conversation as resolved.
Show resolved Hide resolved
stepSize?: number;
qoi: OptimizeQoi;
riskBound: number;
Expand All @@ -711,7 +713,6 @@ export interface SimulationRequest {
timespan: TimeSpan;
extra: any;
engine: string;
projectId: string;
interventions?: Intervention[];
}

Expand Down Expand Up @@ -748,7 +749,7 @@ export interface OptimizeQoi {
method: string;
}

export interface OptimizedIntervention {
export interface OptimizedInterventions {
selection: string;
paramNames: string[];
paramValues?: number[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ResponseEntity<Simulation> makeForecastRun(
sim.setStatus(ProgressState.QUEUED);

// FIXME: These fiels are arguable unnecessary
final Optional<Project> project = projectService.getProject(request.getProjectId());
final Optional<Project> project = projectService.getProject(projectId);
if (project.isPresent()) {
sim.setProjectId(project.get().getId());
sim.setUserId(project.get().getUserId());
Expand Down Expand Up @@ -174,7 +174,7 @@ public ResponseEntity<Simulation> makeForecastRunCiemss(
sim.setExecutionPayload(objectMapper.convertValue(request, JsonNode.class));
sim.setStatus(ProgressState.QUEUED);

final Optional<Project> project = projectService.getProject(request.getProjectId());
final Optional<Project> project = projectService.getProject(projectId);
if (project.isPresent()) {
sim.setProjectId(project.get().getId());
sim.setUserId(project.get().getUserId());
Expand Down Expand Up @@ -205,15 +205,67 @@ public ResponseEntity<JobResponse> makeCalibrateJob(@RequestBody final Calibrati

@PostMapping("ciemss/calibrate")
@Secured(Roles.USER)
public ResponseEntity<JobResponse> makeCalibrateJobCiemss(@RequestBody final CalibrationRequestCiemss request) {
public ResponseEntity<JobResponse> makeCalibrateJobCiemss(
@RequestBody final CalibrationRequestCiemss request, @RequestParam("project-id") final UUID projectId) {
Schema.Permission permission =
projectService.checkPermissionCanWrite(currentUserService.get().getId(), projectId);

// Get model config's interventions and append them to requests:
try {
final Optional<ModelConfiguration> modelConfiguration =
Tom-Szendrey marked this conversation as resolved.
Show resolved Hide resolved
modelConfigService.getAsset(request.getModelConfigId(), permission);
if (modelConfiguration.isEmpty()) {
return ResponseEntity.notFound().build();
}
final List<Intervention> modelInterventions =
modelConfiguration.get().getInterventions();
if (modelInterventions != null) {
List<Intervention> allInterventions = request.getInterventions();
if (allInterventions == null) {
allInterventions = new ArrayList<Intervention>();
}
allInterventions.addAll(modelInterventions);
request.setInterventions(allInterventions);
}
} catch (IOException e) {
String error = "Unable to find model configuration";
log.error(error, e);
mwdchang marked this conversation as resolved.
Show resolved Hide resolved
throw new ResponseStatusException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, error);
}
return ResponseEntity.ok(simulationCiemssServiceProxy
.makeCalibrateJob(convertObjectToSnakeCaseJsonNode(request))
.getBody());
}

@PostMapping("ciemss/optimize")
@Secured(Roles.USER)
public ResponseEntity<JobResponse> makeOptimizeJobCiemss(@RequestBody final OptimizeRequestCiemss request) {
public ResponseEntity<JobResponse> makeOptimizeJobCiemss(
@RequestBody final OptimizeRequestCiemss request, @RequestParam("project-id") final UUID projectId) {
Schema.Permission permission =
projectService.checkPermissionCanWrite(currentUserService.get().getId(), projectId);

// Get model config's interventions and append them to requests:
try {
final Optional<ModelConfiguration> modelConfiguration =
modelConfigService.getAsset(request.getModelConfigId(), permission);
if (modelConfiguration.isEmpty()) {
return ResponseEntity.notFound().build();
}
final List<Intervention> modelInterventions =
modelConfiguration.get().getInterventions();
if (modelInterventions != null) {
List<Intervention> allInterventions = request.getFixedStaticParameterInterventions();
if (allInterventions == null) {
allInterventions = new ArrayList<Intervention>();
}
allInterventions.addAll(modelInterventions);
request.setFixedStaticParameterInterventions(allInterventions);
}
} catch (IOException e) {
String error = "Unable to find model configuration";
log.error(error, e);
Tom-Szendrey marked this conversation as resolved.
Show resolved Hide resolved
throw new ResponseStatusException(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR, error);
}
return ResponseEntity.ok(simulationCiemssServiceProxy
.makeOptimizeJob(convertObjectToSnakeCaseJsonNode(request))
.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.fasterxml.jackson.annotation.JsonAlias;
import java.io.Serializable;
import java.util.List;
import java.util.UUID;
import lombok.Data;
import lombok.experimental.Accessors;
import software.uncharted.terarium.hmiserver.annotations.TSModel;
import software.uncharted.terarium.hmiserver.annotations.TSOptional;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.DatasetLocation;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.Intervention;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.TimeSpan;

@Data
Expand All @@ -15,13 +18,16 @@
// Used to kick off a calibration job in simulation-service
public class CalibrationRequestCiemss implements Serializable {
@JsonAlias("model_config_id")
private String modelConfigId;
private UUID modelConfigId;

private Object extra;
mwdchang marked this conversation as resolved.
Show resolved Hide resolved

@TSOptional
private TimeSpan timespan;

@TSOptional
private List<Intervention> interventions;

private DatasetLocation dataset;
private String engine;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.fasterxml.jackson.annotation.JsonAlias;
import java.io.Serializable;
import java.util.List;
import java.util.UUID;
import lombok.Data;
import lombok.experimental.Accessors;
import software.uncharted.terarium.hmiserver.annotations.TSModel;
import software.uncharted.terarium.hmiserver.annotations.TSOptional;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.Intervention;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.OptimizeExtra;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.OptimizeQoi;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.OptimizedIntervention;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.OptimizedInterventions;
import software.uncharted.terarium.hmiserver.models.simulationservice.parts.TimeSpan;

@Data
Expand All @@ -18,14 +20,17 @@
// Used to kick off a Optimize job in simulation-service
public class OptimizeRequestCiemss implements Serializable {
@JsonAlias("model_config_id")
private String modelConfigId;
private UUID modelConfigId;

private TimeSpan timespan;

@TSOptional
// FIXME: make pluraal more consistent here:
// https://github.com/DARPA-ASKEM/pyciemss-service/blob/main/service/models/operations/optimize.py#L80
private OptimizedIntervention interventions;
private OptimizedInterventions policyInterventions;

@TSOptional
// The interventions provided via the model config which are not being optimized on
private List<Intervention> fixedStaticParameterInterventions;

@JsonAlias("step_size")
@TSOptional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class SimulationRequest implements Serializable {

private String engine;

private UUID projectId;

@TSOptional
private List<Intervention> interventions;

Expand All @@ -44,7 +42,6 @@ public SimulationRequest clone() {
: null);
clone.setExtra(this.extra.deepCopy());
clone.setEngine(this.engine);
clone.setProjectId(this.projectId);
clone.setInterventions(new ArrayList<>());
for (final Intervention intervention : this.interventions) {
clone.getInterventions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Accessors(chain = true)
@TSModel
// Used to specify any interventions provided by the AMR and given to the simulation-service.
public class OptimizedIntervention {
public class OptimizedInterventions {
private String selection;

@JsonAlias("param_names")
Expand Down
Loading