Skip to content

Commit

Permalink
Fix plan duplication bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Sep 5, 2024
1 parent 71ed927 commit ee3b359
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 46 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
import gov.nasa.jpl.aerie.scheduler.simulation.SimulationFacade;
import gov.nasa.jpl.aerie.scheduler.solver.ConflictSatisfaction;
import gov.nasa.jpl.aerie.scheduler.solver.Evaluation;
import org.apache.commons.lang3.NotImplementedException;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static gov.nasa.jpl.aerie.scheduler.plan.InMemoryEditablePlan.toSchedulingActivityDirective;
import static gov.nasa.jpl.aerie.scheduler.plan.InMemoryEditablePlan.toSchedulingActivity;

public class Procedure extends Goal {
private final Path jarPath;
Expand Down Expand Up @@ -58,24 +57,14 @@ public void run(Evaluation eval, Plan plan, MissionModel<?> missionModel, Functi
lookupActivityType::apply
);

/*
TODO
Comments from Joel:
- Part of the intent of editablePlan was to be able to re-use it across procedures.
- Could be done by initializing EditablePlanImpl with simulation results
Duration construction and arithmetic can be less awkward
*/

procedureMapper.deserialize(this.args).run(editablePlan);

if (!editablePlan.getUncommittedChanges().isEmpty()) {
throw new IllegalStateException("procedural goal %s had changes that were not committed or rolled back".formatted(jarPath.getFileName()));
}
for (final var edit : editablePlan.getTotalDiff()) {
if (edit instanceof Edit.Create c) {
newActivities.add(toSchedulingActivityDirective(c.getDirective(), lookupActivityType::apply, true));
newActivities.add(toSchedulingActivity(c.getDirective(), lookupActivityType::apply, true));
} else {
throw new IllegalStateException("Unexpected value: " + edit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public MissionModel<?> getMissionModel() {
* @return the initial seed plan that schedulers may start from
*/
public Plan getInitialPlan() {
return initialPlan;
return initialPlan.duplicate();
}

/**
Expand All @@ -148,7 +148,7 @@ public void setInitialPlan(
) {
initialPlan = plan;
this.initialSimulationResults = initialSimulationResults.map(simulationResults -> new SimulationData(
plan,
getInitialPlan(),
simulationResults,
SimulationResultsConverter.convertToConstraintModelResults(simulationResults)
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,8 @@ public SimulationData simulateWithResults(
) throws SimulationException, SchedulingInterruptedException {
if (this.initialSimulationResults != null) {
final var inputPlan = scheduleFromPlan(plan, schedulerModel);
final HashMap<ActivityDirectiveId, ActivityDirective> planActuallySimulated = new HashMap<>();
final var initialPlan = this.initialSimulationResults.plan().getActivitiesById();
var allActivitiesFound = true;
for (final var activityInstance: this.initialSimulationResults.constraintsResults().activities) {
if (activityInstance.directiveId().isPresent()) {
final var directiveId = activityInstance.directiveId().get();
final var directive = initialPlan.get(directiveId);
if (directive != null) {
planActuallySimulated.put(activityInstance.directiveId().get(), schedulingActToActivityDir(directive, schedulerModel));
} else {
allActivitiesFound = false;
}
}
}
if (allActivitiesFound && inputPlan.equals(new PlanSimCorrespondence(planActuallySimulated))) {
return initialSimulationResults;
}
final var initialPlan = scheduleFromPlan(this.initialSimulationResults.plan(), schedulerModel);
if (inputPlan.equals(initialPlan)) return initialSimulationResults;
}
final var resultsInput = simulateNoResults(plan, until);
final var driverResults = resultsInput.simulationResultsComputerInputs().computeResults(resourceNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ data class InMemoryEditablePlan(
val resolved = directive.resolve(id, parent)
uncommittedChanges.add(Edit.Create(resolved))
resolved.validateArguments(lookupActivityType)
plan.add(resolved.toSchedulingActivityDirective(lookupActivityType, true))
plan.add(resolved.toSchedulingActivity(lookupActivityType, true))
return id
}

Expand All @@ -70,7 +70,7 @@ data class InMemoryEditablePlan(
for (edit in result) {
when (edit) {
is Edit.Create -> {
plan.remove(edit.directive.toSchedulingActivityDirective(lookupActivityType, true))
plan.remove(edit.directive.toSchedulingActivity(lookupActivityType, true))
}
}
}
Expand All @@ -93,7 +93,7 @@ data class InMemoryEditablePlan(
lookupActivityType(type).specType.inputType.validateArguments(inner.arguments)
}

@JvmStatic fun Directive<AnyDirective>.toSchedulingActivityDirective(lookupActivityType: (String) -> ActivityType, isNew: Boolean) = SchedulingActivity(
@JvmStatic fun Directive<AnyDirective>.toSchedulingActivity(lookupActivityType: (String) -> ActivityType, isNew: Boolean) = SchedulingActivity(
id,
lookupActivityType(type),
when (val s = start) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package gov.nasa.jpl.aerie.scheduler.plan

import gov.nasa.jpl.aerie.merlin.protocol.types.Duration
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue
import gov.nasa.jpl.aerie.scheduler.model.ActivityType
import gov.nasa.jpl.aerie.scheduler.model.PlanningHorizon
import gov.nasa.ammos.aerie.procedural.timeline.Interval
import gov.nasa.ammos.aerie.procedural.timeline.collections.Directives
import gov.nasa.ammos.aerie.procedural.timeline.util.duration.minus
import gov.nasa.ammos.aerie.procedural.timeline.util.duration.plus
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.Directive
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart.Anchor.AnchorPoint.Companion.anchorToStart
import gov.nasa.ammos.aerie.procedural.timeline.util.duration.minus
import gov.nasa.ammos.aerie.procedural.timeline.util.duration.plus
import gov.nasa.jpl.aerie.merlin.protocol.types.Duration
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue
import gov.nasa.jpl.aerie.scheduler.model.PlanningHorizon
import java.time.Instant
import gov.nasa.jpl.aerie.scheduler.model.Plan as SchedulerPlan
import gov.nasa.ammos.aerie.procedural.timeline.plan.Plan as TimelinePlan
import gov.nasa.jpl.aerie.scheduler.model.Plan as SchedulerPlan

data class SchedulerToProcedurePlanAdapter(
private val schedulerPlan: SchedulerPlan,
Expand Down

0 comments on commit ee3b359

Please sign in to comment.