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

nilsbeck/add time dependent matrices #66

Merged
merged 19 commits into from
Oct 23, 2024

Conversation

nilsbeck
Copy link
Collaborator

Description

Up until now it was only possible to add one single duration_matrix in the input which was being used for all vehicles (when given).
To be more flexible and cover more use cases this PR allows to pass more matrices.

  • A schema.DurationMatrices can be used to model time dependent matrices for all vehicles
type DurationMatrices struct {
	// VehicleIDs is a list of vehicle IDs for which the duration matrix is defined.
	// Must be empty for a single matrix.
	VehicleIDs []string `json:"vehicle_ids,omitempty"`
	// DefaultMatrix is the default duration matrix used for undefined time frames
	DefaultMatrix [][]float64 `json:"default_matrix"`

	// TimeFrames contains time-dependent matrices or scaling factors
	TimeFrames []TimeFrame `json:"time_frames,omitempty"`
}

In this scenario the vehicle ids have to be blank, the time frames can be set to create time dependencies for the default matrix. Within a TimeFrame you can either pass a new matrix or scale the default matrix with a factor. The specific matrix will be applied for the time frame, specified by a start and end date.

  • A slice of schema.DurationMatrices to create multiple time dependent matrices for different vehicles. This is done be specifying the vehicle ids in the input so that every vehicle gets assigned the corresponding time dependent matrices.

Copy link
Member

@merschformann merschformann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks! Just had some naming comments and other questions. 😊
Happy to quickly sync on it and move on. ☺️


// TryAssertFloat64Matrix tries to assert that the given matrix is a
// [][]float64. Returns true if successful and the matrix otherwise false.
func TryAssertFloat64Matrix(matrix []any) ([][]float64, bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also check whether the matrix is square?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not done here, but in the validateMatrix function here to re-use the code we were using already:

func validateMatrix(

scaling_factor: Optional[float] = None
"""Scaling factor for the time frame."""

class DurationMatrices(BaseModel):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this something more specific? DurationMatrices suggests to me that it is simply a slice of duration matrices, but it is much more complex than that.
If we are planning to extend the features further in future, I would probably even go for something like CustomizedMatrix. There is probably a better name for it. If it is only features controlled by time frames, maybe we just call it TimeBasedMatrix or TimeDependentMatrix to be in line with other language we use.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -29,6 +30,24 @@ class DurationGroup(BaseModel):
group: List[str]
"""Stop IDs contained in the group."""

class TimeFrame(BaseModel):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can rename this struct to MatrixTimeFrame or something a little more specific. Just to make sure it won't get mixed up with something else, as this feels like a very custom feature and not very generic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

schema/input.go Outdated
Comment on lines 39 to 40
// DurationMatrices represents time-dependent duration matrices.
type DurationMatrices struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same naming comments as with the Python code. 😊

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

schema/input.go Outdated
Comment on lines 51 to 52
// TimeFrame represents a time-dependent duration matrix or scaling factor.
type TimeFrame struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here. I should have commented on the Go code first. 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a very brief markdown file on what we expect for these test cases? Similar to multi_window.md and such. Really not required, but I have trouble following such complex golden file tests and am concerned that we loose the tests intention accidentally over time.
Can totally be shorted as well. No need to copy the content of above example, just having some comment in a equally named .md feels like it may help these test cases. 😊

Comment on lines +68 to +75
jsonData, err := json.Marshal(matrix)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonData, &durationMatrices)
if err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we do this to simplify conversion, and I am not opposing it. Just wondering: do we know whether this becomes a performance problem at a certain size that we think is still realistic to have? 🤔
(saw this being used before; just a thought - not blocking at all)

@nilsbeck nilsbeck merged commit f04497b into develop Oct 23, 2024
33 checks passed
@nilsbeck nilsbeck deleted the nilsbeck/add-time-dependent-matrices branch October 23, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants