[Lemon-commits] [lemon_svn] athos: r1734 - hugo/trunk/src/work/athos/lp
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:47:15 CET 2006
Author: athos
Date: Mon Apr 4 16:46:08 2005
New Revision: 1734
Added:
hugo/trunk/src/work/athos/lp/lp_cplex.cc
hugo/trunk/src/work/athos/lp/lp_cplex.h
hugo/trunk/src/work/athos/lp/lp_test_cplex.cc
Log:
Started cplex low level interface.
Added: hugo/trunk/src/work/athos/lp/lp_cplex.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/lp/lp_cplex.cc Mon Apr 4 16:46:08 2005
@@ -0,0 +1,105 @@
+/* -*- C++ -*-
+ * src/lemon/lp_cplex.cc
+ * - Part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Combinatorial Optimization Research Group, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#include"lp_cplex.h"
+
+///\file
+///\brief Implementation of the LEMON-CPLEX lp solver interface.
+namespace lemon {
+
+ int LpCplex::_addCol()
+ {
+ int i = CPXgetnumcols (env, lp);
+ int lb[1],ub[1];
+ lb[0]=-INF;//-CPX_INFBOUND;
+ ub[0]=INF;//CPX_INFBOUND;
+ status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
+ return i;
+ }
+
+ int LpCplex::_addRow()
+ {
+ int i = CPXgetnumrows (env, lp);
+ status = CPXnewrows (env, lp, 1, NULL, NULL, NULL, NULL, NULL);
+ return i;
+ }
+
+ ///\warning Data at index 0 is ignored iin the arrays.
+ void LpCplex::_setRowCoeffs(int i,
+ int length,
+ int const * indices,
+ Value const * values )
+ {
+ int rowlist[length+1];
+ for (int k=1;k<=length;++k){
+ rowlist[k]=i;
+ }
+ status = CPXchgcoeflist(env, lp,
+ length,
+ rowlist++,
+ inices++,
+ values++);
+ }
+
+ void LpCplex::_setColCoeffs(int i,
+ int length,
+ int const * indices,
+ Value const * values)
+ {
+ int collist[length+1];
+ for (int k=1;k<=length;++k){
+ collist[k]=i;
+ }
+ status = CPXchgcoeflist(env, lp,
+ length,
+ inices++,
+ collist++,
+ values++);
+ }
+
+ void LpCplex::_setColLowerBound(int i, Value value)
+ {
+ }
+
+ void LpCplex::_setColUpperBound(int i, Value value)
+ {
+ }
+
+ void LpCplex::_setRowLowerBound(int i, Value value)
+ {
+ }
+
+ void LpCplex::_setRowUpperBound(int i, Value value)
+ {
+ }
+
+ void LpCplex::_setObjCoeff(int i, Value obj_coef)
+ {
+ }
+
+ LpCplex::SolutionType LpCplex::_solve()
+ {
+ return OPTIMAL;
+ }
+
+ LpCplex::Value LpCplex::_getSolution(int i)
+ {
+ return 0;
+ }
+
+} //namespace lemon
+
Added: hugo/trunk/src/work/athos/lp/lp_cplex.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/lp/lp_cplex.h Mon Apr 4 16:46:08 2005
@@ -0,0 +1,122 @@
+/* -*- C++ -*-
+ * src/lemon/lp_cplex.h - Part of LEMON, a generic C++ optimization library
+ *
+ * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Combinatorial Optimization Research Group, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_LP_CPLEX_H
+#define LEMON_LP_CPLEX_H
+
+///\file
+///\brief Header of the LEMON-CPLEX lp solver interface.
+
+#include "lp_base.h"
+extern "C" {
+#include "ilcplex/cplex.h"
+}
+
+namespace lemon {
+
+
+ /// \brief Wrapper for GLPK solver
+ ///
+ /// This class implements a lemon wrapper for GLPK.
+ class LpCplex : public LpSolverBase {
+
+ public:
+
+ typedef LpSolverBase Parent;
+
+ /// \e
+ int status;
+ CPXENVptr env;
+ CPXLPptr lp;
+
+
+ /// \e
+ LpCplex() : Parent() {
+ env = NULL;
+ lp = NULL;
+ env = CPXopenCPLEXdevelop(&status);
+// if (Env == NULL)
+// {
+// fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
+// CPXgeterrorstring(Env, Status, ErrorMsg);
+// fprintf(stderr,"%s",ErrorMsg);
+// goto Terminate;
+// }
+
+ // *** A problema létrehozása ***
+ lp = CPXcreateprob(env, &status, "LP problem");
+
+// if (Problem == NULL)
+// {
+// fprintf(stderr,"Az LP létrehozása sikertelen");
+// goto Terminate;
+// }
+
+ }
+ /// \e
+ ~LpCplex() {
+ status = CPXfreeprob(env,&lp);
+// if (Status != 0)
+// {
+// fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
+// CPXgeterrorstring(Env, Status, ErrorMsg);
+// fprintf(stderr,"%s",ErrorMsg);
+// goto Terminate;
+// }
+
+ status = CPXcloseCPLEX(&env);
+// if (Status != 0)
+// {
+// fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
+// CPXgeterrorstring(Env, Status, ErrorMsg);
+// fprintf(stderr,"%s",ErrorMsg);
+// goto Terminate;
+// }
+
+ }
+
+ protected:
+ virtual int _addCol();
+ virtual int _addRow();
+ virtual void _setRowCoeffs(int i,
+ int length,
+ const int * indices,
+ const Value * values );
+ virtual void _setColCoeffs(int i,
+ int length,
+ const int * indices,
+ const Value * values);
+ virtual void _setColLowerBound(int i, Value value);
+ virtual void _setColUpperBound(int i, Value value);
+ virtual void _setRowLowerBound(int i, Value value);
+ virtual void _setRowUpperBound(int i, Value value);
+ virtual void _setObjCoeff(int i, Value obj_coef);
+ ///\e
+
+ ///\bug Unimplemented
+ ///
+ virtual SolutionType _solve();
+ ///\e
+
+ ///\bug Unimplemented
+ ///
+ virtual Value _getSolution(int i);
+
+ };
+} //END OF NAMESPACE LEMON
+
+#endif //LEMON_LP_CPLEX_H
+
Added: hugo/trunk/src/work/athos/lp/lp_test_cplex.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/lp/lp_test_cplex.cc Mon Apr 4 16:46:08 2005
@@ -0,0 +1,185 @@
+#include"lp_solver_skeleton.h"
+#include"lp_cplex.h"
+#include<lemon/list_graph.h>
+
+using namespace lemon;
+
+void lpTest(LpSolverBase & lp)
+{
+ typedef LpSolverBase LP;
+
+ std::vector<LP::Col> x;
+ for(int i=0;i<10;i++) x.push_back(lp.addCol());
+
+ std::vector<LP::Col> y(10);
+ lp.addColSet(y);
+
+ std::map<int,LP::Col> z;
+
+ z.insert(std::make_pair(12,INVALID));
+ z.insert(std::make_pair(2,INVALID));
+ z.insert(std::make_pair(7,INVALID));
+ z.insert(std::make_pair(5,INVALID));
+
+ lp.addColSet(z);
+
+
+ LP::Expr e,f,g;
+ LP::Col p1,p2,p3,p4,p5;
+ LP::Constr c;
+
+ e[p1]=2;
+ e.constComp()=12;
+ e[p1]+=2;
+ e.constComp()+=12;
+ e[p1]-=2;
+ e.constComp()-=12;
+
+ e=2;
+ e=2.2;
+ e=p1;
+ e=f;
+
+ e+=2;
+ e+=2.2;
+ e+=p1;
+ e+=f;
+
+ e-=2;
+ e-=2.2;
+ e-=p1;
+ e-=f;
+
+ e*=2;
+ e*=2.2;
+ e/=2;
+ e/=2.2;
+
+ e=((p1+p2)+(p1-p2)+(p1+12)+(12+p1)+(p1-12)+(12-p1)+
+ (f+12)+(12+f)+(p1+f)+(f+p1)+(f+g)+
+ (f-12)+(12-f)+(p1-f)+(f-p1)+(f-g)+
+ 2.2*f+f*2.2+f/2.2+
+ 2*f+f*2+f/2+
+ 2.2*p1+p1*2.2+p1/2.2+
+ 2*p1+p1*2+p1/2
+ );
+
+
+ c = (e <= f );
+ c = (e <= 2.2);
+ c = (e <= 2 );
+ c = (e <= p1 );
+ c = (2.2<= f );
+ c = (2 <= f );
+ c = (p1 <= f );
+ c = (p1 <= p2 );
+ c = (p1 <= 2.2);
+ c = (p1 <= 2 );
+ c = (2.2<= p2 );
+ c = (2 <= p2 );
+
+ c = (e >= f );
+ c = (e >= 2.2);
+ c = (e >= 2 );
+ c = (e >= p1 );
+ c = (2.2>= f );
+ c = (2 >= f );
+ c = (p1 >= f );
+ c = (p1 >= p2 );
+ c = (p1 >= 2.2);
+ c = (p1 >= 2 );
+ c = (2.2>= p2 );
+ c = (2 >= p2 );
+
+ c = (e == f );
+ c = (e == 2.2);
+ c = (e == 2 );
+ c = (e == p1 );
+ c = (2.2== f );
+ c = (2 == f );
+ c = (p1 == f );
+ //c = (p1 == p2 );
+ c = (p1 == 2.2);
+ c = (p1 == 2 );
+ c = (2.2== p2 );
+ c = (2 == p2 );
+
+ c = (2 <= e <= 3);
+ c = (2 <= p1<= 3);
+
+ c = (2 >= e >= 3);
+ c = (2 >= p1>= 3);
+
+ e[x[3]]=2;
+ e[x[3]]=4;
+ e[x[3]]=1;
+ e.constComp()=12;
+
+ lp.addRow(LP::INF,e,23);
+ lp.addRow(LP::INF,3.0*(p1+p2)-p3,23);
+ lp.addRow(LP::INF,3.0*(x[1]+x[2]/2)-x[3],23);
+ lp.addRow(LP::INF,3.0*(p1+p2*2-5*p3+12-p4/3)+2*p4-4,23);
+ lp.addRow(LP::INF,3.0*(x[1]+x[2]*2-5*x[3]+12-x[4]/3)+2*x[4]-4,23);
+
+ lp.addRow(x[1]+x[3]<=x[5]-3);
+ lp.addRow(-7<=x[1]+x[3]-12<=3);
+ //lp.addRow(x[1]<=x[5]);
+
+}
+
+
+template<class G,class C>
+double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
+{
+ LpGlpk lp;
+
+ typedef G Graph;
+ typedef typename G::Node Node;
+ typedef typename G::NodeIt NodeIt;
+ typedef typename G::Edge Edge;
+ typedef typename G::EdgeIt EdgeIt;
+ typedef typename G::OutEdgeIt OutEdgeIt;
+ typedef typename G::InEdgeIt InEdgeIt;
+
+ typename G::EdgeMap<LpGlpk::Col> x(g);
+ lp.addColSet(x);
+ //for(EdgeIt e(g);e!=INVALID;++e) x[e]=lp.addCol();
+
+ for(EdgeIt e(g);e!=INVALID;++e) {
+ lp.setColUpperBound(x[e],cap[e]);
+ lp.setColLowerBound(x[e],0);
+ }
+
+ for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
+ LpGlpk::Expr ex;
+ for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];
+ for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
+ lp.addRow(0,ex,0);
+ }
+ {
+ LpGlpk::Expr ex;
+ for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e];
+ for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
+ lp.setObj(ex);
+ }
+
+ lp.solve();
+
+ return 0;
+}
+
+int main()
+{
+ LpSolverSkeleton lp_skel;
+ LpGlpk lp_glpk;
+ LpCplex lp_cplex;
+
+ lpTest(lp_skel);
+ lpTest(lp_cplex);
+
+ ListGraph g;
+ ListGraph::EdgeMap<double> cap(g);
+
+ maxFlow(g,cap,ListGraph::NodeIt(g),ListGraph::NodeIt(g));
+
+}
More information about the Lemon-commits
mailing list