Resources
Article

How to export MPS files from your existing OR tool for the HybridSolver

Quantagonia macht alte Software fit für Quantencomputer. Würde Frank Thelen investieren?

Jul 26, 2024
5
min read

Test it yourself - Benchmark the HybridSolver’s performance entirely for free. All you have to do is bring your MIP, LP, QUBO models in MPS, QUBO or LP file format. Submit the exported model with a free account via our platform and enjoy 20 Jobs monthly for free. If you encounter issues or need more computing power, we are happy to help through support@quantagonia.com.

In our workflow, we prefer the precision of MPS files over any other file types. Note that the MPS format is not completely standardized, and you might run into issues. If you encounter errors, check the formatting of the example files and our documentation. Do not hesitate to initiate contact with us at support@quantagonia.com.

Export MPS from IBM CPLEX

C#	|	cplex.ExportModel("model.mps");
C++	|	cplex.exportModel("model.mps");
Java	|	cplex.exportModel("model.mps");
Python	|	cpx.write("model.mps")
Shell	|	write model.mps
MATLAB	|	cplex.write("model.mps")

Export MPS from Gurobi

C	|	GRBwrite(model, 'model.mps');
C#	|	GRBModel.write('model.mps');
C++	|	GRBModel::write('model.mps');
Java	|	GRBModel.write('model.mps')
MATLAB	|	gurobi_write(model, 'mymodel.mps');
Python	|	model.write('model.mps')
R	|	gurobi_write(model, 'model.mps');

Export MPS from FICO Xpress

Depending on your OBJSENSE setting you want to Minimize, or Maximize. For FICO Xpress you have to specify that in your export.

Mosel	|	exportprob(EP_MIN+EP_MPS+EP_STRIP, 'model.mps', Constraint_to_MIN)
Mosel	|	exportprob(EP_MAX+EP_MPS+EP_STRIP, 'model.mps', Constraint_to_MAX)

Export MPS from lp_solve

C/C++	|	write_mps(lp, 'model.mps');
Python	|	lpsolve(‘write_mps’, lp, 'model.mps')
Matlab	|	mxlpsolve(‘write_mps’, lp, 'model.mps');
Shell	|	generate_model_program | lpsolve -wmps model.mps

Export MPS from GLPK

C++	|	glp_write_mps(lp, GLP_MPS_DECK, NULL, 'model.mps');
Shell	|	glpsol --check --model model.mod --data model.dat --wfreemps model.mps

Export MPS from SCIP

C	|	SCIPwriteOrigProblem(scip, 'model.mps', NULL, 0);
Shell	|	write genproblem model.mps

Export MPS from OR-Tools

To ensure compatibility with the HybridSolver use the custom functions defined below.

C++	|	ExportModelToMPS('model.mps')
Java	|	solver.exportModel('model.mps');
Python	|	export_mip_to_mps(solver, 'model.mps')

Declare the respective custom function before the export function call.

C++

#include "ortools/linear_solver/linear_solver.h"
#include <fstream>
#include <iostream>

void ExportModelToMPS(const operations_research::MPSolver& solver, const std::string& filename) {
    std::ofstream file(filename);
    if (!file) {
        std::cerr << "Could not open the file!" << std::endl;
        return;
    }
    file << "NAME          EXAMPLE\n";
    
    // OBJSENSE
    if (solver.Objective().maximization()) {
        file << "OBJSENSE\n MAX\n";
    } else {
        file << "OBJSENSE\n MIN\n";
    }
    
    file << "ROWS\n";
    file << " N  COST\n";
    for (int i = 0; i < solver.NumConstraints(); ++i) {
        file << " L  c" << i << "\n";
    }
    file << "COLUMNS\n";
    for (int j = 0; j < solver.NumVariables(); ++j) {
        const operations_research::MPVariable* var = solver.variable(j);
        for (int i = 0; i < solver.NumConstraints(); ++i) {
            const operations_research::MPConstraint* constraint = solver.constraint(i);
            double coefficient = constraint->GetCoefficient(var);
            if (coefficient != 0) {
                file << "    " << var->name() << "  c" << i << "  " << coefficient << "\n";
            }
        }
        // Objective function coefficients
        double obj_coefficient = solver.Objective().GetCoefficient(var);
        if (obj_coefficient != 0) {
            file << "    " << var->name() << "  COST  " << obj_coefficient << "\n";
        }
    }
    file << "RHS\n";
    for (int i = 0; i < solver.NumConstraints(); ++i) {
        const operations_research::MPConstraint* constraint = solver.constraint(i);
        file << "    RHS1  c" << i << "  " << constraint->ub() << "\n";
    }
    file << "BOUNDS\n";
    for (int j = 0; j < solver.NumVariables(); ++j) {
        const operations_research::MPVariable* var = solver.variable(j);
        file << " UP BND1  " << var->name() << "  " << var->ub() << "\n";
        file << " LO BND1  " << var->name() << "  " << var->lb() << "\n";
    }
    file << "ENDATA\n";
    file.close();
}

int main() {
    operations_research::MPSolver solver("simple_model", operations_research::MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
    // Define variables, constraints, and objective here
    ExportModelToMPS(solver, "exported_model.mps");
    return 0;
}

Java

import com.google.ortools.linearsolver.MPSolver;
import java.io.FileWriter;
import java.io.IOException;

public class ExportMPS {
    public static void exportModelToMPS(MPSolver solver, String filename) throws IOException {
        FileWriter writer = new FileWriter(filename);
        writer.write("NAME          EXAMPLE\n");
        
        // OBJSENSE
        if (solver.objective().maximization()) {
            writer.write("OBJSENSE\n MAX\n");
        } else {
            writer.write("OBJSENSE\n MIN\n");
        }
        
        writer.write("ROWS\n");
        writer.write(" N  COST\n");
        for (int i = 0; i < solver.constraints().size(); ++i) {
            writer.write(" L  c" + i + "\n");
        }
        writer.write("COLUMNS\n");
        for (int j = 0; j < solver.variables().size(); ++j) {
            MPSolver.Variable var = solver.variables().get(j);
            for (int i = 0; i < solver.constraints().size(); ++i) {
                MPSolver.Constraint constraint = solver.constraints().get(i);
                double coefficient = constraint.getCoefficient(var);
                if (coefficient != 0) {
                    writer.write("    " + var.getName() + "  c" + i + "  " + coefficient + "\n");
                }
            }
            // Objective function coefficients
            double obj_coefficient = solver.objective().getCoefficient(var);
            if (obj_coefficient != 0) {
                writer.write("    " + var.getName() + "  COST  " + obj_coefficient + "\n");
            }
        }
        writer.write("RHS\n");
        for (int i = 0; i < solver.constraints().size(); ++i) {
            MPSolver.Constraint constraint = solver.constraints().get(i);
            writer.write("    RHS1  c" + i + "  " + constraint.getUb() + "\n");
        }
        writer.write("BOUNDS\n");
        for (int j = 0; j < solver.variables().size(); ++j) {
            MPSolver.Variable var = solver.variables().get(j);
            writer.write(" UP BND1  " + var.getName() + "  " + var.getUb() + "\n");
            writer.write(" LO BND1  " + var.getName() + "  " + var.getLb() + "\n");
        }
        writer.write("ENDATA\n");
        writer.close();
    }

    public static void main(String[] args) throws IOException {
        MPSolver solver = new MPSolver("simple_model", MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING);
        // Define variables, constraints, and objective here
        exportModelToMPS(solver, "exported_model.mps");
    }
}

Python

from ortools.linear_solver import pywraplp

def export_mip_to_mps(solver, filename):
    with open(filename, 'w') as file:
        file.write("NAME          EXAMPLE\n")
        
        # OBJSENSE
        if solver.Objective().Maximization():
            file.write("OBJSENSE\n MAX\n")
        else:
            file.write("OBJSENSE\n MIN\n")
        
        file.write("ROWS\n")
        
        # Objective function
        file.write(" N  COST\n")
        for i, constraint in enumerate(solver.constraints()):
            file.write(f" L  c{i}\n")

        file.write("COLUMNS\n")
        for variable in solver.variables():
            for i, constraint in enumerate(solver.constraints()):
                coefficient = constraint.GetCoefficient(variable)
                if coefficient != 0:
                    file.write(f"    {variable.name()}  c{i}  {coefficient}\n")
            # Objective function coefficients
            obj_coefficient = solver.Objective().GetCoefficient(variable)
            if obj_coefficient != 0:
                file.write(f"    {variable.name()}  COST  {obj_coefficient}\n")

        file.write("RHS\n")
        for i, constraint in enumerate(solver.constraints()):
            file.write(f"    RHS1  c{i}  {constraint.ub()}\n")

        file.write("BOUNDS\n")
        for variable in solver.variables():
            file.write(f" UP BND1  {variable.name()}  {variable.ub()}\n")
            file.write(f" LO BND1  {variable.name()}  {variable.lb()}\n")

        file.write("ENDATA\n")
        
# Define variables, constraints, and objective here

export_mip_to_mps(solver, 'exported_model.mps')
Read full article

Quantagonia Application Logo

Want to get entangled or stay up to date?

Let's push the boundaries of technology and create a quantum-powered future.