COIN-OR::LEMON - Graph Library

Changeset 1542:0219ee65ffcc in lemon-0.x for test


Ignore:
Timestamp:
07/07/05 17:00:04 (15 years ago)
Author:
athos
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2037
Message:

Some testing of the LP interface: bugs got fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/lp_test.cc

    r1508 r1542  
    11#include<lemon/lp_skeleton.h>
    22#include "test_tools.h"
     3
    34
    45#ifdef HAVE_CONFIG_H
     
    183184}
    184185
     186void solveAndCheck(LpSolverBase& lp, LpSolverBase::SolutionStatus stat,
     187                   double exp_opt){
     188  lp.solve();
     189  //int decimal,sign;
     190  std::string buf1;
     191  //  itoa(stat,buf1, 10);
     192  check(lp.primalStatus()==stat,"Primalstatus should be "+buf1);
     193   
     194  if (stat ==  LpSolverBase::OPTIMAL){
     195    check(std::abs(lp.primalValue()-exp_opt)<1e-3,
     196          "Wrong optimal value: the right optimum is ");
     197    //+ecvt(exp_opt,2)
     198  }
     199}
     200 
    185201void aTest(LpSolverBase & lp)
    186202{
    187203  typedef LpSolverBase LP;
    188204
    189  //The following example is taken from the book by Gáspár and Temesi, page 39.
     205 //The following example is very simple
    190206
    191207  typedef LpSolverBase::Row Row;
     
    198214
    199215  //Constraints
    200   lp.addRow(3*x1+2*x2 >=6); 
    201   lp.addRow(-1*x1+x2<=4); 
    202   lp.addRow(5*x1+8*x2<=40); 
    203   lp.addRow(x1-2*x2<=4); 
     216  Row upright=lp.addRow(x1+x2 <=1); 
     217  lp.addRow(x1+x2 >=-1); 
     218  lp.addRow(x1-x2 <=1); 
     219  lp.addRow(x1-x2 >=-1); 
    204220  //Nonnegativity of the variables
    205221  lp.colLowerBound(x1, 0);
    206222  lp.colLowerBound(x2, 0);
    207223  //Objective function
    208   lp.setObj(2*x1+x2);
     224  lp.setObj(x1+x2);
    209225
    210226  lp.max();
    211   lp.solve();
    212 
    213   double opt=122.0/9.0;
    214  
    215   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
    216     std::cout<< "Z = "<<lp.primalValue()
    217              << " (error = " << lp.primalValue()-opt
    218              << "); x1 = "<<lp.primal(x1)
    219              << "; x2 = "<<lp.primal(x2)
    220              <<std::endl;
    221    
    222   }
    223   else{
    224     std::cout<<"Optimal solution not found!"<<std::endl;
    225   }
    226 
    227   check(lp.primalStatus()==LpSolverBase::OPTIMAL,"Primalstatus should be OPTIMAL");
    228 
    229   check(std::abs(lp.primalValue()-opt)<1e-3,
    230         "Wrong optimal value: the right optimum is 122/9 (13.555555...)");
    231 
     227
     228  //Maximization of x1+x2
     229  //over the triangle with vertices (0,0) (0,1) (1,0)
     230  double expected_opt=1;
     231  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
     232 
     233  //Minimization
     234  lp.min();
     235  expected_opt=0;
     236  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
     237 
     238  //Vertex (-1,0) instead of (0,0)
     239  lp.colLowerBound(x1, -LpSolverBase::INF);
     240  expected_opt=-1;
     241  solveAndCheck(lp, LpSolverBase::OPTIMAL, expected_opt);
     242
     243  //Erase one constraint and return to maximization
     244  lp.eraseRow(upright);
     245  lp.max();
     246  expected_opt=LpSolverBase::INF;
     247  solveAndCheck(lp, LpSolverBase::INFINITE, expected_opt);
     248
     249  //Infeasibilty
     250  lp.addRow(x1+x2 <=-2); 
     251  solveAndCheck(lp, LpSolverBase::INFEASIBLE, expected_opt);
     252
     253  //Change problem and forget to solve
     254  lp.min();
     255  check(lp.primalStatus()==LpSolverBase::UNDEFINED,"Primalstatus should be UNDEFINED");
     256
     257//   lp.solve();
     258//   if (lp.primalStatus()==LpSolverBase::OPTIMAL){
     259//     std::cout<< "Z = "<<lp.primalValue()
     260//           << " (error = " << lp.primalValue()-expected_opt
     261//           << "); x1 = "<<lp.primal(x1)
     262//           << "; x2 = "<<lp.primal(x2)
     263//           <<std::endl;
     264   
     265//   }
     266//   else{
     267//     std::cout<<lp.primalStatus()<<std::endl;
     268//     std::cout<<"Optimal solution not found!"<<std::endl;
     269//   }
     270
     271 
    232272
    233273}
Note: See TracChangeset for help on using the changeset viewer.