diff --git a/.golangci.yml b/.golangci.yml index 8ba6af8f..d43a1555 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -216,6 +216,9 @@ linters: - linters: - nestif path: factory/validate\.go + - linters: + - revive + path: common/.*\.go # we do not want to break users to address the "common" package naming paths: - third_party$ - builtin$ diff --git a/check/check.go b/check/check.go index 8fe4f1a8..316d43f0 100644 --- a/check/check.go +++ b/check/check.go @@ -172,15 +172,13 @@ func (m *checkImpl) checkSolutionPlanUnits( planUnitsObserver := newObserver() moveObserver := newObserver() - if int(m.verbosity) >= int(Medium) { - defer func() { - m.solution.Model().RemoveSolutionObserver(planUnitsObserver) - m.solution.Model().RemoveSolutionObserver(moveObserver) - }() - - m.solution.Model().AddSolutionObserver(planUnitsObserver) - m.solution.Model().AddSolutionObserver(moveObserver) - } + m.solution.Model().AddSolutionObserver(planUnitsObserver) + m.solution.Model().AddSolutionObserver(moveObserver) + + defer func() { + m.solution.Model().RemoveSolutionObserver(planUnitsObserver) + m.solution.Model().RemoveSolutionObserver(moveObserver) + }() SolutionPlanUnitLoop: for solutionPlanUnitIdx, solutionPlanUnit := range solutionPlanUnits { @@ -209,7 +207,8 @@ SolutionPlanUnitLoop: } moveMinimumValue := math.MaxFloat64 - moveIsImprovement := false + hasImprovingMove := false + hasWorseMove := false movesFailed := false VehicleLoop: for solutionVehicleIdx, solutionVehicle := range m.solution.Vehicles() { @@ -219,6 +218,8 @@ SolutionPlanUnitLoop: continue } + m.output.PlanUnits[solutionPlanUnitIdx].HasExecutableBestMove = true + if m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves != nil { *m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves++ } @@ -228,6 +229,7 @@ SolutionPlanUnitLoop: VehicleID: solutionVehicle.ModelVehicle().ID(), DeltaObjectiveEstimate: &value, FailedConstraints: []string{}, + WasExecutable: true, } if solutionMoveStops, ok := bestMove.(nextroute.SolutionMoveStops); ok { @@ -242,6 +244,21 @@ SolutionPlanUnitLoop: } } + if bestSolutionMoveStops, ok := bestMove.(nextroute.SolutionMoveStops); ok && int(m.verbosity) >= int(High) { + currentObjectiveTerms := []schema.ObjectiveTermDelta{} + for _, term := range m.solution.Model().Objective().Terms() { + deltaValue := term.Objective().EstimateDeltaValue(bestSolutionMoveStops) + currentObjectiveTerms = append( + currentObjectiveTerms, + schema.ObjectiveTermDelta{ + Name: fmt.Sprintf("%v", term.Objective()), + DeltaValueEstimated: deltaValue, + }, + ) + } + vehicleDetails.ObjectiveDeltas = currentObjectiveTerms + } + if m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves != nil { m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves = append( m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves, @@ -253,23 +270,44 @@ SolutionPlanUnitLoop: actualScoreBeforeMove := m.solution.Score() moveObserver.Reset() + previousObjectiveValues := make([]float64, len(m.solution.Model().Objective().Terms())) + if int(m.verbosity) >= int(High) { + for termIdx, term := range m.solution.Model().Objective().Terms() { + previousObjectiveValues[termIdx] = m.solution.ObjectiveValue(term.Objective()) / term.Factor() + } + } + planned, err := bestMove.Execute(ctx) if err != nil { return err } + if int(m.verbosity) >= int(High) { + for termIdx, term := range m.solution.Model().Objective().Terms() { + value := m.solution.ObjectiveValue(term.Objective()) / term.Factor() - previousObjectiveValues[termIdx] + vehicleDetails.ObjectiveDeltas[termIdx].DeltaValue = &value + } + } + + failedForConstraint := false for _, constraint := range moveObserver.OnPlanFailedConstraints() { name := fmt.Sprintf("%v", constraint) vehicleDetails.FailedConstraints = append( vehicleDetails.FailedConstraints, name, ) + failedForConstraint = true + vehicleDetails.WasExecutable = false + } + + if !planned && !failedForConstraint { + hasWorseMove = true } if planned { - moveIsImprovement = true vehicleDetails.WasPlannable = true deltaObjective := statistics.Float64(m.solution.Score() - actualScoreBeforeMove) + hasImprovingMove = deltaObjective < 0 vehicleDetails.DeltaObjective = &deltaObjective m.output.PlanUnits[solutionPlanUnitIdx].HasPlannableBestMove = true @@ -325,12 +363,12 @@ SolutionPlanUnitLoop: } } + if !hasImprovingMove && hasWorseMove { + m.output.PlanUnits[solutionPlanUnitIdx].PlanningMakesObjectiveWorse = true + m.output.Summary.NumberOfPlanUnitsMakingObjectiveWorse++ + } if m.output.PlanUnits[solutionPlanUnitIdx].HasPlannableBestMove { m.output.Summary.PlanUnitsBestMoveFound++ - if !moveIsImprovement { - m.output.PlanUnits[solutionPlanUnitIdx].PlanningMakesObjectiveWorse = true - m.output.Summary.NumberOfPlanUnitsMakingObjectiveWorse++ - } } else { m.output.Summary.PlanUnitsHaveNoMove++ constraints := make(map[string]int, len(m.solution.Model().Constraints())) @@ -364,7 +402,6 @@ func (m *checkImpl) Check() { localCtx, m.solution.UnPlannedPlanUnits().SolutionPlanUnits(), ) - if err != nil { errorStr := err.Error() m.output.Error = &errorStr diff --git a/check/schema/schema.go b/check/schema/schema.go index 7d74fb88..4d91ce6b 100644 --- a/check/schema/schema.go +++ b/check/schema/schema.go @@ -97,6 +97,11 @@ type PlanUnit struct { // execute it, it wasn't executable - this is an error in the constraint // estimation. HasPlannableBestMove bool `json:"has_plannable_best_move"` + // HasExecutableBestMove is true if a best move is found for the plan unit + // and that best move is executable (i.e. it can be executed). A move might + // be executable but not plannable if it makes the objective worse or if + // its execution later fails. + HasExecutableBestMove bool `json:"has_executable_best_move"` // PlanningMakesObjectiveWorse is true if the best move for the plan unit // increases the objective. PlanningMakesObjectiveWorse bool `json:"planning_makes_objective_worse"` @@ -115,6 +120,23 @@ type PlanUnit struct { Constraints map[string]int `json:"constraints,omitempty"` } +// ObjectiveTermDelta contains information about the improvement of an objective +// term caused by a move. A negative delta means the move improves the value for +// the given term, a positive delta means the move worsens the value for the +// given term. +type ObjectiveTermDelta struct { + // Name is the name of the objective term. + Name string `json:"name"` + // DeltaValue is the estimated difference in value of the objective term + // (without the factor). + DeltaValueEstimated float64 `json:"delta_value_estimated"` + // DeltaValue is the difference in value of the objective term after + // executing the move (without the factor). This is only calculated if the + // move was executed successfully. + DeltaValue *float64 `json:"delta_value,omitempty"` +} + + // VehiclesWithMovesDetail shows details of the vehicles that have moves. type VehiclesWithMovesDetail struct { // Vehicle is the ID of the vehicle. @@ -125,10 +147,18 @@ type VehiclesWithMovesDetail struct { // DeltaObjective is the actual delta of the objective of that will be // incurred by the move. DeltaObjective *statistics.Float64 `json:"delta_objective,omitempty"` + // ObjectiveDeltas are the (estimates of the) objective value deltas of the + // move. I.e., the difference in value of the individual terms of the + // objective. This is only calculated if the verbosity is high. + ObjectiveDeltas []ObjectiveTermDelta `json:"objective_deltas,omitempty"` // FailedConstraints are the constraints that are violated for the move. FailedConstraints []string `json:"failed_constraints,omitempty"` - // WasPlannable is true if the move was plannable, false otherwise. + // WasPlannable is true if the move was plannable, false otherwise. A move + // can be planned if it is executable and does not make the objective worse. WasPlannable bool `json:"was_plannable"` + // WasExecutable is true if the move was executable, false otherwise. A move + // can be executed if constraints are not violated. + WasExecutable bool `json:"was_executable"` // Positions defines where the stop should be inserted. Positions []Position `json:"positions"` } diff --git a/common/distance.go b/common/distance.go index f5893f4b..6bcf3f9b 100644 --- a/common/distance.go +++ b/common/distance.go @@ -1,7 +1,7 @@ // © 2019-present nextmv.io inc // Package common contains common types and functions. -package common // nolint:revive // we are not causing breaking changes to please the linter +package common import "fmt" diff --git a/common/duration.go b/common/duration.go index 2892492b..38498076 100644 --- a/common/duration.go +++ b/common/duration.go @@ -1,6 +1,6 @@ // © 2019-present nextmv.io inc -package common //nolint:revive // we are not causing breaking changes to please the linter +package common import ( "fmt" diff --git a/common/fast_haversine.go b/common/fast_haversine.go index a80b8dd8..3379275b 100644 --- a/common/fast_haversine.go +++ b/common/fast_haversine.go @@ -1,6 +1,6 @@ // © 2019-present nextmv.io inc -package common //nolint:revive // we are not causing breaking changes to please the linter +package common import ( "fmt" diff --git a/common/haversine.go b/common/haversine.go index 423eb2dc..d678fa30 100644 --- a/common/haversine.go +++ b/common/haversine.go @@ -1,6 +1,6 @@ // © 2019-present nextmv.io inc -package common //nolint:revive // we are not causing breaking changes to please the linter +package common import ( "fmt" diff --git a/common/intersect.go b/common/intersect.go index e1826ee8..ec5505fc 100644 --- a/common/intersect.go +++ b/common/intersect.go @@ -1,6 +1,6 @@ // © 2019-present nextmv.io inc -package common //nolint:revive // we are not causing breaking changes to please the linter +package common // Intersect returns the intersection of two slices. func Intersect[T comparable](a []T, b []T) []T { diff --git a/common/slices.go b/common/slices.go index f9d661af..e72de958 100644 --- a/common/slices.go +++ b/common/slices.go @@ -1,6 +1,6 @@ // © 2019-present nextmv.io inc -package common //nolint:revive // we are not causing breaking changes to please the linter +package common // CopySliceFrom cuts of a slice from `alloc` and copies the data from `data` // into it. It returns the new slice and the remaining slice of `alloc`. This diff --git a/tests/check/input.json.golden b/tests/check/input.json.golden index b701dcee..1d09d94b 100644 --- a/tests/check/input.json.golden +++ b/tests/check/input.json.golden @@ -126,6 +126,7 @@ "value": -1998927.0938755402, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -136,6 +137,16 @@ { "delta_objective": -999463.5469377702, "delta_objective_estimate": -999463.5469377701, + "objective_deltas": [ + { + "delta_value": 536.4530622299, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -144,6 +155,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -168,6 +180,7 @@ "value": -1998940.772192232, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -178,6 +191,16 @@ { "delta_objective": -999470.3860961162, "delta_objective_estimate": -999470.386096116, + "objective_deltas": [ + { + "delta_value": 529.6139038839777, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -186,6 +209,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -210,6 +234,7 @@ "value": -1999305.958360977, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -220,6 +245,16 @@ { "delta_objective": -999652.9791804887, "delta_objective_estimate": -999652.9791804885, + "objective_deltas": [ + { + "delta_value": 347.02081951153525, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -228,6 +263,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -252,6 +288,7 @@ "value": -1999174.925711939, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -262,6 +299,16 @@ { "delta_objective": -999587.4628559696, "delta_objective_estimate": -999587.4628559696, + "objective_deltas": [ + { + "delta_value": 412.537144030441, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -270,6 +317,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -294,6 +342,7 @@ "value": -1999041.6594053446, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -304,6 +353,16 @@ { "delta_objective": -999520.8297026725, "delta_objective_estimate": -999520.8297026723, + "objective_deltas": [ + { + "delta_value": 479.1702973276746, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -312,6 +371,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -336,6 +396,7 @@ "value": -1999424.7676562932, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -346,6 +407,16 @@ { "delta_objective": -999712.3838281464, "delta_objective_estimate": -999712.3838281466, + "objective_deltas": [ + { + "delta_value": 287.61617185337144, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -354,6 +425,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ] @@ -378,6 +450,7 @@ "value": -2000000, "vehicle": "v1" }, + "has_executable_best_move": true, "has_plannable_best_move": true, "planning_makes_objective_worse": false, "stops": [ @@ -388,6 +461,16 @@ { "delta_objective": -1000000, "delta_objective_estimate": -1000000, + "objective_deltas": [ + { + "delta_value": 0, + "name": "vehicles_duration" + }, + { + "delta_value": -1000000, + "name": "unplanned_penalty" + } + ], "positions": [ { "next": "v1-end", @@ -396,6 +479,7 @@ } ], "vehicle_id": "v1", + "was_executable": true, "was_plannable": true } ]