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

Load simulation results from DB before scheduling + performance improvements #1051

Merged
merged 10 commits into from
Aug 21, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ public static int compareEndToEnd(final Interval x, final Interval y) {
return 0;
}

public static boolean hasSameStart(Interval x, Interval y){
return compareStartToStart(x,y) == 0;
}

public static boolean hasSameEnd(Interval x, Interval y){
return compareEndToEnd(x,y) == 0;
}

public static int compareStartToEnd(final Interval x, final Interval y) {
// First, order by absolute time.
if (!x.start.isEqualTo(y.end)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,18 @@ public Spans intoSpans(final Interval bounds) {
boolean boundsStartContained = false;
boolean boundsEndContained = false;
if(this.segments.size() == 1){
if (segments.get(0).interval().contains(bounds.start)) boundsStartContained = true;
if (segments.get(0).interval().contains(bounds.end)) boundsEndContained = true;
if (segments.get(0).interval().contains(bounds.start) ||
Interval.hasSameStart(segments.get(0).interval(), bounds)) boundsStartContained = true;
if (segments.get(0).interval().contains(bounds.end) ||
Interval.hasSameEnd(segments.get(0).interval(), bounds)) boundsEndContained = true;
}
for (int i = 0; i < this.segments.size() - 1; i++) {
final var leftInterval = this.segments.get(i).interval();
final var rightInterval = this.segments.get(i+1).interval();
if (leftInterval.contains(bounds.start) || rightInterval.contains(bounds.start)) boundsStartContained = true;
if (leftInterval.contains(bounds.end) || rightInterval.contains(bounds.end)) boundsEndContained = true;
if((leftInterval.contains(bounds.start) || rightInterval.contains(bounds.start)) ||
Interval.hasSameStart(leftInterval, bounds) || Interval.hasSameStart(rightInterval, bounds)) boundsStartContained = true;
if((leftInterval.contains(bounds.end) || rightInterval.contains(bounds.end)) ||
Interval.hasSameEnd(leftInterval, bounds) || Interval.hasSameEnd(rightInterval, bounds)) boundsEndContained = true;
if (leftInterval.isStrictlyBefore(bounds)) continue;
if (rightInterval.isStrictlyAfter(bounds)) continue;
if (!leftInterval.adjacent(rightInterval)) {
Expand Down
319 changes: 319 additions & 0 deletions e2e-tests/src/tests/scheduler-with-sim.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@

Check warning on line 1 in e2e-tests/src/tests/scheduler-with-sim.test.ts

View workflow job for this annotation

GitHub Actions / e2e-test

Slow Test

e2e-tests/src/tests/scheduler-with-sim.test.ts took 16s
import {expect, test} from "@playwright/test";
import req, {awaitScheduling, awaitSimulation} from "../utilities/requests.js";
import time from "../utilities/time.js";

/*
This is testing the check on Plan Revision before loading initial simulation results. We inject results associated with an old plan revision.
In these results, there is only one GrowBanana activity instead of the actual 2 present in the latest plan revision.
A coexistence goal attaching to GrowBanana activities shows that the scheduler did not use the stale sim results.
*/
test.describe.serial('Scheduling with initial sim results', () => {
const rd = Math.random() * 100;
const plan_start_timestamp = "2021-001T00:00:00.000";
const plan_end_timestamp = "2021-001T12:00:00.000";

test('Main', async ({ request }) => {
//upload bananation jar
const jar_id = await req.uploadJarFile(request);

const model: MissionModelInsertInput = {
jar_id,
mission: 'aerie_e2e_tests' + rd,
name: 'Banananation (e2e tests)' + rd,
version: '0.0.0' + rd,
};
const mission_model_id = await req.createMissionModel(request, model);
//delay for generation
await delay(2000);
const plan_input : CreatePlanInput = {
model_id : mission_model_id,
name : 'test_plan' + rd,
start_time : plan_start_timestamp,
duration : time.getIntervalFromDoyRange(plan_start_timestamp, plan_end_timestamp)
};
const plan_id = await req.createPlan(request, plan_input);

const firstGrowBananaToInsert : ActivityInsertInput =
{
//no arguments to ensure that the scheduler is getting effective arguments
arguments : {},
plan_id: plan_id,
type : "GrowBanana",
start_offset : "1h"
};

await req.insertActivity(request, firstGrowBananaToInsert);

await awaitSimulation(request, plan_id);

const secondGrowBananaToInsert : ActivityInsertInput =
{
//no arguments to ensure that the scheduler is getting effective arguments
arguments : {},
plan_id: plan_id,
type : "GrowBanana",
start_offset : "2h"
};

await req.insertActivity(request, secondGrowBananaToInsert);

const schedulingGoal1 : SchedulingGoalInsertInput =
{
description: "Test goal",
model_id: mission_model_id,
name: "ForEachGrowPeel"+rd,
definition: `export default function myGoal() {
return Goal.CoexistenceGoal({
forEach: ActivityExpression.ofType(ActivityType.GrowBanana),
activityTemplate: ActivityTemplates.BiteBanana({
biteSize: 1,
}),
startsAt:TimingConstraint.singleton(WindowProperty.END)
})
}`
};

const first_goal_id = await req.insertSchedulingGoal(request, schedulingGoal1);

let plan_revision = await req.getPlanRevision(request, plan_id);

const schedulingSpecification : SchedulingSpecInsertInput = {
// @ts-ignore
horizon_end: plan_end_timestamp,
horizon_start: plan_start_timestamp,
plan_id : plan_id,
plan_revision : plan_revision,
simulation_arguments : {},
analysis_only: false
}
const scheduling_specification_id = await req.insertSchedulingSpecification(request, schedulingSpecification);

const priority = 0;
const specGoal: SchedulingSpecGoalInsertInput = {
goal_id: first_goal_id,
priority: priority,
specification_id: scheduling_specification_id,
};
await req.createSchedulingSpecGoal(request, specGoal);

const { status, datasetId } = await awaitScheduling(request, scheduling_specification_id);

expect(status).toEqual("complete")
expect(datasetId).not.toBeNull();

const plan = await req.getPlan(request, plan_id)
expect(plan.activity_directives.length).toEqual(4);

//delete plan
const deleted_plan_id = await req.deletePlan(request, plan_id);
expect(deleted_plan_id).not.toBeNull();
expect(deleted_plan_id).toBeDefined();
expect(deleted_plan_id).toEqual(plan_id);

//delete mission model
const deleted_mission_model_id = await req.deleteMissionModel(request, mission_model_id)
expect(deleted_mission_model_id).not.toBeNull();
expect(deleted_mission_model_id).toBeDefined();
expect(deleted_mission_model_id).toEqual(mission_model_id);
});


/* In this test, we load simulation results with the current plan revision but with a different sim config. If the
injected results are picked up, the goal will not be satisfied and the number of activities will stay at its original value.
*/
test('Scheduling sim results 2', async ({ request }) => {
const rd = Math.random() * 100;
const plan_start_timestamp = "2021-001T00:00:00.000";
const plan_end_timestamp = "2021-001T12:00:00.000";

//upload bananation jar
const jar_id = await req.uploadJarFile(request);

const model: MissionModelInsertInput = {
jar_id,
mission: 'aerie_e2e_tests' + rd,
name: 'Banananation (e2e tests)' + rd,
version: '0.0.0' + rd,
};
const mission_model_id = await req.createMissionModel(request, model);
//delay for generation
await delay(2000);
const plan_input : CreatePlanInput = {
model_id : mission_model_id,
name : 'test_plan' + rd,
start_time : plan_start_timestamp,
duration : time.getIntervalFromDoyRange(plan_start_timestamp, plan_end_timestamp)
};
const plan_id = await req.createPlan(request, plan_input);

const simulation_id = await req.getSimulationId(request, plan_id);

const simulation_template : InsertSimulationTemplateInput = {
model_id: mission_model_id,
arguments: {
"initialPlantCount": 400,
},
description: 'Template for Plan ' +plan_id
};

await req.insertAndAssociateSimulationTemplate(request, simulation_template, simulation_id);

await awaitSimulation(request, plan_id);

const empty_simulation_template : InsertSimulationTemplateInput = {
model_id: mission_model_id,
arguments: {
},
description: 'Template for Plan ' +plan_id
};

await req.insertAndAssociateSimulationTemplate(request, empty_simulation_template, simulation_id);

const schedulingGoal1 : SchedulingGoalInsertInput =
{
description: "Test goal",
model_id: mission_model_id,
name: "ForEachPlanLessThan300"+rd,
definition: `export default () => Goal.CoexistenceGoal({
forEach: Real.Resource("/plant").lessThan(300),
activityTemplate: ActivityTemplates.GrowBanana({quantity: 10, growingDuration: Temporal.Duration.from({minutes:1}) }),
startsAt: TimingConstraint.singleton(WindowProperty.START)
})`
};

const first_goal_id = await req.insertSchedulingGoal(request, schedulingGoal1);

const plan_revision = await req.getPlanRevision(request, plan_id);

const schedulingSpecification : SchedulingSpecInsertInput = {
// @ts-ignore
horizon_end: plan_end_timestamp,
horizon_start: plan_start_timestamp,
plan_id : plan_id,
plan_revision : plan_revision,
simulation_arguments : {},
analysis_only: false
}
const specification_id = await req.insertSchedulingSpecification(request, schedulingSpecification);

const priority = 0;
const specGoal: SchedulingSpecGoalInsertInput = {
goal_id: first_goal_id,
priority: priority,
specification_id: specification_id,
};
await req.createSchedulingSpecGoal(request, specGoal);

await awaitScheduling(request, specification_id);

const plan = await req.getPlan(request, plan_id)
expect(plan.activity_directives.length).toEqual(1);

//delete plan
const deleted_plan_id = await req.deletePlan(request, plan_id);
expect(deleted_plan_id).not.toBeNull();
expect(deleted_plan_id).toBeDefined();
expect(deleted_plan_id).toEqual(plan_id);

//delete mission model
const deleted_mission_model_id = await req.deleteMissionModel(request, mission_model_id)
expect(deleted_mission_model_id).not.toBeNull();
expect(deleted_mission_model_id).toBeDefined();
expect(deleted_mission_model_id).toEqual(mission_model_id);
});

/* In this test, we inject simulation results to test that the scheduler is loading them properly. If they are picked up,
there should be no activity in the plan (plant>300). Otherwise, there should be one activity.
*/
test('Scheduling sim results 3', async ({ request }) => {
const rd = Math.random() * 100;
const plan_start_timestamp = "2021-001T00:00:00.000";
const plan_end_timestamp = "2021-001T12:00:00.000";
//upload bananation jar
const jar_id = await req.uploadJarFile(request);

const model: MissionModelInsertInput = {
jar_id,
mission: 'aerie_e2e_tests' + rd,
name: 'Banananation (e2e tests)' + rd,
version: '0.0.0' + rd,
};
const mission_model_id = await req.createMissionModel(request, model);
//delay for generation
await delay(2000);
const plan_input : CreatePlanInput = {
model_id : mission_model_id,
name : 'test_plan' + rd,
start_time : plan_start_timestamp,
duration : time.getIntervalFromDoyRange(plan_start_timestamp, plan_end_timestamp)
};
const plan_id = await req.createPlan(request, plan_input);

const simId = await req.getSimulationId(request, plan_id);
let plan_revision = await req.getPlanRevision(request, plan_id);

const datasetId = await req.insertSimulationDataset(
request,
simId,
plan_start_timestamp,
plan_end_timestamp,
"success",
{},
plan_revision);

const profileId = await req.insertProfile(request, datasetId, "12h", "/plant", { "type": "discrete", "schema": { "type": "int" } });
await req.insertProfileSegment(request, datasetId, 400, false, profileId, "0h");

const schedulingGoal1 : SchedulingGoalInsertInput =
{
description: "Test goal",
model_id: mission_model_id,
name: "ForEachPlanLessThan300"+rd,
definition: `export default () => Goal.CoexistenceGoal({
forEach: Real.Resource("/plant").lessThan(300),
activityTemplate: ActivityTemplates.GrowBanana({quantity: 10, growingDuration: Temporal.Duration.from({minutes:1}) }),
startsAt: TimingConstraint.singleton(WindowProperty.START)
})`
};

const first_goal_id = await req.insertSchedulingGoal(request, schedulingGoal1);

plan_revision = await req.getPlanRevision(request, plan_id);

const schedulingSpecification : SchedulingSpecInsertInput = {
// @ts-ignore
horizon_end: plan_end_timestamp,
horizon_start: plan_start_timestamp,
plan_id : plan_id,
plan_revision : plan_revision,
simulation_arguments : {},
analysis_only: false
}
const specification_id = await req.insertSchedulingSpecification(request, schedulingSpecification);

const priority = 0;
const specGoal: SchedulingSpecGoalInsertInput = {
goal_id: first_goal_id,
priority: priority,
specification_id: specification_id,
};
await req.createSchedulingSpecGoal(request, specGoal);

await awaitScheduling(request, specification_id);

const plan = await req.getPlan(request, plan_id)
expect(plan.activity_directives.length).toEqual(0);

//delete plan
await req.deletePlan(request, plan_id);

//delete mission model
await req.deleteMissionModel(request, mission_model_id)
});
});


function delay(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
Loading
Loading