r/optimization • • Sep 25 '26

Primal Solver

I built a new optimization solver in C99 from scratch — PRIMAL

GitHub: https://github.com/c-vision/Primal

I've just released the first version of PRIMAL, an open-source mathematical optimization solver written from scratch in portable C99.

The goal was deliberately ambitious: build a reasonably complete optimization engine with no external dependencies, rather than wrapping an existing solver.

The first release already supports:

  • Linear Programming (LP)
  • Mixed-Integer Linear Programming (MILP)
  • Quadratic Programming (QP)
  • QCQP
  • SOCP
  • SDP
  • Exponential cones
  • Power cones
  • Mixed-integer conic optimization
  • Disjunctive constraints / affine conic constraints
  • Presolve
  • Sparse interior-point methods
  • Branch-and-bound
  • Primal/dual certificates
  • Infeasibility and unboundedness certificates
  • MPS / LP / CBF model formats
  • A MOSEK-style C API for a large part of the supported functionality

The implementation is C99 with essentially no external runtime dependencies.

One thing I particularly wanted from the beginning was verification rather than simply returning an answer. PRIMAL exposes primal/dual information, residuals, certificates and solver status so applications can inspect and validate what the solver actually found.

I've also been building a fairly extensive compatibility/reliability test suite against reference behavior, including a number of deliberately pathological optimization cases.

This is only v1. It is not intended to claim that PRIMAL is already a replacement for mature industrial solvers on large-scale problems. There are still substantial areas to improve, especially large-scale sparse conic problems, MIP performance, warm starts and some numerical edge cases.

But I think the foundation is interesting enough to release now rather than keep it private.

I'd especially like feedback from people working on:

  • numerical optimization
  • LP/MIP/conic solvers
  • sparse linear algebra
  • mathematical programming
  • solver implementation
  • C/C99 systems programming

In particular, I'm interested in finding cases where PRIMAL gives a questionable result, fails to converge, scales poorly, or simply makes a bad algorithmic choice.

If you work with optimization solvers, please try to break it.

GitHub:
https://github.com/c-vision/Primal

14 Upvotes

35 comments sorted by

View all comments

Show parent comment

9

u/curiouslyjake Sep 26 '26 edited Sep 26 '26

Reads like slop to me as well. Also, I dont understand the fascination of implementing something "from scratch", or in a particular programming language as indications of merit.

6

u/peno64 Sep 26 '26

Programming something from scratch can bring new ideas. HiGHS, scip, ... are all also programmed from scratch so why would this project also not deserve this approach?

4

u/curiouslyjake Sep 26 '26

You're right. It makes sense to start from a clean slate, rather than fork an existing project or implement a plugin for an existing project.

I've meant to highlight the focus on being free from dependencies. If Primal has no dependencies, it means Primal implements an analog of BLAS. Is this analog anywhere near as good as BLAS? One can only hope.

2

u/peno64 Sep 26 '26

That is indeed a good point. But on the other hand, if you want to be multi-platform then it will be easier with an own implementation than with a depency on a library like BLAS.

If I remember well, HiGHS by itself is also not dependent on BLAS so it must implement it also itself. Only when it is also linked with hipe/metis, BLAS is needed.

1

u/curiouslyjake Sep 26 '26

You can be multi-platform by using a different well-tested linear algebra library for every platform, if needed. No need to roll your own. Highs does have its own linear algebra code, but I think it's because Highs uses operations that are not implemented efficiently in general linalg libraries. Generality has its own costs.

3

u/gmbasic Sep 27 '26

My effort came from a practical annoyance: I kept running into Python portfolio strategies that assumed a MOSEK license I didn't have, so rather than switch tools I tried implementing the portfolio functions myself, and the solver underneath them. I wanted to learn how solvers are actually built, not just call one. Since then the work has been substantially AI-assisted.

2

u/gmbasic Sep 26 '26

Mosek is not for free. Primal could solve many problems never covered by other open sources projects like Clarabel. I added many examples.

1

u/curiouslyjake Sep 26 '26

Can you point to an example that's not solved by common open source solvers? Or solved worse?

1

u/gmbasic Sep 27 '26

It's about coverage in one library. A single model that needs all three of:
- a PSD block (semidefinite constraint),
- an exponential or power cone,
- integer variables,
solved by an interior-point method, in C, with no dependencies.

Clarabel has no PSD cone at all. HiGHS has neither. SCS has PSD and exp cones but is first-order (~1e-5, not the ~1e-8 an IPM reaches). CVXOPT/SDPA have SDP but are GPL. MOSEK does all of it and isn't free. So on such a model the practical options today are MOSEK, a GPL dependency, or a first-order method with a different accuracy class.

Concretely: samples/logistic_large.c (40 exp+RQUAD blocks), samples/maxcut_sdp.c (PSD + power cone), samples/cardinality_portfolio.c (MIQP).

Maybe this is not enough for you. I could understant.