Search in sources :

Example 1 with NoFeasibleSolutionException

use of org.apache.commons.math3.optim.linear.NoFeasibleSolutionException in project neqsim by equinor.

the class LinearProgrammingChemicalEquilibrium method generateInitialEstimates.

// Method added by Neeraj
/*
     * public double[] generateInitialEstimates(SystemInterface system, double[] bVector, double
     * inertMoles, int phase){ int i,j; double[] n = new double[components.length]; Matrix atemp,
     * btemp; Matrix mutemp = new
     * Matrix(chemRefPot,1).times(1.0/(R*system.getPhase(phase).getTemperature())). copy(); Matrix
     * ntemp; atemp = new Matrix(7,7); btemp = new Matrix(1,7); //for (i=0;i<4;i++) for
     * (i=0;i<5;i++) { for (j=0;j<7;j++) atemp.set(i,j,Amatrix[i][j]); btemp.set(0,i,bVector[i]);
     * 
     * } atemp.set(5,4,1); atemp.set(6,5,1); //atemp.set(4,4,1); //atemp.set(5,5,1);
     * //atemp.set(6,1,1); //atemp.print(5,1); //btemp.print(5,5); //mutemp.print(5,5); ntemp =
     * atemp.solve(btemp.transpose()); ntemp.print(5,5); for (i=0;i<7;i++) n[i] = ntemp.get(i,0);
     * int rank = atemp.rank(); return n;
     * 
     * 
     * }
     */
// Method updated to use Apache Commons Math 3 by Marlene 07.12.18
/**
 * <p>
 * generateInitialEstimates.
 * </p>
 *
 * @param system a {@link neqsim.thermo.system.SystemInterface} object
 * @param bVector an array of {@link double} objects
 * @param inertMoles a double
 * @param phase a int
 * @return an array of {@link double} objects
 */
public double[] generateInitialEstimates(SystemInterface system, double[] bVector, double inertMoles, int phase) {
    int i, j;
    double rhs = 0.0;
    Matrix mutemp = new Matrix(chemRefPot, 1).times(1.0 / (R * system.getPhase(phase).getTemperature())).copy();
    double[] v = new double[components.length + 1];
    for (i = 0; i < components.length; i++) {
        v[i + 1] = mutemp.get(0, i);
    }
    LinearObjectiveFunction f = new LinearObjectiveFunction(v, 0.0);
    List<LinearConstraint> cons = new ArrayList<LinearConstraint>();
    for (j = 0; j < bVector.length; j++) {
        for (i = 0; i < components.length; i++) {
            v[i + 1] = Amatrix[j][i];
        }
        rhs = bVector[j];
        cons.add(new LinearConstraint(v, Relationship.EQ, rhs));
    }
    NonNegativeConstraint nonneg = new NonNegativeConstraint(true);
    LinearConstraintSet consSet = new LinearConstraintSet(cons);
    SimplexSolver solver = new SimplexSolver();
    PointValuePair optimal = null;
    try {
        optimal = solver.optimize(new MaxIter(1000), f, consSet, GoalType.MINIMIZE, nonneg);
    } catch (NoFeasibleSolutionException exp) {
        System.out.println("no feasible solution");
        return null;
    } catch (Exception exp) {
        System.out.println("linear optimization failed");
        return null;
    }
    int compNumb = system.getPhase(phase).getNumberOfComponents();
    double[] lp_solution = new double[compNumb];
    double[] temp = optimal.getPoint();
    for (i = 0; i < compNumb - (compNumb - components.length); i++) {
        lp_solution[i] = temp[i + 1];
    }
    return lp_solution;
}
Also used : NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearConstraintSet(org.apache.commons.math3.optim.linear.LinearConstraintSet) NoFeasibleSolutionException(org.apache.commons.math3.optim.linear.NoFeasibleSolutionException) LinearObjectiveFunction(org.apache.commons.math3.optim.linear.LinearObjectiveFunction) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) ArrayList(java.util.ArrayList) NonNegativeConstraint(org.apache.commons.math3.optim.linear.NonNegativeConstraint) LinearConstraint(org.apache.commons.math3.optim.linear.LinearConstraint) NoFeasibleSolutionException(org.apache.commons.math3.optim.linear.NoFeasibleSolutionException) PointValuePair(org.apache.commons.math3.optim.PointValuePair) Matrix(Jama.Matrix) SimplexSolver(org.apache.commons.math3.optim.linear.SimplexSolver) MaxIter(org.apache.commons.math3.optim.MaxIter)

Aggregations

Matrix (Jama.Matrix)1 ArrayList (java.util.ArrayList)1 MaxIter (org.apache.commons.math3.optim.MaxIter)1 PointValuePair (org.apache.commons.math3.optim.PointValuePair)1 LinearConstraint (org.apache.commons.math3.optim.linear.LinearConstraint)1 LinearConstraintSet (org.apache.commons.math3.optim.linear.LinearConstraintSet)1 LinearObjectiveFunction (org.apache.commons.math3.optim.linear.LinearObjectiveFunction)1 NoFeasibleSolutionException (org.apache.commons.math3.optim.linear.NoFeasibleSolutionException)1 NonNegativeConstraint (org.apache.commons.math3.optim.linear.NonNegativeConstraint)1 SimplexSolver (org.apache.commons.math3.optim.linear.SimplexSolver)1