All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.ssc.pl.milp.CreatePLProblem Maven / Gradle / Ivy

Go to download

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 java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.logging.Logger;

import it.ssc.datasource.DataSource;
import it.ssc.datasource.DataSourceException;
import it.ssc.i18n.RB;
import it.ssc.log.SscLevel;
import it.ssc.log.SscLogger;

 final class CreatePLProblem implements Costant  {
	 
	 private static final Logger logger=SscLogger.getLogger();
	 
	 
	 public static PLProblem create(LinearObjectiveFunction f,
			 ArrayList constraints, ArrayList nomi_var,ArrayProblem arrayProb,
			 boolean isMilp) throws Exception  {


		 //checkDimensionProblem(f,constraints);

		 double[] C=f.getC();
		 int N=C.length;
		 PLProblem lp_original=new PLProblem(N);
		 int _x=0;
		 for(String nome:nomi_var)  {
			 lp_original.setNameVar(_x++, nome);
		 }

		 if(f.getType()==GoalType.MAX) lp_original.setTargetObjFunction(MAX);
		 else if(f.getType()==GoalType.MIN) lp_original.setTargetObjFunction(MIN);
		 for(int _j=0;_j constraints,
			 						boolean isMilp) throws Exception  {

		 boolean exist_integer_var=false;
		 boolean is_set_row_upper=false;
		 boolean is_set_row_lower=false;
		 boolean is_set_row_binary=false;
		 boolean is_set_row_integer=false;
		 boolean is_set_row_semicont=false;
		 
		 checkDimensionProblem(f,constraints);
		
		 double[] C=f.getC();
		 int N=C.length;
		 PLProblem lp_original=new PLProblem(N);
		 for(int _j=0;_j hash_row_type=new HashMap(); 
		HashMap> hash_row_col=new HashMap>(); 
		TreeSet tree_col=new TreeSet();
		
		int size=source_sparse.getNumColunm();

		boolean exist_column_type=false; 
		boolean exist_column_col=false;  
		boolean exist_column_row=false; 
		boolean exist_column_coe=false; 
		for(int _a=0;_a hash_col_val;
				if(hash_row_col.containsKey(row)) { 
					hash_col_val=hash_row_col.get(row);
					hash_col_val.put(col, coef);
				}
				else { 
					hash_col_val=new HashMap();
					hash_col_val.put(col, coef);
					hash_row_col.put(row, hash_col_val);
				}
			}
		}	
		
		//controllo che ogni row di tipo EQ,LE,GE abbia valorizzato coef
		for(String row2:hash_row_type.keySet()) {
			String type2=hash_row_type.get(row2);
			if( type2.equals(LE) || type2.equals(EQ) || type2.equals(GE) || type2.equals(MAX) || type2.equals(MIN)) {
				//logger.log(SscLevel.WARNING,"####:"+row2);
				HashMap hash_col_val2=hash_row_col.get(row2);
				if(hash_col_val2==null) throw new SimplexException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg49", row2));
				for(String col2:tree_col) {
					Double coef2=hash_col_val2.get(col2);
					if( !col2.equals("RHS") && coef2==null) hash_col_val2.put(col2, 0.0); //se manca vuol dire che e' a zero
				}
			}
		}
		
		//Controlli da fare : a) ogni vincolo di tipo EQ,LE,Ge deve avere un RHS 
		
		
		for(String row2:hash_row_type.keySet()) {
			String type2=hash_row_type.get(row2);
			if( type2.equals(LE)    || type2.equals(EQ)     || type2.equals(GE))  {
				HashMap hash_col_val2=hash_row_col.get(row2);
				boolean exist_rhs=false;
				for(String col2:hash_col_val2.keySet()) {
					if(col2.equals("RHS")) exist_rhs=true;
					//System.out.println("ROW:"+row2 +" COL:"+col2 );
				}
				if(!exist_rhs) throw new SimplexException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg42", row2,type2));  
			}
		}
		
		
		for(String row:hash_row_col.keySet()) {
			if(!hash_row_type.containsKey(row)) {
				throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg43", row));  
			}
		}	
		
		//CONTROLLARE CHE ESISTA ALMENO UN MAX O MIN
		if(!(hash_row_type.containsValue(MAX) || hash_row_type.containsValue(MIN)))  {
			throw new LPException(RB.getString("it.ssc.pl.milp.CreateMilpProblem.msg44")); 
		}
		
		source_sparse.close();
		return createMilpFromHashTables( hash_row_type,hash_row_col,tree_col) ;
	}
	
	
	
	private static PLProblem createMilpFromHashTables(HashMap hash_row_type,
											  HashMap> hash_row_col,
											  TreeSet tree_col) throws LPException  {
		
		int size=tree_col.size();
		int N=size;
		if(tree_col.contains("RHS")) N=size-1;
		//System.out.println("DIME::"+N);
		PLProblem milp_original=new PLProblem(N);
		int _j=0;
		HashMap link_name_index=new HashMap();
		for(String nome_var:tree_col)  {
			 if(!nome_var.equals("RHS")) {
				 milp_original.setNameVar(_j,nome_var);
				 link_name_index.put(nome_var, _j);
				 _j++;
			 }
		 }
		
		for(String row:hash_row_col.keySet()) { 
			String type_row=hash_row_type.get(row);
			HashMap hah_col_val=hash_row_col.get(row);
			
			if(type_row.equals(MAX) || type_row.equals(MIN)) {
				milp_original.setTargetObjFunction(type_row);

				for(String col:hah_col_val.keySet()) { 
					if(!col.equals("RHS")) { 
						milp_original.setCjOF(link_name_index.get(col), hah_col_val.get(col));
						//System.out.println("xxx CIJ::"+link_name_index.get(col)+"::"+hah_col_val.get(col));
					}
				}
			}
			
			else if(type_row.equals(LE) || type_row.equals(GE) || type_row.equals(EQ)) {
				InternalConstraint constraint_i=new InternalConstraint(N) ;
				constraint_i.setName(row);
				//System.out.println("xxx RHS::"+row);
				for(String col:hah_col_val.keySet()) { 
					if(col.equals("RHS")) { 
						 Double double_b=hah_col_val.get(col);
						 if(double_b==null) throw new LPException(RB.getString("it.ssc.pl.milp.CreateMilpProblem.msg1"));
						 constraint_i.setBi(double_b);
						 //System.out.println("xxx RHS::"+double_b);
					}
					else {
						Double double_val=hah_col_val.get(col);
						if(double_val==null) throw new LPException(RB.getString("it.ssc.pl.milp.CreateMilpProblem.msg2"));
						constraint_i.setAij(link_name_index.get(col), double_val);
						//System.out.println("xxx AIJ::"+link_name_index.get(col)+"::"+double_val);

						if(type_row.equals(LE)) constraint_i.setType(InternalConstraint.TYPE_CONSTR.LE);
						else if(type_row.equals(GE)) constraint_i.setType(InternalConstraint.TYPE_CONSTR.GE);
						else if(type_row.equals(EQ)) constraint_i.setType(InternalConstraint.TYPE_CONSTR.EQ);
					}
				}
				milp_original.addConstraint(constraint_i);
			}
			
			else if(type_row.equals(UPPER) || type_row.equals(LOWER) ) {
				
				for(String col:hah_col_val.keySet()) { 
					if(!col.equals("RHS")) { 
						Var xj=milp_original.getVar(link_name_index.get(col));
						Double bound_val=hah_col_val.get(col);
						if(bound_val==null) bound_val=Double.NaN;
						if(type_row.equals(UPPER)) xj.setUpper(bound_val);
						if(type_row.equals(LOWER)) xj.setLower(bound_val);
						//System.out.println("xxx "+type_row+"::"+link_name_index.get(col)+"::"+bound_val);
					}
				}
			}
			
			
			else if(type_row.equalsIgnoreCase(BINARY) || type_row.equalsIgnoreCase(INTEGER) || type_row.equalsIgnoreCase(SEMICONT)) {

				for(String col:hah_col_val.keySet()) { 
					if(!col.equals("RHS")) { 

						Var xj=milp_original.getVar(link_name_index.get(col));
						Double type_var=hah_col_val.get(col);
						
						if(type_var!=null && type_var==1.0) {
							//System.out.println("VAR  "+_j +"  "+type_row);
							if(type_row.equalsIgnoreCase(INTEGER)) { 
								if(xj.getType()==Var.TYPE_VAR.BINARY) throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg22", col));
								xj.setType(Var.TYPE_VAR.INTEGER);
							}
							if(type_row.equalsIgnoreCase(BINARY))  {
								if(xj.getType()==Var.TYPE_VAR.INTEGER) throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg23", col));
								xj.setType(Var.TYPE_VAR.BINARY);
							}
							if(type_row.equalsIgnoreCase(SEMICONT)) { 
								if(xj.getType()==Var.TYPE_VAR.BINARY) throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg25", col));
								xj.setSemicon(true);
							}
						}
						else if(type_var==null || type_var!=0.0) {
							if(type_row.equalsIgnoreCase(INTEGER)) throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg45", row));
							if(type_row.equalsIgnoreCase(BINARY))  throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg46", row));
							if(type_row.equalsIgnoreCase(SEMICONT))  throw new LPException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg47", row));
						}
					}
				}
			}
		}	
		return milp_original;
	}
	
	
	private static void checkDimensionProblem(LinearObjectiveFunction f,
											  ArrayList constraints) throws SimplexException {

		double[] c=f.getC();
		int n=c.length;

		for(Constraint constraint:constraints) {
			if(n!= constraint.getAj().length) {
				throw new SimplexException(RB.format("it.ssc.pl.milp.CreateMilpProblem.msg48", constraint.getRel(),n)); 		 
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy