use of lpsolve.LpSolveException in project jop by jop-devel.
the class IPETSolver method solve.
/**
* Solve the max cost network flow problem using {@link LpSolveWrapper}.
*
* @param flowMapOut if not null, write solution into this map, assigning a flow to each edge
* @param isILP if false, assumes all variables are rational (relaxed problem)
* @return the cost of the solution
* @throws LpSolveException
* @throws Exception if the ILP solver fails
*/
public double solve(Map<T, Long> flowMapOut, boolean isILP) throws LpSolveException {
IDProvider<Object> idProvider = this.generateMapping();
LpSolveWrapper<Object> wrapper = new LpSolveWrapper<Object>(edgeSet.size(), isILP, idProvider);
/* Add Constraints */
for (LinearConstraint<T> lc : edgeConstraints) {
wrapper.addConstraint(lc);
}
/* build cost objective */
LinearVector<T> costVec = new LinearVector<T>();
for (Entry<T, Long> entry : this.edgeCost.entrySet()) {
long costFactor = entry.getValue();
costVec.add(entry.getKey(), costFactor);
}
wrapper.setObjective(costVec, true);
wrapper.freeze();
File dumpFile = null;
if (this.outDir != null) {
try {
dumpFile = dumpILP(wrapper);
} catch (IOException e) {
throw new LpSolveException("Failed to write ILP: " + e.getMessage());
}
}
double sol;
if (flowMapOut != null) {
double[] objVec = new double[edgeSet.size()];
sol = Math.round(wrapper.solve(objVec));
for (int i = 0; i < idEdgeMap.size(); i++) {
flowMapOut.put(idEdgeMap.get(i + 1), Math.round(objVec[i]));
}
} else {
try {
sol = Math.round(wrapper.solve(USE_PRESOLVE));
} catch (LpSolveException ex) {
throw new LpSolveException(ex.getMessage() + ". ILP dump: " + dumpFile);
}
}
return sol;
}
use of lpsolve.LpSolveException in project jop by jop-devel.
the class MaxCostFlow method dumpILP.
private void dumpILP(LpSolveWrapper<?> wrapper) throws LpSolveException {
wrapper.dumpToFile(outFile);
FileWriter fw = null;
try {
fw = new FileWriter(outFile, true);
} catch (IOException e1) {
throw new LpSolveException("Failed to open ILP file");
}
try {
fw.append("/* Mapping: \n");
for (Entry<E, Integer> e : this.idMap.entrySet()) {
fw.append(" " + e.getKey() + " -> C" + e.getValue() + "\n");
}
for (Entry<DecisionVariable, Integer> dv : this.dMap.entrySet()) {
fw.append(" " + dv.getKey() + " -> C" + dv.getValue() + "\n");
}
fw.append(this.toString());
fw.append("*/\n");
} catch (IOException e) {
throw new LpSolveException("Failed to write to ILP file");
} finally {
try {
fw.close();
} catch (IOException e) {
throw new LpSolveException("Failed to close ILP file");
}
}
}
use of lpsolve.LpSolveException in project jop by jop-devel.
the class WCETAnalysis method exploreCacheAnalysis.
/**
* @param mca
* @param iter
* @throws InvalidFlowFactException
*/
@SuppressWarnings("unused")
private void exploreCacheAnalysis() throws InvalidFlowFactException {
// Segment Cache Analysis: Experiments
MethodCacheAnalysis mca = new MethodCacheAnalysis(wcetTool);
/* iterate top down the scope graph (currently: the call graph) */
TopologicalOrderIterator<ExecutionContext, ContextEdge> iter = wcetTool.getCallGraph().reverseTopologicalOrder();
LpSolveWrapper.resetSolverTime();
long blocks = 0;
long start = System.nanoTime();
while (iter.hasNext()) {
ExecutionContext scope = iter.next();
Segment segment = Segment.methodSegment(scope.getMethodInfo(), scope.getCallString(), wcetTool, wcetTool.getCallstringLength(), wcetTool);
int availBlocks = wcetTool.getWCETProcessorModel().getMethodCache().getNumBlocks();
long total, distinctApprox = -1, distinct = -1;
blocks = total = mca.countDistinctBlocksUsed(segment);
if (total > availBlocks || true) {
try {
blocks = distinctApprox = mca.countDistinctBlocksAccessed(segment, false);
if (blocks > availBlocks && blocks < availBlocks * 2 || true) {
blocks = distinct = mca.countDistinctBlocksAccessed(segment, true);
}
} catch (LpSolveException e) {
System.err.println((distinctApprox >= 0 ? "I" : "Relaxed ") + "LP Problem too difficult, giving up: " + e);
}
}
System.out.println(String.format("block-count < %2d [%2d,%2d,%2d] for %-30s @ %s", blocks, total, distinctApprox, distinct, scope.getMethodInfo().getFQMethodName(), scope.getCallString().toStringVerbose(false)));
}
long stop = System.nanoTime();
reportSpecial("block-count", WcetCost.totalCost(blocks), start, stop, LpSolveWrapper.getSolverTime());
System.out.println("solver-time: " + LpSolveWrapper.getSolverTime());
}
use of lpsolve.LpSolveException in project jop by jop-devel.
the class GlobalAnalysis method computeWCET.
/**
* Compute WCET for a segment, using global IPET, and cache analysis results
* @throws InvalidFlowFactException
* @throws LpSolveException
* @throws UnsupportedCacheModelException
*/
public WcetCost computeWCET(String key, Segment segment, CacheCostCalculationMethod cacheMode) throws InvalidFlowFactException, LpSolveException, UnsupportedCacheModelException {
/* create an IPET problem for the segment */
String problemId = formatProblemName(key, segment.getEntryCFGs().toString());
IPETSolver<SuperGraphEdge> ipetSolver = buildIpetProblem(project, problemId, segment, ipetConfig);
/* compute cost */
setExecutionCost(segment, ipetSolver);
/* Add constraints for caches */
HashMap<String, Set<SuperGraphEdge>> costMissEdges = new HashMap<String, Set<SuperGraphEdge>>();
for (CacheModel cacheModel : project.getWCETProcessorModel().getCaches()) {
CacheAnalysis cpa = CacheAnalysis.getCacheAnalysisFor(cacheModel, project);
Set<SuperGraphEdge> edges = cpa.addCacheCost(segment, ipetSolver, cacheMode);
costMissEdges.put(cacheModel.toString(), edges);
}
/* Add constraints for object cache */
// ObjectRefAnalysis objectCacheAnalysis = new ObjectRefAnalysis(project, false, 4, 8, 4);
/* Return variables */
Map<SuperGraphEdge, Long> flowMap = new HashMap<SuperGraphEdge, Long>();
/* Solve */
long _start = System.currentTimeMillis();
double relaxedCost = 0;
try {
relaxedCost = ipetSolver.solve(null, false);
} catch (LpSolveException ex) {
WCETTool.logger.error("Solving the relaxed problem failed - bug in lp solving lib?");
}
long _time_rlp = System.currentTimeMillis() - _start;
double ilpCost = ipetSolver.solve(flowMap);
long _time_ilp = System.currentTimeMillis() - _start;
WCETTool.logger.info(String.format("LP (%d ms) %d | %d ILP (%d ms)", _time_rlp, Math.round(relaxedCost), Math.round(ilpCost), _time_ilp));
/* Cost extraction */
WcetCost cost;
/* extract cost and generate a profile in 'profiles' */
cost = exportCostProfile(flowMap, costMissEdges, ipetSolver, problemId);
/* Sanity Check, and Return */
if (Double.isInfinite(ilpCost)) {
throw new AssertionError("[GlobalAnalysis] Unbounded (infinite lp cost)");
}
long objValue = (long) (ilpCost + 0.5);
if (cost.getCost() != objValue) {
throw new AssertionError("[GlobalAnalysis] Inconsistency: lpValue vs. extracted value: " + objValue + " / " + cost.getCost());
}
return cost;
}
use of lpsolve.LpSolveException in project jop by jop-devel.
the class IPETSolver method dumpILP.
private File dumpILP(LpSolveWrapper<?> wrapper) throws LpSolveException, IOException {
outDir.mkdirs();
File outFile = File.createTempFile(MiscUtils.sanitizeFileName(this.problemName), ".lp", outDir);
wrapper.dumpToFile(outFile);
FileWriter fw = null;
try {
fw = new FileWriter(outFile, true);
} catch (IOException e1) {
throw new LpSolveException("Failed to open ILP file: " + e1.getMessage());
}
try {
fw.append("/* Mapping: \n");
for (Entry<T, Integer> e : this.edgeIdMap.entrySet()) {
fw.append(" " + e.getKey() + " -> C" + e.getValue() + "\n");
}
fw.append(this.toString());
fw.append("*/\n");
} catch (IOException e) {
throw new LpSolveException("Failed to write to ILP file: " + e.getMessage());
} finally {
try {
fw.close();
} catch (IOException e) {
throw new LpSolveException("Failed to close ILP file: " + e.getMessage());
}
}
return outFile;
}
Aggregations