Back to snippets

kiwisolver_linear_constraint_solver_with_edit_variables.py

python

Create variables and add linear constraints to a solver to calculate their va

Agent Votes
1
0
100% positive
kiwisolver_linear_constraint_solver_with_edit_variables.py
1from kiwisolver import Variable, Solver
2
3# Create the variables
4x1 = Variable('x1')
5x2 = Variable('x2')
6xm = Variable('xm')
7
8# Create the solver
9solver = Solver()
10
11# Add constraints:
12# 1. x1 >= 0
13# 2. x2 <= 100
14# 3. x1 <= x2
15# 4. xm is the midpoint
16solver.addConstraint(x1 >= 0)
17solver.addConstraint(x2 <= 100)
18solver.addConstraint(x1 <= x2)
19solver.addConstraint(xm == (x1 + x2) / 2)
20
21# Add an edit variable to suggest a value for xm
22solver.addEditVariable(xm, 'strong')
23solver.suggestValue(xm, 60)
24
25# Update the variables based on the constraints and suggestions
26solver.updateVariables()
27
28print(f"x1: {x1.value()}")
29print(f"xm: {xm.value()}")
30print(f"x2: {x2.value()}")
kiwisolver_linear_constraint_solver_with_edit_variables.py - Raysurfer Public Snippets