Skip to content

Commit

Permalink
Shift bounds to avoid unnecessary gaps in profile evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jun 28, 2023
1 parent c06f43c commit f5280f3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ActivityWindow(final String activityAlias) {
public Windows evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(Interval.FOREVER, false),
Segment.of(bounds, false),
Segment.of(activity.interval, true)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DiscreteParameter(final String activityAlias, final String parameterName)
public DiscreteProfile evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new DiscreteProfile(
Segment.of(Interval.FOREVER, activity.parameters.get(this.parameterName))
Segment.of(bounds, activity.parameters.get(this.parameterName))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public record DiscreteProfileFromDuration(
@Override
public DiscreteProfile evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final Duration duration = this.duration.evaluate(results, bounds, environment);
return new DiscreteProfile(Segment.of(Interval.FOREVER, SerializedValue.of(duration.in(Duration.MICROSECOND))));
return new DiscreteProfile(Segment.of(bounds, SerializedValue.of(duration.in(Duration.MICROSECOND))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public EndOf(final String activityAlias) {
public Windows evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(Interval.FOREVER, false),
Segment.of(bounds, false),
Segment.of(Interval.at(activity.interval.end), true)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public record ShiftBy<P extends Profile<P>>(

@Override
public P evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var originalProfile = this.expression.evaluate(results, bounds, environment);
// bounds aren't shifted here because duration expressions don't care about them; durations don't exist on the timeline.
final var duration = this.duration.evaluate(results, bounds, environment);

final var shiftedBounds = bounds.shiftBy(Duration.negate(duration));
final var originalProfile = this.expression.evaluate(results, shiftedBounds, environment);

return originalProfile.shiftBy(duration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ public ShiftWindowsEdges(final Expression<Windows> left, final Expression<Durati

@Override
public Windows evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var windows = this.windows.evaluate(results, bounds, environment);
return windows.shiftEdges(this.fromStart.evaluate(results, bounds, environment), this.fromEnd.evaluate(results, bounds, environment));
final var shiftRising = this.fromStart.evaluate(results, bounds, environment);
final var shiftFalling = this.fromEnd.evaluate(results, bounds, environment);

final var newBounds = Interval.between(
Duration.min(bounds.start.minus(shiftRising), bounds.start.minus(shiftFalling)),
bounds.startInclusivity,
Duration.max(bounds.end.minus(shiftRising), bounds.end.minus(shiftFalling)),
bounds.endInclusivity
);

final var windows = this.windows.evaluate(results, newBounds, environment);
return windows.shiftEdges(shiftRising, shiftFalling).select(bounds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public StartOf(final String activityAlias) {
public Windows evaluate(final SimulationResults results, final Interval bounds, final EvaluationEnvironment environment) {
final var activity = environment.activityInstances().get(this.activityAlias);
return new Windows(
Segment.of(Interval.FOREVER, false),
Segment.of(bounds, false),
Segment.of(Interval.at(activity.interval.start), true)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ public void testStartOf() {
final var result = new StartOf("act").evaluate(simResults, environment);

final var expected = new Windows(
Segment.of(FOREVER, false),
Segment.of(simResults.bounds, false),
Segment.of(at(4, SECONDS), true)
);

Expand Down Expand Up @@ -878,7 +878,7 @@ public void testEndOf() {
final var result = new EndOf("act").evaluate(simResults, environment);

final var expected = new Windows(
Segment.of(FOREVER, false),
Segment.of(simResults.bounds, false),
Segment.of(at(8, SECONDS), true)
);

Expand Down

0 comments on commit f5280f3

Please sign in to comment.