Created
February 24, 2026 17:01
-
-
Save mwdchang/d6969c95370533bf60406f6948c607eb to your computer and use it in GitHub Desktop.
ORTools test constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ortools.sat.python import cp_model | |
| # 1. Create the model | |
| model = cp_model.CpModel() | |
| # 2. Define Boolean variables | |
| high_speed = model.NewBoolVar('high_speed') | |
| high_elevation = model.NewBoolVar('high_elevation') | |
| gps_on = model.NewBoolVar('gps_on') | |
| camera_on = model.NewBoolVar('camera_on') | |
| # 3. Forbidden combination: high_speed AND high_elevation AND camera_on | |
| model.AddBoolOr([ | |
| high_speed.Not(), | |
| high_elevation.Not(), | |
| camera_on.Not() | |
| ]) | |
| # 4. Example plan to test (1 = True, 0 = False) | |
| plan = { | |
| 'high_speed': 1, | |
| 'high_elevation': 1, | |
| 'gps_on': 1, | |
| 'camera_on': 0 | |
| } | |
| # Apply the plan as constraints | |
| model.Add(high_speed == plan['high_speed']) | |
| model.Add(high_elevation == plan['high_elevation']) | |
| model.Add(gps_on == plan['gps_on']) | |
| model.Add(camera_on == plan['camera_on']) | |
| # 5. Solve the model to check feasibility | |
| solver = cp_model.CpSolver() | |
| status = solver.Solve(model) | |
| if status == cp_model.FEASIBLE or status == cp_model.OPTIMAL: | |
| print("Plan is valid") | |
| else: | |
| print("Plan is invalid") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment