Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.13"]
python-version: ["3.10", "3.13"]
Comment thread
merschformann marked this conversation as resolved.
Resolved
platform:
[
ubuntu-latest,
Expand Down
38 changes: 31 additions & 7 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ SolutionPlanUnitLoop:
}

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

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

if m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves != nil {
*m.output.PlanUnits[solutionPlanUnitIdx].VehiclesHaveMoves++
}
Expand All @@ -228,6 +231,7 @@ SolutionPlanUnitLoop:
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 +246,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()),
DeltaValue: 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 @@ -258,16 +277,22 @@ SolutionPlanUnitLoop:
return err
}

failedForConstraint := false
for _, constraint := range moveObserver.OnPlanFailedConstraints() {
name := fmt.Sprintf("%v", constraint)
vehicleDetails.FailedConstraints = append(
vehicleDetails.FailedConstraints,
name,
)
failedForConstraint = true
}

if !planned && !failedForConstraint {
Comment thread
merschformann marked this conversation as resolved.
Resolved
hasWorseMove = true
}

if planned {
moveIsImprovement = true
hasImprovingMove = true
vehicleDetails.WasPlannable = true
deltaObjective := statistics.Float64(m.solution.Score() - actualScoreBeforeMove)
vehicleDetails.DeltaObjective = &deltaObjective
Expand Down Expand Up @@ -325,12 +350,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()))
Expand Down Expand Up @@ -364,7 +389,6 @@ func (m *checkImpl) Check() {
localCtx,
m.solution.UnPlannedPlanUnits().SolutionPlanUnits(),
)

if err != nil {
errorStr := err.Error()
m.output.Error = &errorStr
Expand Down
23 changes: 23 additions & 0 deletions check/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -115,6 +120,17 @@ 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 difference in value of the objective term.
DeltaValue float64 `json:"delta_value"`
}

// VehiclesWithMovesDetail shows details of the vehicles that have moves.
type VehiclesWithMovesDetail struct {
// Vehicle is the ID of the vehicle.
Expand All @@ -125,10 +141,17 @@ 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 is the estimate 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 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 requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pydantic>=2.5.2
ruff>=0.1.7
twine>=4.0.2
hatch>=1.13.0
nextmv>=0.14.1
nextmv>=1.0.0
10 changes: 5 additions & 5 deletions src/tests/solve_golden/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ def main() -> None:
"""Entry point for the program."""

parameters = [
nextmv.Parameter("input", str, "", "Path to input file. Default is stdin.", False),
nextmv.Parameter("output", str, "", "Path to output file. Default is stdout.", False),
nextmv.Option("input", str, "", "Path to input file. Default is stdin.", False),
nextmv.Option("output", str, "", "Path to output file. Default is stdout.", False),
]

default_options = nextroute.Options()
for name, default_value in default_options.to_dict().items():
parameters.append(nextmv.Parameter(name.lower(), type(default_value), default_value, name, False))
parameters.append(nextmv.Option(name.lower(), type(default_value), default_value, name, False))

options = nextmv.Options(*parameters)

input = nextmv.load_local(options=options, path=options.input)
input = nextmv.load(options=options, path=options.input)

nextmv.log("Solving vehicle routing problem:")
nextmv.log(f" - stops: {len(input.data.get('stops', []))}")
nextmv.log(f" - vehicles: {len(input.data.get('vehicles', []))}")

model = DecisionModel()
output = model.solve(input)
nextmv.write_local(output, path=options.output)
nextmv.write(output, path=options.output)
Comment thread
merschformann marked this conversation as resolved.
Resolved


class DecisionModel(nextmv.Model):
Expand Down
Loading
Loading