A Constraint Programming solver in Python designed to address the Crop Planting Layout Problem (CPLP). The project models crops spatial allocation using modern CP-SAT formulations.
The Crop Planting Layout Problem involves assigning agricultural crops to clusters of species across discrete planning horizons while respecting spatial constraints:
- Spatial Allocation: Preventing overlaps of clusters.
- Demand Satisfaction: Meeting the expected units of species.
- Objective: Maximizing beneficial interactions between species.
The instances must be a .dat file like this one:
K=6;
M=198;
H=3;
a=[[0, 1, 0],
[1, 0, 0],
[0, 0, 0]];
o = [ 2, 1, 1 ];
c_min = [ 32, 33, 1 ];
c_max = [ 33, 33, 33 ];
d = [ 16, 165, 1 ];
where
K: Number of rowsM: Total number of cellsH: Number of speciesa: Score matrixo: Number of cells required for one unit of speciesc_min: Minimum cluster lengthc_max: Maximum cluster lengthd: Demand of units of species
- Language: Python 3.10+
- Solver Engine: Google OR-Tools (CP-SAT)
- Data Handling: Pandas
- Visualization: Matplotlib
.
├── utils/
├── README.md
├── analyze_benchmarks.py
├── main.py
├── requirements.txt
└── run_benchmarks.py
git clone https://github.com/davidecavone/Crop-Planting-Layout-Solver.gitCreate a clean virtual environment and install the dependencies:
python -m venv venv
# Linux / macOS:
source venv/bin/activate
# Windows:
# .\venv\Scripts\activate
pip install -r requirements.txtpython main.py <instance> [-h] [--mode {hard,soft}] [--allelopathy-threshold INT]
[--time-limit INT] [--workers INT] [--export-plots]where
instance: Instance path--mode: Adjacency constraint: 'hard' or 'soft' (default: hard)--allelopathy-threshold: Below this allelopathy threshold two species are considered incompatibles (default: -100)--time-limit: Solver Time Limit (default: 60)--workers: Number of parallel threads used by the CP-SAT solver (default: 4)--export-plots: Exports found solution as a PNG image (default: False)
In the project path, do
cd computational_campaign
python run.py <instance> [-h] [--allelopathy-threshold INT] [--export-results] [--export-plots]where
instance: Instance path--allelopathy-threshold: Below this allelopathy threshold two species are considered incompatibles (default: -100)--export-results: Exports solutions benchmarks in results folder as CSV file--export-plots: Exports found solutions as a PNG image (default: False) Campaign time limits, pool size and configurations can be adjusted in the config.py file.
If you use this work or reference the underlying model and tools, please cite the corresponding sources:
Tommaso Adamo, L. Colizzi, G. Dimauro, Emanuela Guerriero, and D. Pareo, "Crop planting layout optimization in sustainable agriculture: A constraint programming approach," Computers and Electronics in Agriculture, vol. 224, p. 109162, 2024. https://doi.org/10.1016/j.compag.2024.109162
Philippe Laborie, Jérôme Rogerie, Paul Shaw, and Petr Vilím, "IBM ILOG CP Optimizer for scheduling," Constraints, vol. 23, no. 2, pp. 210–250, 2018. https://doi.org/10.1007/s10601-018-9281-x
Philippe Laborie, Jérôme Rogerie, Paul Shaw, and Petr Vilím, "Interval-Based Language for Modeling Scheduling Problems: An Extension to Constraint Programming," in Algebraic Modeling Systems: Modeling and Solving Real World Optimization Problems, Applied Optimization, vol. 104, J. Kallrath, Ed., Springer, pp. 111–143, 2012. https://doi.org/10.1007/978-3-642-23592-4_6
Filippo Focacci, Andrea Lodi, and Michela Milano, "Mathematical Programming Techniques in Constraint Programming: A Short Overview," Journal of Heuristics, vol. 8, no. 1, pp. 7–17, 2002.
K. A. Bybee-Finley and M. R. Ryan, "Advancing intercropping research and practices in industrialized agricultural landscapes," Agriculture, vol. 8, no. 6, p. 80, 2018.
Laurent Perron and Frédéric Didier, "Google OR-Tools CP-SAT Solver," 2024. https://developers.google.com/optimization/cp/cp_solver
IBM Corporation, "IBM ILOG CP Optimizer User's Manual," Version 22.1.1, 2023.
Stuart Russell and Peter Norvig, "Constraint Satisfaction Problems," in Artificial Intelligence: A Modern Approach, 4th ed., ch. 5, Pearson, 2020.
Distributed under the MIT License. See LICENSE for more details.
