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

Refactor dwave.optimization.generators #42

Merged
merged 3 commits into from
Jul 4, 2024

Conversation

arcondello
Copy link
Member

  • Clean up pep8 errors and whitespace
  • Remove redundant tests in tests/test_examples.py
  • Refactor input argument checking/coercion
  • Slightly generalize some of the generators

@arcondello arcondello requested a review from JoelPasvolsky July 3, 2024 21:19
Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

That's a lot of trailing spaces you cleaned up.

dwave/optimization/generators.py Outdated Show resolved Hide resolved
raise ValueError(f"`{argname}` must be a square 2D array-like of non-negative numbers")
# we do "size up"
while array.ndim < ndim:
array = array[np.newaxis]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not quite sure what is intended with this clever trick but I doubt it is this:

model = job_shop_scheduling(times=[1, 2, 3, 4], machines=[4, 3, 2, 1])
ValueError: arrays must have the same shape

I think the code will be clearer if, for a requirement on square matrices, you check that array.shape[0] == array.shape[1] and don't upsize.

Copy link
Member Author

Choose a reason for hiding this comment

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

That error was present in the original code. Turns out it fails on main for every non-square times/machines. Will fix.

Re the dimension increase, I am following the pattern of np.atleast_2d(), which is typical in this sort of place.

Copy link
Contributor

Choose a reason for hiding this comment

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

model = job_shop_scheduling(times=[[1], [2], [2], [1]], machines=[[1], [1], [1], [1]])
ValueError: cannot do a reduction on an empty array with an operation that has no identity without supplying an initial value

This is probably fine in this PR, these parameters probably should be accepted, so might need a separate PR to update the model.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that's a different bug. Will fix that one too.

Copy link
Member Author

Choose a reason for hiding this comment

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

#43

Copy link
Member Author

Choose a reason for hiding this comment

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

Rebased off of #44

@arcondello arcondello force-pushed the generators-refactor branch from 7b22af9 to 6e9ee33 Compare July 4, 2024 15:04
raise ValueError(f"`{argname}` must be a square 2D array-like of non-negative numbers")
# we do "size up"
while array.ndim < ndim:
array = array[np.newaxis]
Copy link
Contributor

Choose a reason for hiding this comment

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

model = job_shop_scheduling(times=[[1], [2], [2], [1]], machines=[[1], [1], [1], [1]])
ValueError: cannot do a reduction on an empty array with an operation that has no identity without supplying an initial value

This is probably fine in this PR, these parameters probably should be accepted, so might need a separate PR to update the model.

array = np.atleast_2d(np.asarray(array))
except (ValueError, TypeError):
raise ValueError(f"`{argname}` must be a square 2D array-like of non-negative numbers")
# we do "size up"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment here to give the why?

* Clean up pep8 errors and whitespace
* Remove redundant tests in tests/test_examples.py
* Refactor input argument checking/coercion
* Slightly generalize some of the generators
@arcondello arcondello force-pushed the generators-refactor branch from e290128 to 3f29e7b Compare July 4, 2024 17:36
@arcondello arcondello merged commit c36f47b into dwavesystems:main Jul 4, 2024
31 checks passed
@arcondello arcondello deleted the generators-refactor branch July 4, 2024 19:32
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