Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down
69 changes: 53 additions & 16 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,13 @@
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)

Check failure on line 177 in check/check.go

View workflow job for this annotation

GitHub Actions / go-lint

File is not properly formatted (gci)

Check failure on line 177 in check/check.go

View workflow job for this annotation

GitHub Actions / go-lint

File is not properly formatted (gci)
defer func() {
m.solution.Model().RemoveSolutionObserver(planUnitsObserver)
m.solution.Model().RemoveSolutionObserver(moveObserver)
}()

SolutionPlanUnitLoop:
for solutionPlanUnitIdx, solutionPlanUnit := range solutionPlanUnits {
Expand Down Expand Up @@ -209,7 +207,8 @@
}

moveMinimumValue := math.MaxFloat64
moveIsImprovement := false
hasImprovingMove := false
hasWorseMove := false
movesFailed := false
VehicleLoop:
for solutionVehicleIdx, solutionVehicle := range m.solution.Vehicles() {
Expand All @@ -219,6 +218,8 @@
continue
}

m.output.PlanUnits[solutionPlanUnitIdx].HasExecutableBestMove = true

if m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves != nil {
*m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves++
}
Expand All @@ -228,6 +229,7 @@
VehicleID: solutionVehicle.ModelVehicle().ID(),
DeltaObjectiveEstimate: &value,
FailedConstraints: []string{},
WasExecutable: true,
Comment thread
merschformann marked this conversation as resolved.
Resolved
}

if solutionMoveStops, ok := bestMove.(nextroute.SolutionMoveStops); ok {
Expand All @@ -242,6 +244,21 @@
}
}

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
}
Comment thread
merschformann marked this conversation as resolved.
Resolved

if m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves != nil {
m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves = append(
m.output.PlanUnits[solutionPlanUnitIdx].VehiclesWithMoves,
Expand All @@ -253,23 +270,44 @@
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 {
Comment thread
merschformann marked this conversation as resolved.
Resolved
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
Expand Down Expand Up @@ -325,12 +363,12 @@
}
}

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()))
Expand Down Expand Up @@ -364,7 +402,6 @@
localCtx,
m.solution.UnPlannedPlanUnits().SolutionPlanUnits(),
)

if err != nil {
errorStr := err.Error()
m.output.Error = &errorStr
Expand Down
32 changes: 31 additions & 1 deletion check/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
// 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"`
Expand All @@ -115,6 +120,23 @@
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"`
}


Check failure on line 139 in check/schema/schema.go

View workflow job for this annotation

GitHub Actions / go-lint

File is not properly formatted (gci)

Check failure on line 139 in check/schema/schema.go

View workflow job for this annotation

GitHub Actions / go-lint

File is not properly formatted (gci)
// VehiclesWithMovesDetail shows details of the vehicles that have moves.
type VehiclesWithMovesDetail struct {
// Vehicle is the ID of the vehicle.
Expand All @@ -125,10 +147,18 @@
// 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"`
}
Expand Down
2 changes: 1 addition & 1 deletion common/distance.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion common/duration.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion common/fast_haversine.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion common/haversine.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion common/intersect.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion common/slices.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading
Loading