Skip to content

Commit

Permalink
Fix memory consumption when multiple models are used
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkschumacher committed May 16, 2024
1 parent 18809e8 commit fec3ea7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
19 changes: 7 additions & 12 deletions solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ func NewSolution(

random := rand.New(rand.NewSource(m.Random().Int63()))

maxExpressionIndex := -1
for _, expression := range model.expressions {
if expression.Index() > maxExpressionIndex {
maxExpressionIndex = expression.Index()
}
}
nExpressions := len(model.expressions)

solution := &solutionImpl{
model: m,
Expand All @@ -143,8 +138,8 @@ func NewSolution(
slack: make([]float64, 0, nrStops),
start: make([]float64, 0, nrStops),
end: make([]float64, 0, nrStops),
values: make([][]float64, maxExpressionIndex+1),
cumulativeValues: make([][]float64, maxExpressionIndex+1),
values: make(map[int][]float64, nExpressions),
cumulativeValues: make(map[int][]float64, nExpressions),
stopToPlanUnit: make([]*solutionPlanStopsUnitImpl, nrStops),
constraintStopData: make(map[ModelConstraint][]Copier),
objectiveStopData: make(map[ModelObjective][]Copier),
Expand Down Expand Up @@ -579,12 +574,12 @@ func (s *solutionImpl) addInitialSolution(m Model) error {
type solutionImpl struct {
model Model
scores map[ModelObjective]float64
values [][]float64
values map[int][]float64
objectiveStopData map[ModelObjective][]Copier
constraintStopData map[ModelConstraint][]Copier
objectiveSolutionData map[ModelObjective]Copier
constraintSolutionData map[ModelConstraint]Copier
cumulativeValues [][]float64
cumulativeValues map[int][]float64

// TODO: explore if stopToPlanUnit should rather contain interfaces
stopToPlanUnit []*solutionPlanStopsUnitImpl
Expand Down Expand Up @@ -707,7 +702,7 @@ func (s *solutionImpl) Copy() Solution {
cumulativeTravelDuration: slices.Clone(
s.cumulativeTravelDuration,
),
cumulativeValues: make([][]float64, len(s.cumulativeValues)),
cumulativeValues: make(map[int][]float64, len(s.cumulativeValues)),
stopToPlanUnit: make([]*solutionPlanStopsUnitImpl, len(s.stopToPlanUnit)),
end: slices.Clone(s.end),
first: slices.Clone(s.first),
Expand All @@ -719,7 +714,7 @@ func (s *solutionImpl) Copy() Solution {
start: slices.Clone(s.start),
stop: slices.Clone(s.stop),
stopPosition: slices.Clone(s.stopPosition),
values: make([][]float64, len(s.values)),
values: make(map[int][]float64, len(s.values)),
vehicleIndices: slices.Clone(s.vehicleIndices),
random: random,
fixedPlanUnits: newSolutionPlanUnitCollectionBaseImpl(
Expand Down
41 changes: 41 additions & 0 deletions solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// © 2019-present nextmv.io inc

package nextroute_test

import (
"testing"
"time"

"github.com/nextmv-io/nextroute"
)

func BenchmarkAllocationsSolution(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
model, err := createModel(singleVehiclePlanSequenceModel())
if err != nil {
b.Error(err)
}

maximum := nextroute.NewVehicleTypeDurationExpression(
"maximum duration",
3*time.Minute,
)
expression := nextroute.NewStopExpression("test", 2.0)

cnstr, err := nextroute.NewMaximum(expression, maximum)
if err != nil {
b.Error(err)
}

err = model.AddConstraint(cnstr)
if err != nil {
b.Error(err)
}
b.StartTimer()
_, err = nextroute.NewSolution(model)
if err != nil {
b.Fatal(err)
}
}
}

0 comments on commit fec3ea7

Please sign in to comment.