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_pricer.c
26   	 * @ingroup OTHER_CFILES
27   	 * @brief  public methods for variable pricer 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/debug.h"
46   	#include "scip/pricer.h"
47   	#include "scip/pub_message.h"
48   	#include "scip/scip_pricer.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 variable pricer and includes it in SCIP
55   	 *  To use the variable pricer for solving a problem, it first has to be activated with a call to SCIPactivatePricer().
56   	 *  This should be done during the problem creation stage.
57   	 *
58   	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
59   	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
60   	 *
61   	 *  @pre This method can be called if SCIP is in one of the following stages:
62   	 *       - \ref SCIP_STAGE_INIT
63   	 *       - \ref SCIP_STAGE_PROBLEM
64   	 *
65   	 *  @note method has all pricer callbacks as arguments and is thus changed every time a new callback is added
66   	 *        in future releases; consider using SCIPincludePricerBasic() and setter functions
67   	 *        if you seek for a method which is less likely to change in future releases
68   	 */
69   	SCIP_RETCODE SCIPincludePricer(
70   	   SCIP*                 scip,               /**< SCIP data structure */
71   	   const char*           name,               /**< name of variable pricer */
72   	   const char*           desc,               /**< description of variable pricer */
73   	   int                   priority,           /**< priority of the variable pricer */
74   	   SCIP_Bool             delay,              /**< should the pricer be delayed until no other pricers or already existing
75   	                                              *   problem variables with negative reduced costs are found?
76   	                                              *   if this is set to FALSE it may happen that the pricer produces columns
77   	                                              *   that already exist in the problem (which are also priced in by the
78   	                                              *   default problem variable pricing in the same round) */
79   	   SCIP_DECL_PRICERCOPY  ((*pricercopy)),    /**< copy method of variable pricer or NULL if you don't want to copy your plugin into sub-SCIPs */
80   	   SCIP_DECL_PRICERFREE  ((*pricerfree)),    /**< destructor of variable pricer */
81   	   SCIP_DECL_PRICERINIT  ((*pricerinit)),    /**< initialize variable pricer */
82   	   SCIP_DECL_PRICEREXIT  ((*pricerexit)),    /**< deinitialize variable pricer */
83   	   SCIP_DECL_PRICERINITSOL((*pricerinitsol)),/**< solving process initialization method of variable pricer */
84   	   SCIP_DECL_PRICEREXITSOL((*pricerexitsol)),/**< solving process deinitialization method of variable pricer */
85   	   SCIP_DECL_PRICERREDCOST((*pricerredcost)),/**< reduced cost pricing method of variable pricer for feasible LPs */
86   	   SCIP_DECL_PRICERFARKAS((*pricerfarkas)),  /**< Farkas pricing method of variable pricer for infeasible LPs */
87   	   SCIP_PRICERDATA*      pricerdata          /**< variable pricer data */
88   	   )
89   	{
90   	   SCIP_PRICER* pricer;
91   	
92   	   SCIP_CALL( SCIPcheckStage(scip, "SCIPincludePricer", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
93   	
94   	   /* check whether pricer is already present */
95   	   if( SCIPfindPricer(scip, name) != NULL )
96   	   {
97   	      SCIPerrorMessage("pricer <%s> already included.\n", name);
98   	      return SCIP_INVALIDDATA;
99   	   }
100  	
101  	   SCIP_CALL( SCIPpricerCreate(&pricer, scip->set, scip->messagehdlr, scip->mem->setmem,
102  	         name, desc, priority, delay,
103  	         pricercopy,
104  	         pricerfree, pricerinit, pricerexit, pricerinitsol, pricerexitsol, pricerredcost, pricerfarkas, pricerdata) );
105  	   SCIP_CALL( SCIPsetIncludePricer(scip->set, pricer) );
106  	
107  	   return SCIP_OKAY;
108  	}
109  	
110  	/** creates a variable pricer and includes it in SCIP with all non-fundamental callbacks set to NULL;
111  	 *  if needed, these can be added afterwards via setter functions SCIPsetPricerCopy(), SCIPsetPricerFree(),
112  	 *  SCIPsetPricerInity(), SCIPsetPricerExit(), SCIPsetPricerInitsol(), SCIPsetPricerExitsol(),
113  	 *  SCIPsetPricerFarkas();
114  	 *
115  	 *  To use the variable pricer for solving a problem, it first has to be activated with a call to SCIPactivatePricer().
116  	 *  This should be done during the problem creation stage.
117  	 *
118  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
119  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
120  	 *
121  	 *  @pre This method can be called if SCIP is in one of the following stages:
122  	 *       - \ref SCIP_STAGE_INIT
123  	 *       - \ref SCIP_STAGE_PROBLEM
124  	 *
125  	 *  @note if you want to set all callbacks with a single method call, consider using SCIPincludePricer() instead
126  	 */
127  	SCIP_RETCODE SCIPincludePricerBasic(
128  	   SCIP*                 scip,               /**< SCIP data structure */
129  	   SCIP_PRICER**         pricerptr,          /**< reference to a pricer, or NULL */
130  	   const char*           name,               /**< name of variable pricer */
131  	   const char*           desc,               /**< description of variable pricer */
132  	   int                   priority,           /**< priority of the variable pricer */
133  	   SCIP_Bool             delay,              /**< should the pricer be delayed until no other pricers or already existing
134  	                                              *   problem variables with negative reduced costs are found?
135  	                                              *   if this is set to FALSE it may happen that the pricer produces columns
136  	                                              *   that already exist in the problem (which are also priced in by the
137  	                                              *   default problem variable pricing in the same round) */
138  	   SCIP_DECL_PRICERREDCOST((*pricerredcost)),/**< reduced cost pricing method of variable pricer for feasible LPs */
139  	   SCIP_DECL_PRICERFARKAS((*pricerfarkas)),  /**< Farkas pricing method of variable pricer for infeasible LPs */
140  	   SCIP_PRICERDATA*      pricerdata          /**< variable pricer data */
141  	   )
142  	{
143  	   SCIP_PRICER* pricer;
144  	
145  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPincludePricerBasic", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
146  	
147  	   /* check whether pricer is already present */
148  	   if( SCIPfindPricer(scip, name) != NULL )
149  	   {
150  	      SCIPerrorMessage("pricer <%s> already included.\n", name);
151  	      return SCIP_INVALIDDATA;
152  	   }
153  	
154  	   SCIP_CALL( SCIPpricerCreate(&pricer, scip->set, scip->messagehdlr, scip->mem->setmem,
155  	         name, desc, priority, delay,
156  	         NULL,
157  	         NULL, NULL, NULL, NULL, NULL, pricerredcost, pricerfarkas, pricerdata) );
158  	   SCIP_CALL( SCIPsetIncludePricer(scip->set, pricer) );
159  	
160  	   if( pricerptr != NULL )
161  	      *pricerptr = pricer;
162  	
163  	   return SCIP_OKAY;
164  	}
165  	
166  	/** sets copy method of pricer
167  	 *
168  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
169  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
170  	 *
171  	 *  @pre This method can be called if SCIP is in one of the following stages:
172  	 *       - \ref SCIP_STAGE_INIT
173  	 *       - \ref SCIP_STAGE_PROBLEM
174  	 */
175  	SCIP_RETCODE SCIPsetPricerCopy(
176  	   SCIP*                 scip,               /**< SCIP data structure */
177  	   SCIP_PRICER*          pricer,             /**< pricer */
178  	   SCIP_DECL_PRICERCOPY ((*pricercopy))     /**< copy method of pricer or NULL if you don't want to copy your plugin into sub-SCIPs */
179  	   )
180  	{
181  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerCopy", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
182  	
183  	   assert(pricer != NULL);
184  	
185  	   SCIPpricerSetCopy(pricer, pricercopy);
186  	
187  	   return SCIP_OKAY;
188  	}
189  	
190  	/** sets destructor method of pricer
191  	 *
192  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
193  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
194  	 *
195  	 *  @pre This method can be called if SCIP is in one of the following stages:
196  	 *       - \ref SCIP_STAGE_INIT
197  	 *       - \ref SCIP_STAGE_PROBLEM
198  	 */
199  	SCIP_RETCODE SCIPsetPricerFree(
200  	   SCIP*                 scip,               /**< SCIP data structure */
201  	   SCIP_PRICER*          pricer,             /**< pricer */
202  	   SCIP_DECL_PRICERFREE ((*pricerfree))      /**< destructor of pricer */
203  	   )
204  	{
205  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerFree", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
206  	
207  	   assert(pricer != NULL);
208  	
209  	   SCIPpricerSetFree(pricer, pricerfree);
210  	
211  	   return SCIP_OKAY;
212  	}
213  	
214  	/** sets initialization method of pricer
215  	 *
216  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
217  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
218  	 *
219  	 *  @pre This method can be called if SCIP is in one of the following stages:
220  	 *       - \ref SCIP_STAGE_INIT
221  	 *       - \ref SCIP_STAGE_PROBLEM
222  	 */
223  	SCIP_RETCODE SCIPsetPricerInit(
224  	   SCIP*                 scip,               /**< SCIP data structure */
225  	   SCIP_PRICER*          pricer,             /**< pricer */
226  	   SCIP_DECL_PRICERINIT  ((*pricerinit))     /**< initialize pricer */
227  	   )
228  	{
229  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerInit", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
230  	
231  	   assert(pricer != NULL);
232  	
233  	   SCIPpricerSetInit(pricer, pricerinit);
234  	
235  	   return SCIP_OKAY;
236  	}
237  	
238  	/** sets deinitialization method of pricer
239  	 *
240  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
241  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
242  	 *
243  	 *  @pre This method can be called if SCIP is in one of the following stages:
244  	 *       - \ref SCIP_STAGE_INIT
245  	 *       - \ref SCIP_STAGE_PROBLEM
246  	 */
247  	SCIP_RETCODE SCIPsetPricerExit(
248  	   SCIP*                 scip,               /**< SCIP data structure */
249  	   SCIP_PRICER*          pricer,             /**< pricer */
250  	   SCIP_DECL_PRICEREXIT  ((*pricerexit))     /**< deinitialize pricer */
251  	   )
252  	{
253  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerExit", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
254  	
255  	   assert(pricer != NULL);
256  	
257  	   SCIPpricerSetExit(pricer, pricerexit);
258  	
259  	   return SCIP_OKAY;
260  	}
261  	
262  	/** sets solving process initialization method of pricer
263  	 *
264  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
265  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
266  	 *
267  	 *  @pre This method can be called if SCIP is in one of the following stages:
268  	 *       - \ref SCIP_STAGE_INIT
269  	 *       - \ref SCIP_STAGE_PROBLEM
270  	 */
271  	SCIP_RETCODE SCIPsetPricerInitsol(
272  	   SCIP*                 scip,               /**< SCIP data structure */
273  	   SCIP_PRICER*          pricer,             /**< pricer */
274  	   SCIP_DECL_PRICERINITSOL ((*pricerinitsol))/**< solving process initialization method of pricer */
275  	   )
276  	{
277  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerInitsol", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
278  	
279  	   assert(pricer != NULL);
280  	
281  	   SCIPpricerSetInitsol(pricer, pricerinitsol);
282  	
283  	   return SCIP_OKAY;
284  	}
285  	
286  	/** sets solving process deinitialization method of pricer
287  	 *
288  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
289  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
290  	 *
291  	 *  @pre This method can be called if SCIP is in one of the following stages:
292  	 *       - \ref SCIP_STAGE_INIT
293  	 *       - \ref SCIP_STAGE_PROBLEM
294  	 */
295  	SCIP_RETCODE SCIPsetPricerExitsol(
296  	   SCIP*                 scip,               /**< SCIP data structure */
297  	   SCIP_PRICER*          pricer,             /**< pricer */
298  	   SCIP_DECL_PRICEREXITSOL((*pricerexitsol)) /**< solving process deinitialization method of pricer */
299  	   )
300  	{
301  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPsetPricerExitsol", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
302  	
303  	   assert(pricer != NULL);
304  	
305  	   SCIPpricerSetExitsol(pricer, pricerexitsol);
306  	
307  	   return SCIP_OKAY;
308  	}
309  	
310  	/** returns the variable pricer of the given name, or NULL if not existing */
311  	SCIP_PRICER* SCIPfindPricer(
312  	   SCIP*                 scip,               /**< SCIP data structure */
313  	   const char*           name                /**< name of variable pricer */
314  	   )
315  	{
316  	   assert(scip != NULL);
317  	   assert(scip->set != NULL);
318  	   assert(name != NULL);
319  	
320  	   return SCIPsetFindPricer(scip->set, name);
321  	}
322  	
323  	/** returns the array of currently available variable pricers; active pricers are in the first slots of the array */
324  	SCIP_PRICER** SCIPgetPricers(
325  	   SCIP*                 scip                /**< SCIP data structure */
326  	   )
327  	{
328  	   assert(scip != NULL);
329  	   assert(scip->set != NULL);
330  	
331  	   SCIPsetSortPricers(scip->set);
332  	
333  	   return scip->set->pricers;
334  	}
335  	
336  	/** returns the number of currently available variable pricers */
337  	int SCIPgetNPricers(
338  	   SCIP*                 scip                /**< SCIP data structure */
339  	   )
340  	{
341  	   assert(scip != NULL);
342  	   assert(scip->set != NULL);
343  	
344  	   return scip->set->npricers;
345  	}
346  	
347  	/** returns the number of currently active variable pricers, that are used in the LP solving loop */
348  	int SCIPgetNActivePricers(
349  	   SCIP*                 scip                /**< SCIP data structure */
350  	   )
351  	{
352  	   assert(scip != NULL);
353  	   assert(scip->set != NULL);
354  	
355  	   return scip->set->nactivepricers;
356  	}
357  	
358  	/** sets the priority priority of a variable pricer */
359  	SCIP_RETCODE SCIPsetPricerPriority(
360  	   SCIP*                 scip,               /**< SCIP data structure */
361  	   SCIP_PRICER*          pricer,             /**< variable pricer */
362  	   int                   priority            /**< new priority of the variable pricer */
363  	   )
364  	{
365  	   assert(scip != NULL);
366  	   assert(scip->set != NULL);
367  	
368  	   SCIPpricerSetPriority(pricer, scip->set, priority);
369  	
370  	   return SCIP_OKAY;
371  	}
372  	
373  	/** activates pricer to be used for the current problem
374  	 *  This method should be called during the problem creation stage for all pricers that are necessary to solve
375  	 *  the problem model.
376  	 *  The pricers are automatically deactivated when the problem is freed.
377  	 *
378  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
379  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
380  	 *
381  	 *  @pre This method can be called if SCIP is in one of the following stages:
382  	 *       - \ref SCIP_STAGE_PROBLEM
383  	 */
384  	SCIP_RETCODE SCIPactivatePricer(
385  	   SCIP*                 scip,               /**< SCIP data structure */
386  	   SCIP_PRICER*          pricer              /**< variable pricer */
387  	   )
388  	{
389  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPactivatePricer", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) );
390  	
391  	   SCIP_CALL( SCIPpricerActivate(pricer, scip->set) );
392  	
393  	   return SCIP_OKAY;
394  	}
395  	
396  	/** deactivates pricer
397  	 *
398  	 *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
399  	 *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
400  	 *
401  	 *  @pre This method can be called if SCIP is in one of the following stages:
402  	 *       - \ref SCIP_STAGE_PROBLEM
403  	 *       - \ref SCIP_STAGE_SOLVING
404  	 *       - \ref SCIP_STAGE_EXITSOLVE
405  	 */
406  	SCIP_RETCODE SCIPdeactivatePricer(
407  	   SCIP*                 scip,               /**< SCIP data structure */
408  	   SCIP_PRICER*          pricer              /**< variable pricer */
409  	   )
410  	{
411  	   SCIP_CALL( SCIPcheckStage(scip, "SCIPdeactivatePricer", FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE) );
412  	
413  	   SCIP_CALL( SCIPpricerDeactivate(pricer, scip->set) );
414  	
415  	   return SCIP_OKAY;
416  	}
417