[Lemon-commits] [lemon_svn] marci: r1554 - hugo/trunk/src/work/marci/lp
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:46:10 CET 2006
Author: marci
Date: Thu Feb 17 16:14:13 2005
New Revision: 1554
Added:
hugo/trunk/src/work/marci/lp/magic_square.cc
Modified:
hugo/trunk/src/work/marci/lp/lp_solver_base.h
hugo/trunk/src/work/marci/lp/makefile
Log:
if you have a nuclear power plant and wanna compute small magic squares, then let's do it
Modified: hugo/trunk/src/work/marci/lp/lp_solver_base.h
==============================================================================
--- hugo/trunk/src/work/marci/lp/lp_solver_base.h (original)
+++ hugo/trunk/src/work/marci/lp/lp_solver_base.h Thu Feb 17 16:14:13 2005
@@ -619,6 +619,8 @@
virtual void _setColCont(int i) = 0;
/// \e
virtual void _setColInt(int i) = 0;
+ /// \e
+ virtual _Value _getMIPPrimal(int i) = 0;
public:
/// \e
void setColCont(Col col) {
@@ -628,6 +630,10 @@
void setColInt(Col col) {
_setColInt(col_iter_map[col]);
}
+ /// \e
+ _Value getMIPPrimal(Col col) {
+ return _getMIPPrimal(col_iter_map[col]);
+ }
//@}
};
@@ -1096,9 +1102,11 @@
void setMIP() { lpx_set_class(lp, LPX_MIP); }
protected:
/// \e
- void _setColCont(int i) {lpx_set_col_kind(lp, i, LPX_CV); }
+ void _setColCont(int i) { lpx_set_col_kind(lp, i, LPX_CV); }
+ /// \e
+ void _setColInt(int i) { lpx_set_col_kind(lp, i, LPX_IV); }
/// \e
- void _setColInt(int i) {lpx_set_col_kind(lp, i, LPX_IV); }
+ double _getMIPPrimal(int i) { return lpx_mip_col_val(lp, i); }
};
/// @}
Added: hugo/trunk/src/work/marci/lp/magic_square.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/marci/lp/magic_square.cc Thu Feb 17 16:14:13 2005
@@ -0,0 +1,88 @@
+// -*- c++ -*-
+#include <iostream>
+#include <fstream>
+
+#include <lemon/time_measure.h>
+#include <lp_solver_base.h>
+
+using std::cout;
+using std::endl;
+using namespace lemon;
+
+/*
+ for n=3,4 , the program is very fast
+ for n=5, with glpk, the run takes hours
+ */
+
+int main(int, char **) {
+ const int n=4;
+ const double row_sum=(1.0+n*n)*n/2;
+ Timer ts;
+ ts.reset();
+ typedef LPGLPK LPSolver;
+ typedef LPSolver::Col Col;
+ LPSolver lp;
+ typedef std::map<std::pair<int, int>, Col> Coords;
+ Coords x;
+ // we create a new variable for each entry
+ // of the magic square
+ for (int i=1; i<=n; ++i) {
+ for (int j=1; j<=n; ++j) {
+ Col col=lp.addCol();
+ x[std::make_pair(i,j)]=col;
+ lp.setColLowerBound(col, 1.0);
+ lp.setColUpperBound(col, double(n*n));
+ }
+ }
+ LPSolver::Expression expr3, expr4;
+ for (int i=1; i<=n; ++i) {
+ LPSolver::Expression expr1, expr2;
+ for (int j=1; j<=n; ++j) {
+ expr1+=x[std::make_pair(i, j)];
+ expr2+=x[std::make_pair(j, i)];
+ }
+ // sum of rows and columns
+ lp.addRow(expr1==row_sum);
+ lp.addRow(expr2==row_sum);
+ expr3+=x[std::make_pair(i, i)];
+ expr4+=x[std::make_pair(i, (n+1)-i)];
+ }
+ // sum of the diagonal entries
+ lp.addRow(expr3==row_sum);
+ lp.addRow(expr4==row_sum);
+ lp.solveSimplex();
+ cout << "elapsed time: " << ts << endl;
+ for (int i=1; i<=n; ++i) {
+ for (int j=1; j<=n; ++j) {
+ cout << "x("<<i<<","<<j<<")="<<lp.getPrimal(x[std::make_pair(i,j)])
+ << endl;
+ }
+ }
+ // we make new binary variables for each pair of
+ // entries of the square to achieve that
+ // the values of different entries are different
+ lp.setMIP();
+ for (Coords::const_iterator it=x.begin(); it!=x.end(); ++it) {
+ Coords::const_iterator jt=it; ++jt;
+ for(; jt!=x.end(); ++jt) {
+ Col col1=(*it).second;
+ Col col2=(*jt).second;
+ Col col=lp.addCol();
+ lp.setColLowerBound(col, 0.0);
+ lp.setColUpperBound(col, 1.0);
+ lp.addRow(double(-n*n+1.0)<=1.0*col2-1.0*col1-double(n*n)*col<=-1.0);
+ lp.setColInt(col);
+ }
+ }
+ cout << "elapsed time: " << ts << endl;
+ lp.solveSimplex();
+ // let's solve the integer problem
+ lp.solveBandB();
+ cout << "elapsed time: " << ts << endl;
+ for (int i=1; i<=n; ++i) {
+ for (int j=1; j<=n; ++j) {
+ cout << "x("<<i<<","<<j<<")="<<lp.getMIPPrimal(x[std::make_pair(i,j)])
+ << endl;
+ }
+ }
+}
Modified: hugo/trunk/src/work/marci/lp/makefile
==============================================================================
--- hugo/trunk/src/work/marci/lp/makefile (original)
+++ hugo/trunk/src/work/marci/lp/makefile Thu Feb 17 16:14:13 2005
@@ -5,7 +5,7 @@
CXXFLAGS = -g -O2 -W -Wall $(INCLUDEDIRS) -ansi -pedantic
LDFLAGS = -lglpk#-lcplex -lm -lpthread -lilocplex -L/usr/local/cplex/cplex75/lib/i86_linux2_glibc2.2_gcc3.0/static_mt# -L$(GLPKROOT)/lib
-BINARIES = max_flow_expression expression_test max_flow_by_lp# sample sample2 sample11 sample15
+BINARIES = magic_square max_flow_expression expression_test max_flow_by_lp# sample sample2 sample11 sample15
#include ../makefile
More information about the Lemon-commits
mailing list