Skip to content

Commit

Permalink
Merge pull request #58 from nextmv-io/feature/eng-5116-maximumconstra…
Browse files Browse the repository at this point in the history
…int-efficiency-improvement

Maximum constraint/objective efficiency improvement
  • Loading branch information
davidrijsman authored May 21, 2024
2 parents 206bf97 + e11114d commit 6da4cc8
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions model_maximum.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type maximumImpl struct {
resourceExpression ModelExpression
maximumByVehicleType []float64
penaltyOffset float64
hasNoEffect []bool
}

func (l *maximumImpl) PenaltyOffset() float64 {
Expand Down Expand Up @@ -107,18 +108,28 @@ func (l *maximumImpl) Lock(model Model) error {
)
}

planUnits := model.PlanStopsUnits()

l.hasNoEffect = make([]bool, len(planUnits))

if !l.hasStopExpressionAndNoNegativeValues {
return nil
}

planUnits := model.PlanStopsUnits()
l.deltas = make([]float64, len(planUnits))
for _, planUnit := range model.PlanStopsUnits() {

for _, planUnit := range planUnits {
delta := 0.0
hasNoEffect := true
for _, stop := range planUnit.Stops() {
delta += l.Expression().Value(nil, nil, stop)
value := l.Expression().Value(nil, nil, stop)
delta += value
if value != 0 {
hasNoEffect = false
}
}
l.deltas[planUnit.Index()] = delta
l.hasNoEffect[planUnit.Index()] = hasNoEffect
}

return nil
Expand Down Expand Up @@ -180,14 +191,18 @@ func (l *maximumImpl) DoesStopHaveViolations(s SolutionStop) bool {
func (l *maximumImpl) EstimateIsViolated(
move SolutionMoveStops,
) (isViolated bool, stopPositionsHint StopPositionsHint) {
moveImpl := move.(*solutionMoveStopsImpl)

if l.hasNoEffect[moveImpl.planUnit.modelPlanStopsUnit.Index()] {
return false, constNoPositionsHint
}

// All contributions to the level are negative, no need to check
// it will always be below the implied minimum level of zero.
if l.hasNegativeValues && !l.hasPositiveValues {
return true, constSkipVehiclePositionsHint
}

moveImpl := move.(*solutionMoveStopsImpl)

vehicle := moveImpl.vehicle()
vehicleType := vehicle.ModelVehicle().VehicleType()

Expand Down Expand Up @@ -302,6 +317,10 @@ func (l *maximumImpl) EstimateDeltaValue(
) (deltaValue float64) {
moveImpl := move.(*solutionMoveStopsImpl)

if l.hasNoEffect[moveImpl.planUnit.modelPlanStopsUnit.Index()] {
return 0.0
}

vehicle := moveImpl.vehicle()

hasViolation := vehicle.Last().ObjectiveData(l).(*maximumObjectiveDate).hasViolation
Expand Down

0 comments on commit 6da4cc8

Please sign in to comment.