-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ec3162
commit 4a16914
Showing
3 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...n/java/gov/nasa/ammos/aerie/procedural/examples/fooprocedures/procedures/StayWellFed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package gov.nasa.ammos.aerie.procedural.examples.fooprocedures.procedures; | ||
|
||
import gov.nasa.ammos.aerie.procedural.scheduling.Rule; | ||
import gov.nasa.ammos.aerie.procedural.scheduling.annotations.SchedulingProcedure; | ||
import gov.nasa.ammos.aerie.procedural.scheduling.plan.EditablePlan; | ||
import gov.nasa.ammos.aerie.procedural.timeline.Interval; | ||
import gov.nasa.ammos.aerie.procedural.timeline.collections.profiles.Real; | ||
import gov.nasa.ammos.aerie.procedural.timeline.collections.profiles.Strings; | ||
import gov.nasa.ammos.aerie.procedural.timeline.payloads.LinearEquation; | ||
import gov.nasa.ammos.aerie.procedural.timeline.payloads.Segment; | ||
import gov.nasa.ammos.aerie.procedural.timeline.payloads.activities.DirectiveStart; | ||
import gov.nasa.jpl.aerie.merlin.protocol.types.Duration; | ||
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@SchedulingProcedure | ||
public record StayWellFed(double bitePeriodHours) implements Rule { | ||
@Override | ||
public void run(@NotNull final EditablePlan plan) { | ||
final var bitePeriod = Duration.hours(bitePeriodHours); | ||
var simResults = plan.latestResults(); | ||
if (simResults == null) simResults = plan.simulate(); | ||
|
||
// I'm using producer as a substitute for a mission phase variable. | ||
// This goal will only apply during the Dole mission phase. :) | ||
final var dolePhase = simResults | ||
.resource("/producer", Strings::deserialize) | ||
.highlightEqualTo("Dole"); | ||
dolePhase.cache(); | ||
|
||
// Manual recurrence goal: during Dole phase, require a bitebanana every `bitePeriod`. | ||
final var bites = plan.directives("BiteBanana") | ||
.filterByWindows(dolePhase, false) | ||
.collect(); | ||
|
||
var currentTime = Duration.MIN_VALUE; | ||
for (final var phase: dolePhase.collect()) { | ||
currentTime = Duration.max(currentTime, phase.start); | ||
|
||
while (currentTime.plus(bitePeriod).shorterThan(phase.end)) { | ||
var nextExistingBiteTime = bites.isEmpty() ? Duration.MAX_VALUE : bites.getFirst().getStartTime(); | ||
while (nextExistingBiteTime.minus(currentTime).noLongerThan(bitePeriod)) { | ||
currentTime = Duration.max(currentTime, nextExistingBiteTime); | ||
bites.removeFirst(); | ||
|
||
nextExistingBiteTime = bites.isEmpty() ? Duration.MAX_VALUE : bites.getFirst().getStartTime(); | ||
} | ||
while (nextExistingBiteTime.minus(currentTime).longerThan(bitePeriod) && phase.contains(currentTime.plus(bitePeriod))) { | ||
currentTime = currentTime.plus(bitePeriod); | ||
plan.create( | ||
"BiteBanana", | ||
new DirectiveStart.Absolute(currentTime), | ||
Map.of("biteSize", SerializedValue.of(1)) | ||
); | ||
} | ||
} | ||
} | ||
|
||
plan.commit(); | ||
simResults = plan.simulate(); | ||
|
||
final var newBites = plan.directives("BiteBanana") | ||
.filterByWindows(dolePhase, false); | ||
|
||
// All this banana biting made us run out of bananas. | ||
// So we iteratively find the first time /fruit drops below zero | ||
// and add a grow banana fix it. We then mock the effect of grow banana | ||
// by adding one to /fruit, rather than resimulating, and do it again. | ||
var fruit = simResults.resource("/fruit", Real::deserialize); | ||
fruit.cache(); | ||
|
||
var ranOutAt = fruit.lessThan(0).filterByWindows(dolePhase, true).risingEdges().highlightTrue().collect(); | ||
while (!ranOutAt.isEmpty()) { | ||
final var problemStart = ranOutAt.getFirst().start; | ||
final var growStart = problemStart.minus(Duration.HOUR); | ||
final var activeBites = newBites.collect(Interval.at(problemStart)); | ||
|
||
final var currentFruit = fruit.sample(problemStart); | ||
final var pastFruit = fruit.sample(growStart); | ||
|
||
plan.create( | ||
"GrowBanana", | ||
// activeBites.isEmpty() ? new DirectiveStart.Absolute(growStart) | ||
// : new DirectiveStart.Anchor(activeBites.getFirst().id, Duration.HOUR.negate(), DirectiveStart.Anchor.AnchorPoint.Start), | ||
new DirectiveStart.Absolute(growStart), | ||
Map.of( | ||
"growingDuration", SerializedValue.of(Duration.HOUR.micros()), | ||
"quantity", SerializedValue.of(pastFruit - currentFruit) | ||
) | ||
); | ||
|
||
fruit = fruit.plus( | ||
Real.step(growStart, pastFruit - currentFruit) | ||
.set(new Real(List.of( | ||
new Segment<>(Interval.between(growStart, problemStart), new LinearEquation(growStart, 0.0, (pastFruit - currentFruit) / (Duration.HOUR.in(Duration.SECONDS)))) | ||
))) | ||
); | ||
|
||
ranOutAt = fruit.lessThan(0).filterByWindows(dolePhase, true).risingEdges().highlightTrue().collect(); | ||
} | ||
|
||
plan.commit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters