SciPy
Provides fundamental algorithms for scientific computing.
- Downloads / mo
- 392.1M
- Stars
- 15.2K
- Last release
- 1.18.1
- License
- BSD-3-Clause
Quickstart
from scipy.optimize import linprog result = linprog([-1.0], bounds=[(0.0, 1.0)], method='highs') print(result.message)
SciPy examples
def solve_via_data(self, data, warm_start: bool, verbose: bool, solver_opts, solver_cache=None): from scipy import optimize as opt # Set default method which can be overridden by user inputs. meth = "highs" # Check if the problem is a MIP. problem_is_a_mip = data[s.BOOL_IDX] or data[s.INT_IDX] # Build variable bounds from BOUNDED_VARIABLES data n = data[s.C].shape[0] lb = data.get(s.LOWER_BOUNDS) ub = data.get(s.UPPER_BOUNDS) # Track variable integrality options. An entry of zero implies that # the variable is continuous. An entry of one implies that the # variable is either binary or an integer with bounds. if problem_is_a_mip: integrality = [0] * n for index in data[s.BOOL_IDX] + data[s.INT_IDX]: integrality[index] = 1 bounds = [( lb[i] if lb is not None else None, ub[i] if ub is not None else None, ) for i in range(n)] for index in data[s.BOOL_IDX]: cur_lb, cur_ub = bounds[index] new_lb = max(0, cur_lb) if cur_lb is not None else 0 new_ub = min(1, cur_ub) if cur_ub is not None else 1 bounds[index] = (new_lb, new_ub) else: integrality = None if lb is not None or ub is not None: bounds = [( lb[i] if lb is not None else None, ub[i] if ub is not None else None, ) for i in range(n)] else: bounds = (None, None) # Extract solver options which are not part of the options dictionary if solver_opts: # Raise error message if the parameters are not passed in # a dictionary called 'scipy_options'. if "scipy_options" not in solver_opts: raise ValueError("All parameters for the SCIPY solver should " "be encased within a dictionary called " "scipy_options e.g. \n" "prob.solve(solver='SCIPY', verbose=True," " scipy_options={'method':'highs-ds', 'maxiter':10000})") if "method" in solver_opts["scipy_options"]: meth = solver_opts["scipy_options"].pop("method") # Disable the 'bounds' parameter to avoid problems with # canonicalised problems. if "bounds" in solver_opts['scipy_options']: raise ValueError("Please do not specify bounds through " "scipy_options. Please specify bounds " "through CVXPY.") # Check that the 'integrality' parameter isn't specified. if "integrality" in solver_opts['scipy_options']: raise ValueError("Please do not specify variable integrality through " "scipy_options. Please specify variable types " "through CVXPY.") # Check for SciPy method support (i.e., only 'highs' supports MIP models). method_supports_mip = meth == 'highs' if problem_is_a_mip and not method_supports_mip: raise ValueError("Only the 'highs' SciPy method can solve MIP models.") # Not supported by HiGHS solvers: # callback = solver_opts['scipy_options'].pop("callback", None) # x0 = solver_opts['scipy_options'].pop("x0", None) else: # Instantiate an empty `scipy_options` entry. solver_opts['scipy_options'] = {} if problem_is_a_mip: constraints = [] G = data[s.G] if G is not None: ineq = scipy.optimize.LinearConstraint(G, ub=data[s.H]) # ensure that index arrays are int32 ineq.A.indptr = ineq.A.indptr.astype(np.int32) ineq.A.indices = ineq.A.indices.astype(np.int32) constraints.append(ineq) A = data[s.A] if A is not None: eq = scipy.optimize.LinearConstraint(A,data[s.B], data[s.B]) constraints.append(eq) lb = [t[0] if t[0] is not None else -np.inf for t in bounds] ub = [t[1] if t[1] is not None else np.inf for t in bounds] bounds = scipy.optimize.Bounds(lb, ub) solution = opt.milp(data[s.C], constraints=constraints, options=solver_opts['scipy_options'], integrality=integrality, bounds=bounds) else: solution = opt.linprog(data[s.C], A_ub=data[s.G], b_ub=data[s.H], A_eq=data[s.A], b_eq=data[s.B], method=meth, options=solver_opts['scipy_options'], bounds=bounds) # Replace `scipy_options` method to avoid future warnings. solver_opts["scipy_options"]["method"] = meth if verbose is True: print("Solver terminated with message: " + solution.message) return solution