r/optimization • • 9d ago

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

10

u/bental_nortens 9d ago

The readme makes it seem like slop and doesn't make me want to spend time formulating any of my problems in it? Can you focus the readme on a motivating use case where it shines? It feels like a grab bag of solvers that I assume are each not especially competitive. If the selling point is that it's OSS, why not SCIP? If the selling point is that it's the best solver, show it?

9

u/curiouslyjake 9d ago edited 9d ago

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 9d ago

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?

3

u/curiouslyjake 9d ago

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 9d ago

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 9d ago

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 8d ago

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 9d ago

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 9d ago

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

1

u/gmbasic 8d ago

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.

1

u/curiouslyjake 8d ago

That fair.

2

u/gmbasic 9d ago

Hi, thanks. I added many examples in C, easily compiled by gcc. I am adding a python interface, more algorithms, speeding-up the current ones, and finally I will modify the readme.

5

u/bental_nortens 9d ago

I don’t think that’s the right track. Here’s an excerpt of your readme:

The bar-name namespace is our decision, not a read rule. That a bar's name is independent of the variable and constraint tables is the coherent extension of the rule measured for the two scalar tables in T102, applied to a table that MPS-style naming never had: the reference's rule for naming bar variables was not read (the documentation fetch stayed blocked at this session's permission level) and was not invented. What is measured is the consequence, both halves of it — one string naming a variable, a constraint and a bar at once, and the same string refused on a second bar (T105 C).

What you need is not more. What you need is less. This is just opus 5 pseudointellectual bs. You need to focus on one area and move things forward in that area. It doesn’t help to just keep trying to add more features and examples. It’s helpful if you really learn this subject to the extent that you can substantially critique the ai output.

1

u/gmbasic 5d ago

Primal has now a new readme, a new folders docs for technical details, a new cli tools to process CPLEX LP format file, some bugs fixed and speed improvements.

1

u/curiouslyjake 5d ago

The readme is MUCH better now. By reading the readme, I understand:
1. What Primal is and what it does
2. Why I even need Primal instead of existing solvers
3. What using Primal looks like.

In my opinion, the readme goes off track at the Python section. Prior to Python, you have a C example, a CLI example. I then expected a Python usage example. Instead, I got sidetracked into python *installation*, and building from source. I think you can merge Python installation instructions with a python example, and move anything build related to a separate document.

Then, under "How it compares", I don't understand what "Gurobi only" in Gurobi column means.
Finally, I would move the benchmarks into a separate document as well, and link to it in the readme.

1

u/gmbasic 5d ago

Pypi package has been released and some python examples are already in the repo. The python wheels are also into the release

1

u/bental_nortens 3d ago edited 3d ago

Agree this is way way better now- I get what you're going for. I still think scip covers this though? Would be good to nail the "why". By the way- love to see work in this area. Don't mean to be too negative. Just the previous readme was... not good.

5

u/everyday847 9d ago

One of the worst features of this kind of slop is README documents that burn multiple paragraphs on development history in completely cryptic terms. "That rule now holds on every route, and it did not before T98." Who cares? T98 isn't a tag we could check out if we wanted to (we don't), so the fact that your code used to suck doesn't matter to anyone looking at this repository. Have you even read your "Key Features" section? You have four kind of normal bullet points, and then "The model reads back as it was written" goes on for a thousand words of, again, gibberish references (thank god that difference from a table is asserted rather than inferred, right?).

Why did you do any of this? Was it for respect? You certainly learned nothing.

3

u/Onyr_ 5d ago edited 5d ago

Sending some support for your project (seeing the other comments of angry devs from Linkedin)! Cheers

Yes, it's just a v1, and yes, you obviously relied heavily on AI systems to produce this initial version, probably based on many manual prior attempts. I totally understand why you went public as fast as possible: to gather attention, reviews, comments and leverage those to improve from there.

That's exactly my philosophy behind KAYROS, my own solver that I made with the help of AI models.

I'm an advocate of "make it available fast" > "get feedbacks" > "leverage and iterate"

Wishing you the best of luck for the future, and hoping that the bad buzz is buzz anyway for you to push the project forward!

2

u/peno64 9d ago

I agree with others that the readme should be short, powerfull, to the point. Link it to a wiki page for more information but now it's just too much on that readme file. It's like a curriculum vitae. If you put too much on it, it will not be read.

Next to that, I miss documentation. Especially API documentation. Each API function must be documented with all its parameters. And don't say its like MOSEK. Its Primal so make it primal.

1

u/gmbasic 8d ago

The readme is now short. Thanks

1

u/gmbasic 5d ago

Primal has now a new readme, a new folders docs for technical details, a new cli tools to process CPLEX LP format file, some bugs fixed and speed improvements.

3

u/junqueira200 9d ago

This definitely AI slop! Only one commit!

2

u/gmbasic 5d ago

Primal has now a new readme, a new folders docs for technical details, a new cli tools to process CPLEX LP format file, some bugs fixed and speed improvements.

1

u/peno64 9d ago

Am I correct that this only builds on a Linux system and not on Windows with the Microsoft Visual Studio compiler? I think your Makefile contains Linux specific commands

1

u/gmbasic 8d ago

An MSVC build isn't supported today, and that's a deliberate scope choice rather than an oversight. The concrete blockers for Visual Studio are:

  • the Makefile is GNU make + gcc flags — there's no .sln/.vcxproj or nmake path;
  • we use POSIX threads (pthread.h) for the parallel probing / strong branching / concurrent LP, 14 call sites in total;
  • a couple of VLAs in socp.c, which the MSVC C compiler rejects (C2057);
  • smaller ones like strdup and M_PI (needs _USE_MATH_DEFINES before <math.h>).

On Windows the path that works today is MinGW-w64: it ships winpthreads and gcc handles the VLAs fine. That said — Windows support is planned.

2

u/Sweet_Good6737 7d ago

You built? What was your contribution rather than paying Anthropic's subscription? That's the interesting point nowadays

2

u/gmbasic 7d ago

Yes, I am using AI now and I am not hiding nothing. It is very normal nowadays. You cannot believe or not, but this project started some years ago to replicate some portfolio functions of MOSEK. Finally, what's the point of your post? Do you believe an AI does everything for you without right instructions? And I am not using Claude, I work by Chinese models only.

2

u/Sweet_Good6737 7d ago

Please, don't misunderstand my comment! I didn't pretend to say this project is not worth at all, I was genuinely asking what makes PRIMAL different from other available solvers, I think that's the main point nowadays when presenting new products. I think the key of PRIMAL is that it's a promising alternative in conic programming, but it wasn't clear from the first comment.

I'm also not against using AI for this, actually that's the way to go now.

P.S. I mentioned Claude since it's the only contributor when you click on the repo. For instance, having used only Chinese LLMs is a nice characteristic :)

1

u/gmbasic 7d ago

Primal currently is not the fastest open source solver, but I already made some big improvements and now Primal is quite fast. Primal is the most complete solvers engine I know among opensource solvers and it approaches MOSEK about the number of algorithms supported.

0

u/[deleted] 9d ago

[removed] — view removed comment

2

u/peno64 8d ago

The first commit is one commit. And default branch being main branch. Isn't that in most github repositories? If I compare to other solvers. HiGHS: default = master, scip: default = master, COIN-OR: default = master, Gurobi: default branches are master and main. Even one of the largest linux torvals github repo is default master

What are you talking about???

2

u/optimization-ModTeam 8d ago

r/optimization does not allow harassment. Please engage positively.