I don't normally do Python, but...
Needs must, as all the best cool kids are making Constraints Solvers in Python. Shall I rewrite Practical Python AI Projects: Mathematical Models of Optimization Problems in Ruby? Might be funs.
Anyway, Chapter 1 introduced a simple solver, but this is even simpler.
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
#!/usr/local/bin/python3 | |
from __future__ import print_function | |
from ortools.linear_solver import pywraplp | |
t = 'WhatIsX' | |
s = pywraplp.Solver(t, pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) | |
x = s.NumVar(0, 1000, 'x') | |
s.Add(x + 2*x + 3*x + 4*x == 10) # it's almost algebraic syntax | |
s.Solve() | |
print(x.SolutionValue()) |