|
| 1 | +// ============================================================================== |
| 2 | +// |
| 3 | +// Copyright (c) 2026- |
| 4 | +// Authors: |
| 5 | +// * Dave Parker <david.parker@comlab.ox.ac.uk> (University of Oxford) |
| 6 | +// |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// |
| 9 | +// This file is part of PRISM. |
| 10 | +// |
| 11 | +// PRISM is free software; you can redistribute it and/or modify |
| 12 | +// it under the terms of the GNU General Public License as published by |
| 13 | +// the Free Software Foundation; either version 2 of the License, or |
| 14 | +// (at your option) any later version. |
| 15 | +// |
| 16 | +// PRISM is distributed in the hope that it will be useful, |
| 17 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +// GNU General Public License for more details. |
| 20 | +// |
| 21 | +// You should have received a copy of the GNU General Public License |
| 22 | +// along with PRISM; if not, write to the Free Software Foundation, |
| 23 | +// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | +// |
| 25 | +// ============================================================================== |
| 26 | + |
| 27 | +package solver; |
| 28 | + |
| 29 | +import com.gurobi.gurobi.GRB; |
| 30 | +import com.gurobi.gurobi.GRBEnv; |
| 31 | +import com.gurobi.gurobi.GRBException; |
| 32 | +import com.gurobi.gurobi.GRBLinExpr; |
| 33 | +import com.gurobi.gurobi.GRBModel; |
| 34 | +import com.gurobi.gurobi.GRBVar; |
| 35 | +import prism.PrismException; |
| 36 | + |
| 37 | +/** |
| 38 | + * LP solver backend backed by Gurobi. |
| 39 | + */ |
| 40 | +public class GurobiSolver implements LPSolver |
| 41 | +{ |
| 42 | + private final GRBEnv env; |
| 43 | + private final GRBModel model; |
| 44 | + private final GRBVar[] xVars; |
| 45 | + public static final String ID = "gurobi"; |
| 46 | + public static final String DISPLAY_NAME = "Gurobi"; |
| 47 | + |
| 48 | + @Override public String getId() { return ID; } |
| 49 | + @Override public String getDisplayName() { return DISPLAY_NAME; } |
| 50 | + |
| 51 | + private int varCount = 0; |
| 52 | + |
| 53 | + public GurobiSolver(int numVars) throws PrismException |
| 54 | + { |
| 55 | + xVars = new GRBVar[numVars]; |
| 56 | + try { |
| 57 | + // Empty env suppresses console and file logging |
| 58 | + env = new GRBEnv(true); |
| 59 | + env.set(GRB.IntParam.OutputFlag, 0); |
| 60 | + env.start(); |
| 61 | + model = new GRBModel(env); |
| 62 | + } catch (GRBException e) { |
| 63 | + throw new PrismException("Error initialising Gurobi: " + e.getMessage()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void addVar(double lb, double ub, double objCoeff) throws PrismException |
| 69 | + { |
| 70 | + try { |
| 71 | + double gurobiUb = Double.isInfinite(ub) ? GRB.INFINITY : ub; |
| 72 | + xVars[varCount] = model.addVar(lb, gurobiUb, objCoeff, GRB.CONTINUOUS, null); |
| 73 | + } catch (GRBException e) { |
| 74 | + throw new PrismException("Error adding Gurobi variable: " + e.getMessage()); |
| 75 | + } |
| 76 | + varCount++; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void addConstraint(int count, double[] coeffs, int[] vars, char sense, double rhs) throws PrismException |
| 81 | + { |
| 82 | + char grbiSense; |
| 83 | + switch (sense) { |
| 84 | + case '=': grbiSense = GRB.EQUAL; break; |
| 85 | + case '<': grbiSense = GRB.LESS_EQUAL; break; |
| 86 | + case '>': grbiSense = GRB.GREATER_EQUAL; break; |
| 87 | + default: throw new PrismException("Unknown LP constraint sense: " + sense); |
| 88 | + } |
| 89 | + try { |
| 90 | + GRBLinExpr expr = new GRBLinExpr(); |
| 91 | + for (int j = 0; j < count; j++) { |
| 92 | + expr.addTerm(coeffs[j], xVars[vars[j]]); |
| 93 | + } |
| 94 | + model.addConstr(expr, grbiSense, rhs, null); |
| 95 | + } catch (GRBException e) { |
| 96 | + throw new PrismException("Error adding Gurobi constraint: " + e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public double[] solve(boolean maximize) throws PrismException |
| 102 | + { |
| 103 | + try { |
| 104 | + model.set(GRB.IntAttr.ModelSense, maximize ? GRB.MAXIMIZE : GRB.MINIMIZE); |
| 105 | + model.optimize(); |
| 106 | + int status = model.get(GRB.IntAttr.Status); |
| 107 | + if (status == GRB.Status.OPTIMAL) { |
| 108 | + double[] soln = new double[varCount]; |
| 109 | + for (int s = 0; s < varCount; s++) { |
| 110 | + soln[s] = xVars[s].get(GRB.DoubleAttr.X); |
| 111 | + } |
| 112 | + return soln; |
| 113 | + } else { |
| 114 | + String detail = status == GRB.Status.INFEASIBLE ? " (infeasible)" : status == GRB.Status.UNBOUNDED ? " (unbounded)" : ""; |
| 115 | + throw new PrismException("Error solving LP" + detail); |
| 116 | + } |
| 117 | + } catch (GRBException e) { |
| 118 | + throw new PrismException("Error solving LP: " + e.getMessage()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void dispose() |
| 124 | + { |
| 125 | + try { |
| 126 | + model.dispose(); |
| 127 | + env.dispose(); |
| 128 | + } catch (GRBException e) { |
| 129 | + // Suppress — nothing useful can be done during cleanup |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments