it.ssc.pl.milp.ConstraintFromString Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsr331-ssc Show documentation
Show all versions of jsr331-ssc Show documentation
This is a JSR331 interface for SSC (Software for the Calculation of the Simplex) is a java library for solving linear programming problems v. 3.0.1.
SSC was designed and developed by Stefano Scarioli.
The newest version!
package it.ssc.pl.milp;
import it.ssc.i18n.RB;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final class ConstraintFromString {
private ArrayList constraint;
ConstraintFromString(int dimension, ArrayList inequality) throws SimplexException, LPException {
this.constraint=new ArrayList ();
for(String disequa:inequality) {
constraint.add(getConstraintFromString(dimension, disequa));
}
}
ConstraintFromString(int dimension, ArrayList inequality,ArrayList constraint) throws SimplexException, LPException {
this.constraint=constraint;
for(String disequa:inequality) {
this.constraint.add(getConstraintFromString(dimension, disequa));
}
}
public ArrayList getConstraint() {
return constraint;
}
private Constraint getConstraintFromString(int dim,String dis) throws LPException, SimplexException {
Pattern pattern = Pattern.compile("\\s*([+-]?)\\s*(((\\d+)((\\.)?)(\\d*))?)X(\\d+)((\\s*([+-])\\s*(((\\d+)((\\.)?)(\\d*))?)X(\\d+))*)\\s*((<\\s*=)|(>\\s*=)|(=))\\s*(([+-]?)(\\d+)((\\.)?)(\\d*))",Pattern.CASE_INSENSITIVE);
Matcher matcher_group_var = pattern.matcher(dis);
if (matcher_group_var.matches()) {
return equationFromString(dim, dis);
}
else {
throw new LPException(RB.getString("it.ssc.pl.milp.ConstraintFromString.msg1")+dis);
}
}
private Constraint equationFromString(int dim, String s) throws SimplexException {
ConsType rel=null;
//System.out.println("dim"+dim);
if ( s.matches("(.+)>\\s*=(.+)")) {
rel = ConsType.GE;
}
else if (s.matches("(.+)<\\s*=(.+)")) {
rel = ConsType.LE;
}
else if (s.contains("=")) {
rel = ConsType.EQ;
}
String[] disequation = s.split("[><]?\\s*=");
double b = Double.parseDouble(disequation[1].trim());
double[] Aj = new double[dim];
for(int i=0;i