Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SolutionVehicle iface to struct #53

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions model_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ func (l *clusterImpl) estimateDeltaScore(

var c *centroidData
if asConstraint {
c = vehicle.last().ConstraintData(l).(*centroidData)
c = vehicle.Last().ConstraintData(l).(*centroidData)
} else {
c = vehicle.last().ObjectiveData(l).(*centroidData)
c = vehicle.Last().ObjectiveData(l).(*centroidData)
}

centroid := c.location
Expand All @@ -249,7 +249,7 @@ func (l *clusterImpl) estimateDeltaScore(
continue
}
centroidOtherVehicle := otherVehicle.
last().
Last().
ConstraintData(l).(*centroidData).location

if haversineDistance(
Expand Down Expand Up @@ -285,12 +285,11 @@ func (l *clusterImpl) getSolutionStops(vehicle SolutionVehicle) []SolutionStop {
}
func (l *clusterImpl) Value(solutionStop Solution) float64 {
sum := 0.0
for _, v := range solutionStop.(*solutionImpl).vehiclesMutable() {
vehicle := v.(solutionVehicleImpl)
for _, vehicle := range solutionStop.(*solutionImpl).vehiclesMutable() {
if vehicle.IsEmpty() {
continue
}
sum += vehicle.last().ObjectiveData(l).(*centroidData).compactness
sum += vehicle.Last().ObjectiveData(l).(*centroidData).compactness
}
return sum
}
2 changes: 1 addition & 1 deletion model_constraint_maximum_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (l *maximumDurationConstraintImpl) EstimateIsViolated(

maximumValue := l.maximum.Value(vehicleType, nil, nil)

startValue := vehicle.first().StartValue()
startValue := vehicle.First().StartValue()
previous, _ := moveImpl.previous()
endValue := previous.EndValue()

Expand Down
5 changes: 2 additions & 3 deletions model_constraint_maximum_travel_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ func (l *maximumTravelDurationConstraintImpl) EstimateIsViolated(
}

func (l *maximumTravelDurationConstraintImpl) DoesVehicleHaveViolations(vehicle SolutionVehicle) bool {
vehicleImpl := vehicle.(solutionVehicleImpl)
return vehicleImpl.last().CumulativeTravelDurationValue() >
l.maximum.Value(vehicleImpl.ModelVehicle().VehicleType(), nil, nil)
return vehicle.Last().CumulativeTravelDurationValue() >
l.maximum.Value(vehicle.ModelVehicle().VehicleType(), nil, nil)
}

func (l *maximumTravelDurationConstraintImpl) IsTemporal() bool {
Expand Down
6 changes: 3 additions & 3 deletions model_latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func (l *latestImpl) Value(s Solution) float64 {
solution := s.(*solutionImpl)
value := 0.0
for _, vehicle := range solution.vehicles {
solutionStop := vehicle.first().Next()
lastSolutionStop := vehicle.last()
solutionStop := vehicle.First().Next()
lastSolutionStop := vehicle.Last()
for {
latenessFactor := l.latenessFactor.Value(
nil,
Expand Down Expand Up @@ -241,7 +241,7 @@ func (l *latestImpl) estimateDeltaScore(
first := true

arrival, start, end := 0.0, 0.0, 0.0
previousStop := vehicle.first().ModelStop()
previousStop := vehicle.First().ModelStop()
generator := newSolutionStopGenerator(*move, false, true)
defer generator.release()

Expand Down
10 changes: 5 additions & 5 deletions model_maximum.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (l *maximumImpl) EstimateIsViolated(
// level at the end of the vehicle. We can only do this if the expression
// is a stop expression.
if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.last().CumulativeValue(expression)
cumulativeValue := vehicle.Last().CumulativeValue(expression)

if cumulativeValue+l.deltas[moveImpl.planUnit.modelPlanStopsUnit.Index()] > maximum {
return true, constSkipVehiclePositionsHint
Expand Down Expand Up @@ -241,7 +241,7 @@ func (l *maximumImpl) EstimateIsViolated(

if !l.hasNegativeValues {
violated := level-previousStop.CumulativeValue(l.Expression())+
vehicle.last().CumulativeValue(l.Expression()) > maximum
vehicle.Last().CumulativeValue(l.Expression()) > maximum
return violated, constNoPositionsHint
}

Expand Down Expand Up @@ -304,7 +304,7 @@ func (l *maximumImpl) EstimateDeltaValue(

vehicle := moveImpl.vehicle()

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

vehicleType := vehicle.ModelVehicle().VehicleType()
maximum := l.maximumByVehicleType[vehicleType.Index()]
Expand All @@ -325,7 +325,7 @@ func (l *maximumImpl) EstimateDeltaValue(
// level at the end of the vehicle. We can only do this if the expression
// is a stop expression.
if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.last().CumulativeValue(l.resourceExpression)
cumulativeValue := vehicle.Last().CumulativeValue(l.resourceExpression)

returnValue := 0.0
excess := cumulativeValue + l.deltas[moveImpl.planUnit.modelPlanStopsUnit.Index()] - maximum
Expand Down Expand Up @@ -394,7 +394,7 @@ func (l *maximumImpl) Value(
maximum := l.maximumByVehicleType[vehicleType.Index()]

if l.hasStopExpressionAndNoNegativeValues {
cumulativeValue := vehicle.last().CumulativeValue(l.resourceExpression)
cumulativeValue := vehicle.Last().CumulativeValue(l.resourceExpression)
excess := cumulativeValue - maximum
if excess > 0 {
score += excess
Expand Down
2 changes: 1 addition & 1 deletion model_objective_earliness.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (l *earlinessObjectiveImpl) EstimateDeltaValue(
// Init data
first := true
arrival, start, end := 0.0, 0.0, 0.0
previousStop := vehicle.first()
previousStop := vehicle.First()

// Get sequence starting with the first stop prior to the first stop to be
// inserted.
Expand Down
2 changes: 1 addition & 1 deletion model_objective_travelduration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (t *travelDurationObjectiveImpl) Value(solution Solution) float64 {

score := 0.0
for _, vehicle := range solutionImp.vehicles {
score += vehicle.last().CumulativeTravelDurationValue()
score += vehicle.Last().CumulativeTravelDurationValue()
}
return score
}
Expand Down
6 changes: 3 additions & 3 deletions model_objective_vehicles_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (t *vehiclesDurationObjectiveImpl) EstimateDeltaValue(

first := true
end := 0.0
previousStop := vehicle.first()
previousStop := vehicle.First()

generator := newSolutionStopGenerator(*solutionMoveStops, false, isDependentOnTime)
defer generator.release()
Expand All @@ -83,7 +83,7 @@ func (t *vehiclesDurationObjectiveImpl) EstimateDeltaValue(
nextmove, _ := solutionMoveStops.next()

if nextmove.IsLast() || isDependentOnTime {
return end - vehicle.last().EndValue()
return end - vehicle.Last().EndValue()
}

for solutionStop := nextmove.Next(); !solutionStop.IsLast(); solutionStop = solutionStop.Next() {
Expand All @@ -99,7 +99,7 @@ func (t *vehiclesDurationObjectiveImpl) EstimateDeltaValue(
}
}

last := vehicle.last()
last := vehicle.Last()
_, _, _, end = vehicleType.TemporalValues(
end,
last.Previous().ModelStop(),
Expand Down
28 changes: 14 additions & 14 deletions solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func NewSolution(
model: m,

vehicleIndices: make([]int, 0, nrVehicles),
vehicles: make([]solutionVehicleImpl, 0, nrVehicles),
vehicles: make([]SolutionVehicle, 0, nrVehicles),
solutionVehicles: make([]SolutionVehicle, 0, nrVehicles),
first: make([]int, 0, nrVehicles),
last: make([]int, 0, nrVehicles),
Expand Down Expand Up @@ -395,7 +395,7 @@ func (s *solutionImpl) addInitialSolution(m Model) error {
PlanUnitLoop:
for _, planUnit := range planUnits {
stopPositions := make(StopPositions, 0, len(planUnit.SolutionStops()))
previousStop := solutionVehicle.first()
previousStop := solutionVehicle.First()

solutionPlanUnit := s.unwrapRootPlanUnit(planUnit)
allPlanUnits[solutionPlanUnit] = true
Expand Down Expand Up @@ -455,7 +455,7 @@ func (s *solutionImpl) addInitialSolution(m Model) error {
newStopPosition(
previousStop,
solutionStop,
solutionVehicle.last(),
solutionVehicle.Last(),
),
)
}
Expand Down Expand Up @@ -594,7 +594,7 @@ type solutionImpl struct {
vehicleIndices []int

// TODO: explore if vehicles should rather be interfaces, then we can avoid creating new vehicles on the fly
vehicles []solutionVehicleImpl
vehicles []SolutionVehicle
solutionVehicles []SolutionVehicle
start []float64
slack []float64
Expand Down Expand Up @@ -664,17 +664,17 @@ func (s *solutionImpl) SolutionVehicle(vehicle ModelVehicle) SolutionVehicle {
if solutionVehicle, ok := s.solutionVehicle(vehicle); ok {
return solutionVehicle
}
return nil
return SolutionVehicle{}
}

func (s *solutionImpl) solutionVehicle(vehicle ModelVehicle) (solutionVehicleImpl, bool) {
func (s *solutionImpl) solutionVehicle(vehicle ModelVehicle) (SolutionVehicle, bool) {
if vehicle != nil {
return solutionVehicleImpl{
return SolutionVehicle{
index: vehicle.Index(),
solution: s,
}, true
}
return solutionVehicleImpl{}, false
return SolutionVehicle{}, false
}

func (s *solutionImpl) Copy() Solution {
Expand Down Expand Up @@ -834,7 +834,7 @@ func (s *solutionImpl) newVehicle(
modelVehicle ModelVehicle,
) (SolutionVehicle, error) {
if modelVehicle == nil {
return nil, fmt.Errorf("modelVehicle is nil")
return SolutionVehicle{}, fmt.Errorf("modelVehicle is nil")
}

model := s.model.(*modelImpl)
Expand All @@ -858,11 +858,11 @@ func (s *solutionImpl) newVehicle(
modelVehicle.Last().Index(),
)
s.vehicleIndices = append(s.vehicleIndices, modelVehicle.Index())
s.vehicles = append(s.vehicles, solutionVehicleImpl{
s.vehicles = append(s.vehicles, SolutionVehicle{
index: modelVehicle.Index(),
solution: s,
})
s.solutionVehicles = append(s.solutionVehicles, solutionVehicleImpl{
s.solutionVehicles = append(s.solutionVehicles, SolutionVehicle{
index: modelVehicle.Index(),
solution: s,
})
Expand Down Expand Up @@ -903,10 +903,10 @@ func (s *solutionImpl) newVehicle(

constraint, _, err := s.isFeasible(len(s.stop)-2, true)
if err != nil {
return nil, err
return SolutionVehicle{}, err
}
if constraint != nil {
return nil, fmt.Errorf("failed creating new vehicle: %v", constraint)
return SolutionVehicle{}, fmt.Errorf("failed creating new vehicle: %v", constraint)
}

return toSolutionVehicle(s, len(s.vehicles)-1), nil
Expand Down Expand Up @@ -1083,7 +1083,7 @@ func (s *solutionImpl) BestMove(ctx context.Context, planUnit SolutionPlanUnit)
return bestMove
}

solutionVehicle := solutionVehicleImpl{
solutionVehicle := SolutionVehicle{
index: -1,
solution: s,
}
Expand Down
4 changes: 2 additions & 2 deletions solution_move_stops.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func (m *solutionMoveStopsImpl) Solution() Solution {

func (m *solutionMoveStopsImpl) Vehicle() SolutionVehicle {
if len(m.stopPositions) == 0 {
return nil
return SolutionVehicle{}
}
return m.stopPositions[len(m.stopPositions)-1].next().Vehicle()
}

func (m *solutionMoveStopsImpl) vehicle() solutionVehicleImpl {
func (m *solutionMoveStopsImpl) vehicle() SolutionVehicle {
return m.stopPositions[len(m.stopPositions)-1].next().vehicle()
}

Expand Down
8 changes: 4 additions & 4 deletions solution_move_stops_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ) {
// }
func SolutionMoveStopsGeneratorChannel(
vehicle solutionVehicleImpl,
vehicle SolutionVehicle,
planUnit *solutionPlanStopsUnitImpl,
quit <-chan struct{},
stops SolutionStops,
Expand Down Expand Up @@ -64,7 +64,7 @@ func SolutionMoveStopsGeneratorChannelTest(
preAllocatedMoveContainer *PreAllocatedMoveContainer,
) chan SolutionMoveStops {
return SolutionMoveStopsGeneratorChannel(
vehicle.(solutionVehicleImpl),
vehicle,
planUnit.(*solutionPlanStopsUnitImpl),
quit,
stops,
Expand All @@ -82,7 +82,7 @@ func SolutionMoveStopsGeneratorTest(
shouldStop func() bool,
) {
SolutionMoveStopsGenerator(
vehicle.(solutionVehicleImpl),
vehicle,
planUnit.(*solutionPlanStopsUnitImpl),
yield,
stops,
Expand All @@ -94,7 +94,7 @@ func SolutionMoveStopsGeneratorTest(
// SolutionMoveStopsGenerator generates all possible moves for a given vehicle and
// plan unit. The function yield is called for each solutionMoveStopsImpl.
func SolutionMoveStopsGenerator(
vehicle solutionVehicleImpl,
vehicle SolutionVehicle,
planUnit *solutionPlanStopsUnitImpl,
yield func(move SolutionMoveStops),
stops SolutionStops,
Expand Down
4 changes: 2 additions & 2 deletions solution_move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestVehicleBestMoveSinglePlanUnit(t *testing.T) {

solutionVehicle := solution.SolutionVehicle(model.Vehicle(0))

if solutionVehicle == nil {
if solutionVehicle.IsZero() {
t.Error("solutionVehicle is nil")
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func TestVehicleBestMoveSequencePlanUnit(t *testing.T) {

solutionVehicle := solution.SolutionVehicle(model.Vehicle(0))

if solutionVehicle == nil {
if solutionVehicle.IsZero() {
t.Error("solutionVehicle is nil")
}

Expand Down
4 changes: 2 additions & 2 deletions solution_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func (v SolutionStop) Vehicle() SolutionVehicle {
return v.solution.solutionVehicles[v.solution.inVehicle[v.index]]
}

func (v SolutionStop) vehicle() solutionVehicleImpl {
return solutionVehicleImpl{
func (v SolutionStop) vehicle() SolutionVehicle {
return SolutionVehicle{
index: v.solution.inVehicle[v.index],
solution: v.solution,
}
Expand Down
2 changes: 1 addition & 1 deletion solution_stop_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newSolutionStopGenerator(
startAtFirst bool,
endAtLast bool,
) *solutionStopGeneratorImpl {
nextStop := move.vehicle().first()
nextStop := move.Vehicle().First()
if !startAtFirst {
nextStop = move.stopPositions[0].previous()
}
Expand Down
Loading
Loading