Skip to main content
Glama

solve_cvxpy_problem

Solve CVXPY optimization problems by defining variables, objectives, and constraints. Returns solutions for problems like minimizing ||Ax - b||₂² subject to bounds 0 ≤ x ≤ 1.

Instructions

Solve a CVXPY optimization problem.

This tool takes a CVXPY optimization problem defined with variables, objective, and constraints, and returns a solution if one exists. Example: Solve the following problem: minimize ||Ax - b||₂² subject to: 0 ≤ x ≤ 1 where A = [1.0, -0.5; 0.5, 2.0; 0.0, 1.0] and b = [2.0, 1.0, -1.0] Should be this tool call: simple_cvxpy_solver( variables=[{"name": "x", "shape": 2}], objective_type="minimize", objective_expr="cp.sum_squares(np.array(A) @ x - np.array(b))", constraints=["x >= 0", "x <= 1"], parameters={"A": [[1.0, -0.5], [0.5, 2.0], [0.0, 1.0]], "b": [2.0, 1.0, -1.0]} ) Args: problem: The problem definition with variables, objective, and constraints Returns: A list of TextContent containing the solution or an error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
problemYes

Implementation Reference

  • MCP tool handler for 'solve_cvxpy_problem': receives CVXPYProblem input, delegates to core solver, formats and returns JSON result as TextContent.
    @app.tool("solve_cvxpy_problem") async def solve_cvxpy_problem_tool(problem: CVXPYProblem) -> list[TextContent]: """Solve a CVXPY optimization problem. This tool takes a CVXPY optimization problem defined with variables, objective, and constraints, and returns a solution if one exists. Example: Solve the following problem: minimize ||Ax - b||₂² subject to: 0 ≤ x ≤ 1 where A = [1.0, -0.5; 0.5, 2.0; 0.0, 1.0] and b = [2.0, 1.0, -1.0] Should be this tool call: simple_cvxpy_solver( variables=[{"name": "x", "shape": 2}], objective_type="minimize", objective_expr="cp.sum_squares(np.array(A) @ x - np.array(b))", constraints=["x >= 0", "x <= 1"], parameters={"A": [[1.0, -0.5], [0.5, 2.0], [0.0, 1.0]], "b": [2.0, 1.0, -1.0]} ) Args: problem: The problem definition with variables, objective, and constraints Returns: A list of TextContent containing the solution or an error message """ result = solve_cvxpy_problem(problem) match result: case Success(solution): return [ TextContent( type="text", text=json.dumps( { "values": { k: v.tolist() if hasattr(v, "tolist") else v for k, v in solution.values.items() }, "objective_value": solution.objective_value, "status": solution.status, "dual_values": { k: v.tolist() if hasattr(v, "tolist") else v for k, v in (solution.dual_values or {}).items() }, } ), ) ] case Failure(error): return [TextContent(type="text", text=f"Error solving problem: {error}")] case _: return [ TextContent( type="text", text="Unexpected error in solve_cvxpy_problem_tool", ) ]
  • Core implementation of CVXPY problem solving: creates variables, parses objective and constraints, builds and solves CVXPY Problem, extracts solution values and duals.
    def solve_cvxpy_problem(problem: CVXPYProblem) -> Result[CVXPYSolution, str]: """Solve a CVXPY optimization problem. Args: problem: The problem definition Returns: Result containing a CVXPYSolution or an error message """ try: # Create variables variables: dict[str, cp.Variable] = {} for var in problem.variables: variables[var.name] = create_variable(var.name, var.shape) # Parse objective objective_expr = parse_expression( problem.objective.expression, variables, problem.parameters ) objective = ( cp.Minimize(objective_expr) if problem.objective.type == ObjectiveType.MINIMIZE else cp.Maximize(objective_expr) ) # Parse constraints constraints = [] for _i, constraint in enumerate(problem.constraints): constraint_expr = parse_expression( constraint.expression, variables, problem.parameters ) constraints.append(constraint_expr) # Create and solve the problem prob = cp.Problem(objective, constraints) result = prob.solve() # Extract solution values = {name: var.value for name, var in variables.items()} dual_values = { i: constraint.dual_value for i, constraint in enumerate(constraints) if hasattr(constraint, "dual_value") and constraint.dual_value is not None } return Success( CVXPYSolution( values=values, objective_value=float(result) if result is not None else None, status=prob.status, dual_values=dual_values if dual_values else None, ) ) except Exception as e: return Failure(f"Error solving CVXPY problem: {e!s}")
  • Pydantic BaseModel defining the input schema for CVXPY problems: lists of variables/constraints, objective, parameters dict, and description.
    class CVXPYProblem(BaseModel): """Model representing a complete CVXPY optimization problem.""" variables: list[CVXPYVariable] objective: CVXPYObjective constraints: list[CVXPYConstraint] parameters: dict[str, Any] = {} # For A, b, etc. description: str = ""
  • Helper function to create CVXPY Variable instances from name and shape.
    def create_variable(name: str, shape: int | tuple[int, ...]) -> cp.Variable: """Create a CVXPY variable with the given shape. Args: name: Name of the variable shape: Shape of the variable (int for scalar, tuple for vector/matrix) Returns: CVXPY variable """ return cp.Variable(shape, name=name)
  • Helper function to safely parse and evaluate string expressions into CVXPY expressions using provided variables and parameters.
    def parse_expression( expr_str: str, variables: dict[str, cp.Variable], params: dict[str, Any] ) -> CVXPYExpr: """Parse a CVXPY expression string. Args: expr_str: String representation of the expression variables: Dictionary of variable names to CVXPY variables params: Dictionary of parameter names to values Returns: Parsed CVXPY expression """ # Create a local dictionary with variables and parameters local_dict = { **variables, **params, "cp": cp, "np": np, } # Evaluate the expression in the context of the local dictionary return eval(expr_str, {"__builtins__": {}}, local_dict)

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sdiehl/usolver'

If you have feedback or need assistance with the MCP directory API, please join our Discord server