source: trunk/Cbc/src/CbcBranchAllDifferent.cpp

Last change on this file was 2465, checked in by unxusr, 5 months ago

script to format sources

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1// $Id: CbcBranchAllDifferent.cpp 2465 2019-01-03 19:26:52Z forrest $
2// Copyright (C) 2004, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6// Edwin 11/13/2009-- carved out of CbcBranchCut
7
8#if defined(_MSC_VER)
9// Turn off compiler warning about long names
10#pragma warning(disable : 4786)
11#endif
12#include <cassert>
13#include <cstdlib>
14#include <cmath>
15#include <cfloat>
16//#define CBC_DEBUG
17
18#include "OsiSolverInterface.hpp"
19#include "CbcModel.hpp"
20#include "CbcMessage.hpp"
21#include "CbcBranchCut.hpp"
22#include "CoinSort.hpp"
23#include "CoinError.hpp"
24#include "CbcBranchAllDifferent.hpp"
25
26/** Default Constructor
27*/
28CbcBranchAllDifferent::CbcBranchAllDifferent()
29  : CbcBranchCut()
30  , numberInSet_(0)
31  , which_(NULL)
32{
33}
34
35/* Useful constructor - passed set of variables
36*/
37CbcBranchAllDifferent::CbcBranchAllDifferent(CbcModel *model, int numberInSet,
38  const int *members)
39  : CbcBranchCut(model)
40{
41  numberInSet_ = numberInSet;
42  which_ = CoinCopyOfArray(members, numberInSet_);
43}
44// Copy constructor
45CbcBranchAllDifferent::CbcBranchAllDifferent(const CbcBranchAllDifferent &rhs)
46  : CbcBranchCut(rhs)
47{
48  numberInSet_ = rhs.numberInSet_;
49  which_ = CoinCopyOfArray(rhs.which_, numberInSet_);
50}
51
52// Clone
53CbcObject *
54CbcBranchAllDifferent::clone() const
55{
56  return new CbcBranchAllDifferent(*this);
57}
58
59// Assignment operator
60CbcBranchAllDifferent &
61CbcBranchAllDifferent::operator=(const CbcBranchAllDifferent &rhs)
62{
63  if (this != &rhs) {
64    CbcBranchCut::operator=(rhs);
65    delete[] which_;
66    numberInSet_ = rhs.numberInSet_;
67    which_ = CoinCopyOfArray(rhs.which_, numberInSet_);
68  }
69  return *this;
70}
71
72// Destructor
73CbcBranchAllDifferent::~CbcBranchAllDifferent()
74{
75  delete[] which_;
76}
77CbcBranchingObject *
78CbcBranchAllDifferent::createCbcBranch(OsiSolverInterface * /*solver*/
79  ,
80  const OsiBranchingInformation * /*info*/,
81  int /*way*/)
82{
83  // by default way must be -1
84  //assert (way==-1);
85  const double *solution = model_->testSolution();
86  double *values = new double[numberInSet_];
87  int *which = new int[numberInSet_];
88  int i;
89  for (i = 0; i < numberInSet_; i++) {
90    int iColumn = which_[i];
91    values[i] = solution[iColumn];
92    which[i] = iColumn;
93  }
94  CoinSort_2(values, values + numberInSet_, which);
95  double last = -1.0;
96  double closest = 1.0;
97  int worst = -1;
98  for (i = 0; i < numberInSet_; i++) {
99    if (values[i] - last < closest) {
100      closest = values[i] - last;
101      worst = i - 1;
102    }
103    last = values[i];
104  }
105  assert(closest <= 0.99999);
106  OsiRowCut down;
107  down.setLb(-COIN_DBL_MAX);
108  down.setUb(-1.0);
109  int pair[2];
110  double elements[] = { 1.0, -1.0 };
111  pair[0] = which[worst];
112  pair[1] = which[worst + 1];
113  delete[] values;
114  delete[] which;
115  down.setRow(2, pair, elements);
116  // up is same - just with rhs changed
117  OsiRowCut up = down;
118  up.setLb(1.0);
119  up.setUb(COIN_DBL_MAX);
120  // Say is not a fix type branch
121  CbcCutBranchingObject *newObject = new CbcCutBranchingObject(model_, down, up, false);
122  if (model_->messageHandler()->logLevel() > 1)
123    printf("creating cut in CbcBranchCut\n");
124  return newObject;
125}
126double
127CbcBranchAllDifferent::infeasibility(const OsiBranchingInformation * /*info*/,
128  int &preferredWay) const
129{
130  preferredWay = -1;
131  //OsiSolverInterface * solver = model_->solver();
132  const double *solution = model_->testSolution();
133  //const double * lower = solver->getColLower();
134  //const double * upper = solver->getColUpper();
135  double *values = new double[numberInSet_];
136  int i;
137  for (i = 0; i < numberInSet_; i++) {
138    int iColumn = which_[i];
139    values[i] = solution[iColumn];
140  }
141  std::sort(values, values + numberInSet_);
142  double last = -1.0;
143  double closest = 1.0;
144  for (i = 0; i < numberInSet_; i++) {
145    if (values[i] - last < closest) {
146      closest = values[i] - last;
147    }
148    last = values[i];
149  }
150  delete[] values;
151  if (closest > 0.99999)
152    return 0.0;
153  else
154    return 0.5 * (1.0 - closest);
155}
156
157/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
158*/
Note: See TracBrowser for help on using the repository browser.