Skip to content

Add support for quadratic objectives in convex QP problems#43

Merged
wspringer merged 4 commits intomainfrom
feature/quadratic-objectives-26
Jun 17, 2025
Merged

Add support for quadratic objectives in convex QP problems#43
wspringer merged 4 commits intomainfrom
feature/quadratic-objectives-26

Conversation

@wspringer
Copy link
Owner

Summary

This PR implements support for quadratic programming (QP) problems in the HiGHS MCP server, enabling optimization of quadratic objectives of the form minimize c^T x + 0.5 x^T Q x.

Fixes #26

Changes

Schema Enhancement

  • Added QuadraticObjectiveSchema supporting both dense and sparse matrix formats
  • Made linear coefficients optional in ObjectiveSchema (required if quadratic is not provided)
  • Added validation to ensure Q matrix is square and properly dimensioned
  • Added check to prevent MIQP (quadratic with integer variables)

Implementation

  • encode.ts: Extended formatObjective() to generate HiGHS LP format quadratic terms using [ quadratic_terms ] / 2 syntax
  • decode.ts: Updated to use variables.length instead of objective.linear.length for determining variable count
  • Properly handles symmetric matrices in both sparse and dense formats

Testing

  • Added comprehensive unit tests for schema validation and encoding (tests/quadratic-objectives.test.ts)
  • Added integration tests with actual HiGHS solver (tests/integration/quadratic-simple.test.ts)
  • All existing tests continue to pass

Documentation

  • Updated README with QP example (portfolio optimization)
  • Added notes about QP limitations (convex only, continuous variables only)
  • Updated API schema documentation

Key Areas for Review

  1. Quadratic Term Format: The implementation follows HiGHS LP format for quadratic terms. Please verify the encoding in encode.ts lines 135-193, particularly:

    • Handling of symmetric matrices (summing off-diagonal entries for dense format)
    • Proper formatting of terms like x1^2 and x1 * x2
  2. Validation Logic: Check the schema refinements in schemas.ts:

    • Lines 78-99: Ensuring at least one of linear/quadratic is provided
    • Lines 204-212: Preventing MIQP (quadratic with integer variables)
  3. Matrix Specification: The Q matrix values need to be doubled to account for the 0.5 factor in the objective. This is documented but may need emphasis.

Test Results

All tests pass including the new QP-specific tests:

  • Schema validation for dense/sparse formats
  • Encoding to proper LP format
  • Integration tests solving actual QP problems
  • Portfolio optimization example

The implementation is backward compatible - all existing LP and MIP functionality remains unchanged.

wspringer and others added 2 commits June 17, 2025 11:04
Implemented quadratic objective support for convex quadratic programming (QP)
problems in HiGHS MCP server. The enhancement allows users to minimize
quadratic objectives of the form: minimize c^T x + 0.5 x^T Q x, where Q
must be positive semidefinite.

Key features:
- Support for both dense and sparse quadratic matrix formats
- Validation to ensure Q is square and symmetric
- Restriction to continuous variables only (no MIQP support)
- Proper encoding to HiGHS LP format with quadratic terms
- Comprehensive test coverage for various QP scenarios

The implementation uses a discriminated union schema to provide type-safe
separation between sparse and dense formats, ensuring proper validation
and clear error messages.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@wspringer wspringer linked an issue Jun 17, 2025 that may be closed by this pull request
9 tasks
Updated the MCP tool description to explicitly mention quadratic programming
limitations so LLMs understand the constraints:
- QP only supports continuous variables (no MIQP)
- Q matrix must be positive semidefinite (convex only)
- Matrix values should be doubled for 0.5 factor

Added test assertions to verify the description includes QP information.
Changed quadratic objectives from discriminated union to property-based
format consistent with constraint matrices:

Before (discriminated union):
{
  format: 'dense',
  matrix: [[...]]
}

After (property-based like constraints):
{
  dense: [[...]]  // OR
  sparse: { rows: [...], cols: [...], values: [...], shape: [...] }
}

This provides a consistent API pattern across all matrix specifications
in the schema and aligns with existing constraint matrix design.

Updated all tests and documentation to reflect the new format.
@wspringer wspringer merged commit d72bb33 into main Jun 17, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support quadratic objectives for convex QP problems

1 participant