Commit Graph

23 Commits

Author SHA1 Message Date
Jeremy Saklad 551363b9b7
Replace unnecessary variables with lambdas
Variables that are only used in one line lend undue complexity to code.
Using a one-shot lambda makes the use of a declaration much more
obvious.
2022-01-02 17:57:53 -06:00
Jeremy Saklad 15317f6298
Avoid extraneous intermediate variables
Intermediate variables should not be necessary if the equivalent
expression is a constant or a variable with the same bounds.

This change reduces the total number of variables in the model.

The documentation of BoneMarketModel.NewIntermediateIntVar has been
tweaked to allow for any number of constraints.
2022-01-02 17:28:58 -06:00
Jeremy Saklad e211becb30
Add type hints to helper functions
For the sake of maintainability, it is very important the parameters and
results of the helper functions are spelled out explicitly.

Union type hints are being left until Python 3.10 is supported.
2021-10-16 17:17:56 -05:00
Jeremy Saklad 40246c4815
Remove BoneMarketModel.AddDivisionMultiplicationEquality
This function has been obsoleted by the ability to pass partial methods
as parameters to helper functions.

Use of this function has been replaced with partial methods accordingly.

In addition, the following code style has been applied: partial methods that
contain a nested partial method have the function arguments broken into
distinct lines. This means that the bottom partial method is formatted in the
same manner as a partial method without any nesting.
2021-10-16 15:11:12 -05:00
Jeremy Saklad 1175575c92
Remove BoneMarketModel.AddDivisionApproximateExponentiationEquality
This function has been obsoleted by the ability to pass partial methods
as parameters to helper functions.

Use of this function has been replaced with partial methods accordingly.
This has allowed some intermediate variables to be removed entirely,
allowing for further simplification.
2021-10-16 13:14:30 -05:00
Jeremy Saklad 6b9643041a
Overload BoneMarketModel.NewIntermediateIntVar
Intermediate integer variables can now be created for compatible partial
methods, which massively increases the expressiveness of each helper
function.

This change should allow the compound helper functions, such as
AddDivisionMultiplicationEquality, to be removed entirely. It will also
allow even the most complex buyers to avoid intermediate variables.
2021-10-16 13:04:31 -05:00
Jeremy Saklad 9671190059
Add BoneMarketModel.AddDivisionApproximateExponentiationEquality
This method combines two common operations, avoiding the need for
intermediate variables at the call site.
2021-10-16 10:22:31 -05:00
Jeremy Saklad 110f524f62
Update OR-Tools to v9.1
This update, despite ostensibly being a minor version, includes breaking
changes that must be accounted for.

Overflow is much more strictly checked, so the magnitude of certain
constants has been decreased.

CP-SAT's default number of workers has been changed to reflect the
default of this script. As such, the script no longer needs to change
that parameter unless a specific number of workers has been specified.

In light of the breaking changes, the OR-Tools version is now pinned at
9.1 instead of being permitted to use future minor versions.
2021-10-15 21:32:55 -05:00
Jeremy Saklad 1bf18fe8e1
Add BoneMarketModel.AddDivisionMultiplicationEquality
This method combines two common operations, avoiding the need for
intermediate variables at the call site.
2021-10-15 15:05:17 -05:00
Jeremy Saklad 46edfcb35b
Override CpModel.AddDivisionEquality
The overridden method supports enforcement literals using indirection.
2021-10-15 15:05:07 -05:00
Jeremy Saklad 6f0e0ac379
Add BoneMarketModel.AddIf method
This method allows a series of constraints to be applied all at once
using the same enforcement literal, which can substantially improve
readability and writability.
2021-10-15 15:04:37 -05:00
Jeremy Saklad 17add382ec
Replace BoneMarketModel.NewIntermediateBoolVar
The replacement method, BoolExpression, accepts a
BoundedLinearExpression directly, and uses memoization to reuse Boolean
variables. This technique is unsuitable for other methods, as
constraints need to be applied separately, but in this case it is fine.

The new method is much easier to understand, and far easier to read at
call sites.

Variable names are generated from the expression, rather than being
explicitly specified.
2021-10-15 15:04:37 -05:00
Jeremy Saklad 3d90d61deb
Replace helper functions with subclass
The new subclass, BoneMarketModel, overrides CpModel's methods with
improvements such as default parameters and support for enforcement
literals.

This should allow substantial improvements to the readability of the
solver in the future.

In addition, various lists have been replaced with tuples where
appropriate.
2021-10-15 15:03:55 -05:00
Jeremy Saklad 5ef972295b
Use f-strings and multiline strings
Inline string expressions are far easier to read, particularly for
complicated strings like the skeleton printer's output.

Care has been taken to ensure that no behavior has been changed in the
process.
2021-09-18 20:01:30 -05:00
Jeremy Saklad 50625cdc27
Add __slots__ to internal parser types
Dynamic properties are not used by these types, so using __slots__ is
slightly more efficient. Public inherited properties are also included.
2021-09-18 19:18:39 -05:00
Jeremy Saklad 21ea715dd1
Refactor Action as data class
Action is now a frozen data class.

All properties of Action now have type hints, and __slots__ are used
for properties without default values (that is, name).
2021-09-18 19:18:39 -05:00
Jeremy Saklad 35663dd8d4
Add list option
This option lists enumeration identifiers, along with their string forms
for clarity's sake. This makes it much easier to blacklist things
without reading the source code.

A note about listing has been added to the Blacklisting section of the
README.

A dedicated action has been created for the list command.
2021-08-14 17:55:45 -05:00
Jeremy Saklad 3b1ca61104
Fix BlacklistAction bug
This bug would have caused an error if BlacklistAction were used with a
different nargs.

The "verbose" argument no longer has a default specified. Instead, the
code that checks it supplies a default for itself.
2021-08-14 14:40:15 -05:00
Jeremy Saklad cae8ac5055
Clarify nargs specifiers
Rather than using raw strings, nargs specifiers are now expressed in
terms of argparse's corresponding global variables.
2021-08-14 13:05:17 -05:00
Jeremy Saklad 4034530ddb
Add support for configuration files
Configuration files allow common arguments like --shadowy-level and
--blacklist to be saved and referenced rather being typed out each time.

ArgumentParser has been subclassed to allow more freedom when parsing
configuration files.
2021-08-13 10:26:59 -05:00
Jeremy Saklad be44c9c489
Add blacklist parameter to CLI
Blacklisting uses a dedicated action to parse arguments into enumeration
members.

Choices are not provided for this option, as it would utterly overwhelm
the help interface. This means that errors may not be as helpful,
however.

The README has been updated with a small section on blacklisting, how to
do it, and why you may wish to do it.

Options that were previously commented out are now enabled, since the
user can simply blacklist them instead.
2021-08-13 09:45:27 -05:00
Jeremy Saklad cacb985be6
Fix Enum arguments that accept multiple values
EnumAction was erasing nargs before passing it to the superclass
initializer, which broke handling for arguments with consecutive values.

The help text for --desired-buyer has been updated to reflect that you
actually can specify multiple values with a single declaration now.
2021-08-13 08:27:23 -05:00
Jeremy Saklad 84c62aa3c6
Convert project to package
All scripts are now contained in a package named "bonemarketsolver".

The command-line interface has been moved to __main__.py.

The solver script has been moved to solve.py.

Relative module imports are now used where appropriate.

The invocation method of the CLI has changed: instead of running Python
itself, you can now use "pipenv run bone_market_solver".

The README has been updated to reflect the new usage method.
2021-08-03 14:48:42 -05:00