PageRenderTime 51ms CodeModel.GetById 38ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/farmR/src/java/src/jfm/model/Output.java

https://code.google.com/p/javawfm/
Java | 141 lines | 113 code | 13 blank | 15 comment | 18 complexity | 918aef12ef6793bc0e004969181dfe39 MD5 | raw file
  1package jfm.model;
  2import java.util.*;
  3
  4import jfm.model.Crop.CropCopy;
  5import jfm.model.Types.*;
  6import jfm.utils.*;
  7import jfm.lp.*;
  8/** Static functions to extract solution information from model objects. 
  9 * @author Ira Cooke  */
 10public final class Output {
 11	public static String oneLineSummary(Farm farm){
 12		StringBuffer buff=new StringBuffer();
 13		Map<Double,CropType> cropAreas=new HashMap<Double,CropType>();
 14		for(CropType ct:farm.cropping.baseCropTypes()){
 15			cropAreas.put(farm.cropping.getCrop(ct).getSolvedArea(),ct);
 16		}
 17		buff.append("Total: "+MathPrint.f2.format(farm.landUse.getArea())+" ");
 18		
 19		List<Double> areas = new ArrayList<Double>(cropAreas.keySet());
 20		Collections.sort(areas);
 21		int numDisplay=Math.min(areas.size(),6);
 22		for(int i=0;i<numDisplay;i++){
 23			Double a=areas.get(areas.size()-1-i);
 24			buff.append(cropAreas.get(a).shortName+": "+MathPrint.f2.format(a)+" ");
 25		}
 26		return buff.toString();
 27	}
 28	
 29	public static String formulaChecks(Farm farm){
 30		StringBuffer buff=new StringBuffer();
 31		for (Crop cp:farm.cropping.getCrops().values()){
 32			buff.append(cp.type+"\n");
 33			buff.append(cp.printFormulaCheck(farm.location()));
 34		}
 35		return buff.toString();
 36	}
 37	
 38	/** Print a breakdown of the profit components for a Farm. 
 39	 * @param farm The farm to summarize 
 40	 * @param useMarginals Flag whether to print marginal (per ha) values or total values 
 41	 * */
 42	public static String profitSummary(Farm farm,boolean useMarginals){
 43		StringBuffer buff=new StringBuffer();
 44		double totalProfit=0;
 45		for(CropType ct:farm.cropping.baseCropTypes()){
 46			for ( CropCopy cc:farm.cropping.getCrop(ct).getYearCopies()){
 47				if ( cc.getSolvedArea() > 0 ){
 48					Map<String,Double> profits=cc.getProfitSummary(useMarginals);
 49					buff.append(cc.name()+" "+profits.get("Total")+" "+profits.get("Gross")+" "+profits.get("OpPenalty")+" "+profits.get("OpFuel"));
 50					buff.append("\n");
 51					totalProfit+=profits.get("Total");
 52				}
 53			}
 54		}
 55		buff.append("Total: "+totalProfit);
 56		return buff.toString();
 57	}
 58	/** Print a breakdown of the profit components for a Farm. 
 59	 * @param farm The farm to summarize 
 60	 * @param useMarginals Flag whether to print marginal (per ha) values or total values 
 61	 * */
 62	public static String detailedObjectiveSummary(Farm farm){
 63		StringBuffer buff=new StringBuffer();
 64		int columnWidth=15; // Allow 15 characters per column
 65		Map<ObjectiveType,Objective> objs=farm.getObjectives();
 66		for(ObjectiveType obj:objs.keySet()){
 67			buff.append(obj+":"+MathPrint.f2.format(farm.getValueForObjective(obj))+" "+
 68					MathPrint.f2.format(objs.get(obj).scaleFactor())+" "+
 69					MathPrint.f2.format(objs.get(obj).scaleFactor()*farm.getValueForObjective(obj))+"\n");			
 70		}
 71		for(CropType ct:farm.cropping.baseCropTypes()){
 72			for ( CropCopy cc:farm.cropping.getCrop(ct).getYearCopies()){
 73				if ( cc.getSolvedArea() > 0 ){
 74					buff.append(cc.getDetailedProfitBreakdown(columnWidth));
 75				}
 76			}
 77		}
 78		return buff.toString();
 79	}
 80	
 81	/** Print a summary of the workload in each period */
 82	public static String workloadSummary(Farm farm){
 83		StringBuffer buff=new StringBuffer();
 84		Map<Integer,LinkedHashMap<Object,Double>> workLoad=farm.workers.getWorkload();
 85		if ( farm.objectives().containsKey(ObjectiveType.FREETIME)){
 86			FreeTimeComponent ftc=(FreeTimeComponent)farm.getModelComponent(ModelComponent.MCType.FREETIME);
 87			Map<Integer,Double> ft=ftc.getSolvedFreeTime();
 88			for (int p=0;p<workLoad.size();p++){
 89				Map<Object,Double> work=workLoad.get(p);
 90				work.put(ObjectiveType.FREETIME, ft.get(p));
 91			}
 92		}
 93		ArrayList<Object> masterKeys=new ArrayList<Object>(workLoad.get(0).keySet());
 94		for( Object key:masterKeys){
 95			buff.append(key+",");
 96		}
 97		buff.deleteCharAt(buff.length()-1);
 98		buff.append("\n");
 99		for(int p=0;p<farm.numPeriods;p++){
100			for ( Object key:masterKeys){
101				buff.append(MathPrint.f2.format(workLoad.get(p).get(key))+",");
102			}
103			buff.deleteCharAt(buff.length()-1);
104			buff.append("\n");
105		}
106		
107		return buff.toString();
108	}
109	/** Print a summary of the solved model components for this farm. 
110	 * @param farm The farm to be summarized 
111	 * @param detailed Flag whether to print complete detail or not 
112	 * */
113	public static String solution(Farm farm,boolean detailed){
114		StringBuffer buff = new StringBuffer();
115		for (Objective op:farm.objectives().values()){
116			buff.append(op.type+": " +MathPrint.df2.format(farm.getValueForObjective(op.type))+"\n");
117		}
118		if ( farm.getComponents().keySet().contains(ModelComponent.MCType.HEDGEROWS) ){			
119			buff.append("\n-- Hedge Length: "+farm.getValueForObjective(ObjectiveType.HEDGEROWS)+"\n");
120		}
121
122		if ( farm.getComponents().keySet().contains(ModelComponent.MCType.ELSOPTIONS)){
123			ELSOptionsComponent comp = (ELSOptionsComponent)farm.getModelComponent(ModelComponent.MCType.ELSOPTIONS);
124			buff.append("\n-- ELS Options Summary --\n"+comp.printSolution());
125		}
126		
127		
128		buff.append("\n"+farm.cropping.printSolution(farm,detailed)+" \n");
129		buff.append(farm.rotations.printSolution()+"\n ");
130		buff.append(farm.workers.printSolution(farm));
131		buff.append("\n"+farm.workers.printFixedCosts(farm)+"\n");
132		
133		return buff.toString();
134	}
135	
136	
137	
138	public static String csvWorkPlan(Farm farm){
139		return farm.cropping.printCSVWorkPlan();
140	}
141}