summaryrefslogtreecommitdiff
path: root/thirdparty/linux/include/coin/CglClique.hpp
blob: 5b47b40a6fd86a59f432e5eedafbccebcfb042ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// $Id: CglClique.hpp 1119 2013-04-06 20:24:18Z stefan $
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef _CglClique_h_
#define _CglClique_h_

#include "CglCutGenerator.hpp"

//class OsiCuts;
//class OsiSolverInterface;

class CglClique : public CglCutGenerator {

    friend void CglCliqueUnitTest(const OsiSolverInterface * siP,
				  const std::string mpdDir );
public:
    /// Copy constructor
    CglClique(const CglClique& rhs);
    /// Clone
    virtual CglCutGenerator * clone() const;

    /// Assignment operator
    CglClique& operator=(const CglClique& rhs);
   
public:
    
    virtual void
    generateCuts(const OsiSolverInterface& si, OsiCuts & cs,
		 const CglTreeInfo info = CglTreeInfo());
   
    /**@name Constructors and destructors */
    //@{
    /** Default constructor.
	If the setPacking argument is set to true then CglClique will assume that the
	problem in the solverinterface passed to the generateCuts() method
	describes a set packing problem, i.e.,
	- all variables are binary
	- the matrix is a 0-1 matrix
	- all constraints are '= 1' or '<= 1'
       
	Otherwise the user can use the considerRows() method to set the list of
	clique rows, that is,
	- all coeffs corresponding to binary variables at fractional level is 1
	- all other coeffs are non-negative
	- the constraint is '= 1' or '<= 1'.

	If the user does not set the list of clique rows then CglClique will
	start the generateCuts() methods by scanning the matrix for them.
	Also justOriginalRows can be set to true to limit clique creation
    */
    CglClique(bool setPacking = false, bool justOriginalRows = false);
    /// Destructor
    virtual ~CglClique() {}
    /// Create C++ lines to get to current state
    virtual std::string generateCpp( FILE * fp);

    void considerRows(const int numRows, const int* rowInd);

public:
    /** possible choices for selecting the next node in the star clique search
     */
    enum scl_next_node_method {
	SCL_MIN_DEGREE,
	SCL_MAX_DEGREE,
	SCL_MAX_XJ_MAX_DEG
    };

    void setStarCliqueNextNodeMethod(scl_next_node_method method) {
	scl_next_node_rule = method;
    }

    void setStarCliqueCandidateLengthThreshold(int maxlen) {
	scl_candidate_length_threshold = maxlen;
    }
    void setRowCliqueCandidateLengthThreshold(int maxlen) {
	rcl_candidate_length_threshold = maxlen;
    }

    void setStarCliqueReport(bool yesno = true) { scl_report_result = yesno; }
    void setRowCliqueReport(bool yesno = true) { rcl_report_result = yesno; }

    void setDoStarClique(bool yesno = true) { do_star_clique = yesno; }
    void setDoRowClique(bool yesno = true) { do_row_clique = yesno; }

    void setMinViolation(double minviol) { petol = minviol; }
    double getMinViolation() const { return petol; }

private:

    struct frac_graph ;
    friend struct frac_graph ;

    /** A node of the fractional graph. There is a node for every variable at
	fractional level. */
    struct fnode {
	/** pointer into all_nbr */
	int          *nbrs;
	/** 1-x_i-x_j, needed for odd holes, in the same order as the adj list,
	    pointer into all_edgecost */
	double       *edgecosts;
	/** degree of the node */
	int           degree;
	/** the fractional value of the variable corresponding to this node */
	double        val;
    };

    /** A graph corresponding to a fractional solution of an LP. Two nodes are
	adjacent iff their columns are non-orthogonal. */
    struct frac_graph {
	/** # of nodes = # of fractional values in the LP solution */
	int    nodenum;
	/** # of edges in the graph */
	int    edgenum;
	/** density= edgenum/(nodenum choose 2) */
	double density;
	int    min_deg_node;
	int    min_degree;
	int    max_deg_node;
	int    max_degree;
	/** The array of the nodes in the graph */
	fnode  *nodes;
	/** The array of all the neighbors. First the indices of the nodes
	    adjacent to node 0 are listed, then those adjacent to node 1, etc. */
	int    *all_nbr;
	/** The array of the costs of the edges going to the neighbors */
	double *all_edgecost;

	frac_graph() :
	    nodenum(0), edgenum(0), density(0),
	    min_deg_node(0), min_degree(0), max_deg_node(0), max_degree(0),
	    nodes(0), all_nbr(0), all_edgecost(0) {}
    };

protected:
    /** An indicator showing whether the whole matrix in the solverinterface is
	a set packing problem or not */
    bool setPacking_;
    /// True if just look at original rows
    bool justOriginalRows_;
    /** pieces of the set packing part of the solverinterface */
    int sp_numrows;
    int* sp_orig_row_ind;
    int sp_numcols;
    int* sp_orig_col_ind;
    double* sp_colsol;
    int* sp_col_start;
    int* sp_col_ind;
    int* sp_row_start;
    int* sp_row_ind;

    /** the intersection graph corresponding to the set packing problem */
    frac_graph fgraph;
    /** the node-node incidence matrix of the intersection graph. */
    bool* node_node;

    /** The primal tolerance in the solverinterface. */
    double petol;

    /** data for the star clique algorithm */

    /** Parameters */
    /**@{*/
    /** whether to do the row clique algorithm or not. */
    bool do_row_clique;
    /** whether to do the star clique algorithm or not. */
    bool do_star_clique;

    /** How the next node to be added to the star clique should be selected */
    scl_next_node_method scl_next_node_rule;
    /** In the star clique method the maximal length of the candidate list
	(those nodes that are in a star, i.e., connected to the center of the
	star) to allow complete enumeration of maximal cliques. Otherwise a
	greedy algorithm is used. */
    int scl_candidate_length_threshold;
    /** whether to give a detailed statistics on the star clique method */
    bool scl_report_result;

    /** In the row clique method the maximal length of the candidate list
	(those nodes that can extend the row clique, i.e., connected to all
	nodes in the row clique) to allow complete enumeration of maximal
	cliques. Otherwise a greedy algorithm is used. */
    int rcl_candidate_length_threshold;
    /** whether to give a detailed statistics on the row clique method */
    bool rcl_report_result;
    /**@}*/

    /** variables/arrays that are used across many methods */
    /**@{*/
    /** List of indices that must be in the to be created clique. This is just
	a pointer, it is never new'd and therefore does not need to be
	delete[]'d either. */
    const int* cl_perm_indices;
    /** The length of cl_perm_indices */
    int cl_perm_length;

    /** List of indices that should be considered for extending the ones listed
	in cl_perm_indices. */
    int* cl_indices;
    /** The length of cl_indices */
    int cl_length;

    /** An array of nodes discarded from the candidate list. These are
	rechecked when a maximal clique is found just to make sure that the
	clique is really maximal. */
    int* cl_del_indices;
    /** The length of cl_del_indices */
    int cl_del_length;

    /**@}*/

private:
    /** Scan through the variables and select those that are binary and are at
	a fractional level. */
    void selectFractionalBinaries(const OsiSolverInterface& si);
    /** Scan through the variables and select those that are at a fractional
	level. We already know that everything is binary. */
    void selectFractionals(const OsiSolverInterface& si);
    /**  */
    void selectRowCliques(const OsiSolverInterface& si,int numOriginalRows);
    /**  */
    void createSetPackingSubMatrix(const OsiSolverInterface& si);
    /**  */
    void createFractionalGraph();
    /**  */
    int createNodeNode();
    /**  */
    void deleteSetPackingSubMatrix();
    /**  */
    void deleteFractionalGraph();
    /**  */
    void find_scl(OsiCuts& cs);
    /**  */
    void find_rcl(OsiCuts& cs);
    /**  */
    int scl_choose_next_node(const int current_nodenum,
			     const int *current_indices,
			     const int *current_degrees,
			     const double *current_values);
    /**  */
    void scl_delete_node(const int del_ind, int& current_nodenum,
			 int *current_indices, int *current_degrees,
			 double *current_values);
    /**  */
    int enumerate_maximal_cliques(int& pos, bool* scl_label, OsiCuts& cs);
    /**  */
    int greedy_maximal_clique(OsiCuts& cs);
    /**  */
    void recordClique(const int len, int* indices, OsiCuts& cs);
};
//#############################################################################
/** A function that tests the methods in the CglClique class. The
    only reason for it not to be a member method is that this way it doesn't
    have to be compiled into the library. And that's a gain, because the
    library should be compiled with optimization on, but this method should be
    compiled with debugging. */
void CglCliqueUnitTest(const OsiSolverInterface * siP,
		       const std::string mpdDir);
/// This works on a fake solver i.e. invented rows
class CglProbing;
class CglFakeClique : public CglClique {
  
public:
  /// Copy constructor
  CglFakeClique(const CglFakeClique& rhs);
  /// Clone
  virtual CglCutGenerator * clone() const;
  
  /// Assignment operator
  CglFakeClique& operator=(const CglFakeClique& rhs);
  
  virtual void
  generateCuts(const OsiSolverInterface& si, OsiCuts & cs,
	       const CglTreeInfo info = CglTreeInfo());
  
  /**@name Constructors and destructors */
  //@{
  /** Default constructor.
      If the setPacking argument is set to true then CglFakeClique will assume that the
      problem in the solverinterface passed to the generateCuts() method
      describes a set packing problem, i.e.,
      - all variables are binary
      - the matrix is a 0-1 matrix
      - all constraints are '= 1' or '<= 1'
      
      Otherwise the user can use the considerRows() method to set the list of
      clique rows, that is,
      - all coeffs corresponding to binary variables at fractional level is 1
      - all other coeffs are non-negative
      - the constraint is '= 1' or '<= 1'.
      
      If the user does not set the list of clique rows then CglFakeClique will
      start the generateCuts() methods by scanning the matrix for them.
  */
  CglFakeClique(OsiSolverInterface * solver=NULL,bool setPacking = false);
  /// Destructor
  virtual ~CglFakeClique();
  /// Assign solver (generator takes over ownership)
  void assignSolver(OsiSolverInterface * fakeSolver);
protected:
  /// fake solver to use
  OsiSolverInterface * fakeSolver_;
  /// Probing object
  CglProbing * probing_;
};

#endif