1    	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2    	/*                                                                           */
3    	/*                  This file is part of the program and library             */
4    	/*         SCIP --- Solving Constraint Integer Programs                      */
5    	/*                                                                           */
6    	/*  Copyright (c) 2002-2023 Zuse Institute Berlin (ZIB)                      */
7    	/*                                                                           */
8    	/*  Licensed under the Apache License, Version 2.0 (the "License");          */
9    	/*  you may not use this file except in compliance with the License.         */
10   	/*  You may obtain a copy of the License at                                  */
11   	/*                                                                           */
12   	/*      http://www.apache.org/licenses/LICENSE-2.0                           */
13   	/*                                                                           */
14   	/*  Unless required by applicable law or agreed to in writing, software      */
15   	/*  distributed under the License is distributed on an "AS IS" BASIS,        */
16   	/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17   	/*  See the License for the specific language governing permissions and      */
18   	/*  limitations under the License.                                           */
19   	/*                                                                           */
20   	/*  You should have received a copy of the Apache-2.0 license                */
21   	/*  along with SCIP; see the file LICENSE. If not visit scipopt.org.         */
22   	/*                                                                           */
23   	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24   	
25   	/**@file   scip_compr.c
26   	 * @ingroup OTHER_CFILES
27   	 * @brief  public methods for compression plugins
28   	 * @author Tobias Achterberg
29   	 * @author Timo Berthold
30   	 * @author Gerald Gamrath
31   	 * @author Leona Gottwald
32   	 * @author Stefan Heinz
33   	 * @author Gregor Hendel
34   	 * @author Thorsten Koch
35   	 * @author Alexander Martin
36   	 * @author Marc Pfetsch
37   	 * @author Michael Winkler
38   	 * @author Kati Wolter
39   	 *
40   	 * @todo check all SCIP_STAGE_* switches, and include the new stages TRANSFORMED and INITSOLVE
41   	 */
42   	
43   	/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
44   	
45   	#include "scip/compr.h"
46   	#include "scip/debug.h"
47   	#include "scip/pub_message.h"
48   	#include "scip/scip_compr.h"
49   	#include "scip/set.h"
50   	#include "scip/struct_mem.h"
51   	#include "scip/struct_scip.h"
52   	#include "scip/struct_set.h"
53   	
54   	/** creates a tree compression and includes it in SCIP.
55   	 *
56   	 *  @note method has all compression callbacks as arguments and is thus changed every time a new
57   	 *        callback is added in future releases; consider using SCIPincludeComprBasic() and setter functions
58   	 *        if you seek for a method which is less likely to change in future releases
59   	 */
60   	SCIP_RETCODE SCIPincludeCompr(
61   	   SCIP*                 scip,               /**< SCIP data structure */
62   	   const char*           name,               /**< name of tree compression */
63   	   const char*           desc,               /**< description of tree compression */
64   	   int                   priority,           /**< priority of the tree compression */
65   	   int                   minnnodes,          /**< minimal number of nodes to call compression */
66   	   SCIP_DECL_COMPRCOPY   ((*comprcopy)),     /**< copy method of tree compression or NULL if you don't want to copy your plugin into sub-SCIPs */
67   	   SCIP_DECL_COMPRFREE   ((*comprfree)),     /**< destructor of tree compression */
68   	   SCIP_DECL_COMPRINIT   ((*comprinit)),     /**< initialize tree compression */
69   	   SCIP_DECL_COMPREXIT   ((*comprexit)),     /**< deinitialize tree compression */
70   	   SCIP_DECL_COMPRINITSOL ((*comprinitsol)), /**< solving process initialization method of tree compression */
71   	   SCIP_DECL_COMPREXITSOL ((*comprexitsol)), /**< solving process deinitialization method of tree compression */
72   	   SCIP_DECL_COMPREXEC   ((*comprexec)),     /**< execution method of tree compression */
73   	   SCIP_COMPRDATA*       comprdata           /**< tree compression data */
74   	   )
75   	{
76   	   SCIP_COMPR* compr;
77   	
78   	   SCIP_CALL( SCIPcheckStage(scip, "SCIPincludeCompr", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
79   	
80   	   /* check whether compression is already present */
81   	   if( SCIPfindCompr(scip, name) != NULL )
82   	   {
83   	      SCIPerrorMessage("compression <%s> already included.\n", name);
84   	      return SCIP_INVALIDDATA;
85   	   }
86   	
87   	   SCIP_CALL( SCIPcomprCreate(&compr, scip->set, scip->messagehdlr, scip->mem->setmem, name, desc, priority, minnnodes,
88   	         comprcopy, comprfree, comprinit, comprexit, comprinitsol, comprexitsol, comprexec, comprdata) );
89   	
90   	   SCIP_CALL( SCIPsetIncludeCompr(scip->set, compr) );
91   	
92   	   return SCIP_OKAY;
93   	}
94   	
95   	/** creates a tree compression and includes it in SCIP with its most fundamental callbacks.
96   	 *  All non-fundamental (or optional) callbacks
97   	 *  as, e. g., init and exit callbacks, will be set to NULL.
98   	 *  Optional callbacks can be set via specific setter functions, see SCIPsetComprCopy(), SCIPsetComprFree(),
99   	 *  SCIPsetComprInit(), SCIPsetComprExit(), SCIPsetComprInitsol(), and SCIPsetComprExitsol()
100  	 *
101  	 *  @note if you want to set all callbacks with a single method call, consider using SCIPincludeCompr() instead
102  	 */
103  	SCIP_RETCODE SCIPincludeComprBasic(
104  	   SCIP*                 scip,               /**< SCIP data structure */
105  	   SCIP_COMPR**          compr,              /**< pointer to tree compression */
106  	   const char*           name,               /**< name of tree compression */
107  	   const char*           desc,               /**< description of tree compression */
108  	   int                   priority,           /**< priority of the tree compression */
109  	   int                   minnnodes,          /**< minimal number of nodes to call the compression */
110  	   SCIP_DECL_COMPREXEC   ((*comprexec)),     /**< execution method of tree compression */
111  	   SCIP_COMPRDATA*       comprdata           /**< tree compression data */
112  	   )
113  	{
114  	   SCIP_COMPR* comprptr;
115  	
116  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPincludeComprBasic", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
117  	
118  	   /* check whether heuristic is already present */
119  	   if( SCIPfindCompr(scip, name) != NULL )
120  	   {
121  	      SCIPerrorMessage("tree compression <%s> already included.\n", name);
122  	      return SCIP_INVALIDDATA;
123  	   }
124  	
125  	   SCIP_CALL( SCIPcomprCreate(&comprptr, scip->set, scip->messagehdlr, scip->mem->setmem, name, desc, priority,
126  	         minnnodes, NULL, NULL, NULL, NULL, NULL, NULL, comprexec, comprdata) );
127  	
128  	   assert(comprptr != NULL);
129  	
130  	   SCIP_CALL( SCIPsetIncludeCompr(scip->set, comprptr) );
131  	
132  	   if( compr != NULL )
133  	      *compr = comprptr;
134  	
135  	   return SCIP_OKAY;
136  	}
137  	
138  	/* new callback/method setter methods */
139  	
140  	/** sets copy method of tree compression */
141  	SCIP_RETCODE SCIPsetComprCopy(
142  	   SCIP*                 scip,               /**< SCIP data structure */
143  	   SCIP_COMPR*           compr,              /**< tree compression */
144  	   SCIP_DECL_COMPRCOPY   ((*comprcopy))      /**< copy method of tree compression or NULL if you don't want to copy your plugin into sub-SCIPs */
145  	   )
146  	{
147  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprCopy", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
148  	
149  	   assert(compr != NULL);
150  	
151  	   SCIPcomprSetCopy(compr, comprcopy);
152  	
153  	   return SCIP_OKAY;
154  	}
155  	
156  	/** sets destructor method of tree compression */
157  	SCIP_RETCODE SCIPsetComprFree(
158  	   SCIP*                 scip,               /**< SCIP data structure */
159  	   SCIP_COMPR*           compr,              /**< tree compression */
160  	   SCIP_DECL_COMPRFREE   ((*comprfree))      /**< destructor of tree compression */
161  	   )
162  	{
163  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprFree", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
164  	
165  	   assert(compr != NULL);
166  	
167  	   SCIPcomprSetFree(compr, comprfree);
168  	
169  	   return SCIP_OKAY;
170  	}
171  	
172  	/** sets initialization method of tree compression */
173  	SCIP_RETCODE SCIPsetComprInit(
174  	   SCIP*                 scip,               /**< SCIP data structure */
175  	   SCIP_COMPR*           compr,              /**< tree compression */
176  	   SCIP_DECL_COMPRINIT   ((*comprinit))      /**< initialize tree compression */
177  	   )
178  	{
179  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprInit", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
180  	
181  	   assert(compr != NULL);
182  	
183  	   SCIPcomprSetInit(compr, comprinit);
184  	
185  	   return SCIP_OKAY;
186  	}
187  	
188  	/** sets deinitialization method of tree compression */
189  	SCIP_RETCODE SCIPsetComprExit(
190  	   SCIP*                 scip,               /**< SCIP data structure */
191  	   SCIP_COMPR*           compr,              /**< tree compression */
192  	   SCIP_DECL_COMPREXIT   ((*comprexit))      /**< deinitialize tree compression */
193  	   )
194  	{
195  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprExit", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
196  	
197  	   assert(compr != NULL);
198  	
199  	   SCIPcomprSetExit(compr, comprexit);
200  	
201  	   return SCIP_OKAY;
202  	}
203  	
204  	/** sets solving process initialization method of tree compression */
205  	SCIP_RETCODE SCIPsetComprInitsol(
206  	   SCIP*                 scip,               /**< SCIP data structure */
207  	   SCIP_COMPR*           compr,              /**< tree compression */
208  	   SCIP_DECL_COMPRINITSOL ((*comprinitsol))  /**< solving process initialization method of tree compression */
209  	   )
210  	{
211  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprInitsol", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
212  	
213  	   assert(compr != NULL);
214  	
215  	   SCIPcomprSetInitsol(compr, comprinitsol);
216  	
217  	   return SCIP_OKAY;
218  	}
219  	
220  	/** sets solving process deinitialization method of tree compression */
221  	SCIP_RETCODE SCIPsetComprExitsol(
222  	   SCIP*                 scip,               /**< SCIP data structure */
223  	   SCIP_COMPR*           compr,              /**< tree compression */
224  	   SCIP_DECL_COMPREXITSOL ((*comprexitsol))  /**< solving process deinitialization method of tree compression */
225  	   )
226  	{
227  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetComprExitsol", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
228  	
229  	   assert(compr != NULL);
230  	
231  	   SCIPcomprSetExitsol(compr, comprexitsol);
232  	
233  	   return SCIP_OKAY;
234  	}
235  	
236  	/** returns the tree compression of the given name, or NULL if not existing */
237  	SCIP_COMPR* SCIPfindCompr(
238  	   SCIP*                 scip,               /**< SCIP data structure */
239  	   const char*           name                /**< name of tree compression */
240  	   )
241  	{
242  	   assert(scip != NULL);
243  	   assert(scip->set != NULL);
244  	   assert(name != NULL);
245  	
246  	   return SCIPsetFindCompr(scip->set, name);
247  	}
248  	
249  	/** returns the array of currently available tree compression */
250  	SCIP_COMPR** SCIPgetComprs(
251  	   SCIP*                 scip                /**< SCIP data structure */
252  	   )
253  	{
254  	   assert(scip != NULL);
255  	   assert(scip->set != NULL);
256  	
257  	   SCIPsetSortComprs(scip->set);
258  	
259  	   return scip->set->comprs;
260  	}
261  	
262  	/** returns the number of currently available tree compression */
263  	int SCIPgetNCompr(
264  	   SCIP*                 scip                /**< SCIP data structure */
265  	   )
266  	{
267  	   assert(scip != NULL);
268  	   assert(scip->set != NULL);
269  	
270  	   return scip->set->ncomprs;
271  	}
272  	
273  	/** set the priority of a tree compression method */
274  	SCIP_RETCODE SCIPsetComprPriority(
275  	   SCIP*                 scip,               /**< SCIP data structure */
276  	   SCIP_COMPR*           compr,              /**< compression */
277  	   int                   priority            /**< new priority of the tree compression */
278  	   )
279  	{
280  	   assert(scip != NULL);
281  	   assert(scip->set != NULL);
282  	
283  	   SCIPcomprSetPriority(compr, scip->set, priority);
284  	
285  	   return SCIP_OKAY;
286  	}
287