Resources
Article

MPS-Dateien aus Ihrem OR-Tool für den HybridSolver exportieren - Anleitung und Tipps

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

Jul 26, 2024
5
min read

Testen Sie selbst - Benchmarken Sie die Leistung des HybridSolvers völlig kostenlos. Bringen Sie einfach Ihre MIP-, LP- oder QUBO-Modelle im MPS-, QUBO- oder LP-Dateiformat mit. Lösen Sie das exportierte Modell mit einem kostenlosen Konto über unsere Plattform und genießen Sie monatlich 20 kostenlose Jobs. Wenn Sie auf Probleme stoßen oder mehr Rechenleistung benötigen, helfen wir Ihnen gerne weiter unter support@quantagonia.com.

In unserem Workflow bevorzugen wir die Präzision von MPS-Dateien gegenüber anderen Dateiformaten. Beachten Sie, dass das MPS-Format nicht vollständig standardisiert ist und es daher zu Problemen kommen kann. Wenn Sie auf Fehler stoßen, überprüfen Sie die Formatierung der Beispieldateien und unsere Dokumentation. Zögern Sie nicht, uns unter support@quantagonia.com zu kontaktieren.

MPS-Dateien aus IBM CPLEX exportieren

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")

MPS-Dateien aus Gurobi exportieren

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');

MPS-Dateien aus FICO Xpress exportieren

Je nach Ihrer OBJSENSE-Einstellung möchten Sie minimieren oder maximieren. Für FICO Xpress müssen Sie dies in Ihrem Export angeben.

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)

MPS-Dateien aus lp_solve exportieren

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

MPS-Dateien aus GLPK exportieren

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

MPS-Dateien aus SCIP exportieren

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

MPS-Dateien aus OR-Tools exportieren

Um die Kompatibilität mit dem HybridSolver sicherzustellen, verwenden Sie die unten definierten benutzerdefinierten Funktionen.

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

Deklarieren Sie die jeweilige benutzerdefinierte Funktion vor dem Aufruf der Exportfunktion.

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.