dhont_optimizer is a Python library designed for optimizing seat allocations in electoral systems using the D'Hondt method. It offers a flexible approach to model electoral systems, making it suitable for various provinces and parties.
To install dhont_optimizer, clone the repository and use the setup script:
git clone https://github.com/IsmaelOA/dhont_optimizer.git
cd dhont_optimizer
python setup.py install
This will install the package along with its required dependencies.
In "dhont_optimizer," settings play a crucial role in tailoring the optimization process to the specific electoral system and political landscape you want to analyze. Here's a breakdown of the key settings:
This dictionary defines the electoral landscape, including the number of seats in each province and the voting data for each party within those provinces. You can customize this to represent the electoral scenario you're interested in.
partiesandwings: These sub-settings allow you to define default values for parties' and wings' properties. You can set global defaults for party permeability and wing permeability.
You can define custom settings for specific provinces. For example, in the "Cadiz" province, we've defined a "group" as "group-test." This allows you to group provinces with similar settings for easier management.
-
parties_permeabilityandwings_permeability: These settings specify the permeability between parties and wings within the "grupo-test" group. It allows you to model how parties and wings interact within this specific group. -
objective_function_weights: This setting defines the weights for the objective functions used in the optimization process. It determines the importance of different factors such as party seats, wing seats, party movements, and wing movements. -
default_party_permeabilityanddefault_wing_permeability: These settings specify default permeability values for parties and wings if not explicitly defined for a province or group. -
default_party_seats_weightanddefault_wing_seats_weight: These settings define default weights for party and wing seats if not specified for a province or group. -
default_party_movements_weightanddefault_wing_movements_weight: These settings define default weights for party and wing movements if not specified for a province or group. -
first_contested_seat: This setting determines the first contested seat in the optimization process.
This section provides default settings that apply globally if not overridden at the province or group level. It's a convenient way to ensure consistency in settings across the project.
Feel free to customize these settings to model different electoral systems and scenarios effectively.
To understand the seat allocation process in "dhont_optimizer," consider the following code snippet:
model = SeatsModel(province_list, settings)
self.model.create_model()
self.model.solve_model()
-
Model Initialization (model = SeatsModel(province_list, settings)): In this step, an instance of the SeatsModel object is created. This object serves as the representation of the electoral system and is configured using the information provided in province_list and settings. Essentially, it sets up the electoral model that will be used for seat allocation calculations.
-
Model Creation (self.model.create_model()): After initializing the model, the create_model() method is called on the model instance. This method is responsible for defining and constructing the mathematical optimization model that encapsulates the configured electoral system. It establishes the rules and constraints necessary for seat allocation.
-
Model Solving (self.model.solve_model()): Once the model is created and all constraints are set, the solve_model() method is invoked. This method leverages a mathematical solver, potentially implemented using Google OR-Tools, to find the optimal solution for seat allocation. In essence, it executes the optimization process that determines how seats are allocated based on the votes and configurations specified in province_list and settings.
These code snippets represent the core workflow of "dhont_optimizer," where you create, configure, and solve the electoral model to obtain the optimal seat distribution according to the defined electoral rules and constraints.
Contributions to dhont_optimizer are welcome! If you have suggestions for improvement or want to contribute to the code, please feel free to fork the repository, make changes, and submit a pull request. Please ensure your code adheres to the current coding standards and test before submitting any changes.
To run the tests, navigate to the project's root directory and execute:
python -m unittest discover
The project is currently in development. Features and documentation may be added or improved over time.
This project is licensed under the MIT License - see the LICENSE file for details.