00001 /* 00002 * Copyright 2007 Martin von Gagern 00003 * 00004 * 00005 * This file is part of bande. 00006 * 00007 * bande is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * bande is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 00022 /** 00023 * @file 00024 * Implementation of class bande::Solutions. 00025 */ 00026 00027 #include "bande.hh" 00028 00029 namespace bande { 00030 00031 /** 00032 * Write all future solutions to the specified file. 00033 * @param file the file to which solutions will be written. 00034 */ 00035 void Solutions::writeToFile(const char* file) { 00036 clearOut(); 00037 out = new std::ofstream(file); 00038 ownOut = true; 00039 } 00040 00041 /** 00042 * Write all future solutions to the specified stream. 00043 * The stream will nod be closed at the end. 00044 * @param outStream the stream to which solutions will be written. 00045 */ 00046 void Solutions::writeToStream(std::ostream& outStream) { 00047 clearOut(); 00048 out = &outStream; 00049 ownOut = false; 00050 } 00051 00052 /** 00053 * Ensure proper cleanup. 00054 * This deletes the output stream if this object created it in the 00055 * first place. 00056 */ 00057 void Solutions::clearOut() { 00058 if (ownOut) { 00059 delete out; 00060 out = 0; 00061 ownOut = false; 00062 } 00063 } 00064 00065 /** 00066 * Method invoked for every solution found. 00067 * Right now it only writes the solution, but a future version might 00068 * store the solution for postprocessing or something like this. 00069 * 00070 * @param ip the integer program in the starte representing the 00071 * current solution. 00072 */ 00073 void Solutions::solution(const IntegerProgram& ip) { 00074 writeSolution(ip); 00075 } 00076 00077 /** 00078 * Write solution to output stream. 00079 * 00080 * The solution is written as a single line, with columns separated 00081 * by tabs. Each column gives the name of one problem variable, an 00082 * equals sign, and the value of that variable in the current 00083 * solution. Variables with a value of zero are omitted. 00084 * 00085 * @param ip the integer program in the starte representing the 00086 * current solution. 00087 */ 00088 void Solutions::writeSolution(const IntegerProgram& ip) { 00089 if (!out) return; 00090 const char* delim = ""; 00091 for (int col = 0; col < ip.getNumCols(); ++col) { 00092 int val = ip.getIntSolution(col); 00093 if (!val) continue; 00094 *out << delim << ip.getColName(col) << "=" << val; 00095 delim = "\t"; 00096 } 00097 *out << std::endl; 00098 } 00099 00100 }
1.6.0