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_bandit.c
26   	 * @ingroup OTHER_CFILES
27   	 * @brief  public functions for bandit algorithms
28   	 * @author Gregor Hendel
29   	 */
30   	
31   	/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32   	
33   	#include "scip/bandit.h"
34   	#include "scip/pub_message.h"
35   	#include "scip/scip_bandit.h"
36   	#include "scip/scip_mem.h"
37   	#include "scip/scip_randnumgen.h"
38   	#include "scip/set.h"
39   	#include "scip/struct_scip.h"
40   	#include "scip/pub_message.h"
41   	#include "scip/scip_bandit.h"
42   	#include "scip/scip_mem.h"
43   	#include "scip/scip_randnumgen.h"
44   	#include "scip/set.h"
45   	#include "scip/struct_scip.h"
46   	
47   	/** includes a bandit algorithm virtual function table  */
48   	SCIP_RETCODE SCIPincludeBanditvtable(
49   	   SCIP*                 scip,               /**< SCIP data structure */
50   	   SCIP_BANDITVTABLE**   banditvtable,       /**< bandit algorithm virtual function table */
51   	   const char*           name,               /**< a name for the algorithm represented by this vtable */
52   	   SCIP_DECL_BANDITFREE  ((*banditfree)),    /**< callback to free bandit specific data structures */
53   	   SCIP_DECL_BANDITSELECT((*banditselect)),  /**< selection callback for bandit selector */
54   	   SCIP_DECL_BANDITUPDATE((*banditupdate)),  /**< update callback for bandit algorithms */
55   	   SCIP_DECL_BANDITRESET ((*banditreset))    /**< update callback for bandit algorithms */
56   	   )
57   	{
58   	   SCIP_BANDITVTABLE* vtableptr;
59   	
60   	   assert(scip != NULL);
61   	   assert(banditvtable != NULL);
62   	
63   	   if( SCIPfindBanditvtable(scip, name) != NULL )
64   	   {
65   	      SCIPerrorMessage("bandit VTable <%s> already included.\n", name);
66   	       return SCIP_INVALIDDATA;
67   	   }
68   	
69   	   SCIP_CALL( SCIPbanditvtableCreate(&vtableptr, name,
70   	         banditfree, banditselect, banditupdate, banditreset) );
71   	
72   	   SCIP_CALL( SCIPsetIncludeBanditvtable(scip->set, vtableptr) );
73   	
74   	   *banditvtable = vtableptr;
75   	
76   	   return SCIP_OKAY;
77   	}
78   	
79   	/** returns the bandit virtual function table of the given name, or NULL if not existing */
80   	SCIP_BANDITVTABLE* SCIPfindBanditvtable(
81   	   SCIP*                 scip,               /**< SCIP data structure */
82   	   const char*           name                /**< name of bandit algorithm virtual function table */
83   	   )
84   	{
85   	   assert(scip != NULL);
86   	
87   	   return SCIPsetFindBanditvtable(scip->set, name);
88   	}
89   	
90   	/** reset the bandit algorithm */
91   	SCIP_RETCODE SCIPresetBandit(
92   	   SCIP*                 scip,               /**< SCIP data structure */
93   	   SCIP_BANDIT*          bandit,             /**< pointer to bandit algorithm data structure */
94   	   SCIP_Real*            priorities,         /**< priorities for every action, or NULL if not needed */
95   	   unsigned int          seed                /**< initial random seed for bandit selection */
96   	   )
97   	{
98   	   assert(scip != NULL);
99   	   assert(bandit != NULL);
100  	
101  	   SCIP_CALL( SCIPbanditReset(SCIPbuffer(scip), bandit, priorities, SCIPinitializeRandomSeed(scip, seed)) );
102  	
103  	   return SCIP_OKAY;
104  	}
105  	
106  	/** calls destructor and frees memory of bandit algorithm */
107  	SCIP_RETCODE SCIPfreeBandit(
108  	   SCIP*                 scip,               /**< SCIP data structure */
109  	   SCIP_BANDIT**         bandit              /**< pointer to bandit algorithm data structure */
110  	   )
111  	{
112  	   assert(scip != NULL);
113  	   assert(bandit != NULL);
114  	   assert(*bandit != NULL);
115  	
116  	   SCIP_CALL( SCIPbanditFree(SCIPblkmem(scip), bandit) );
117  	
118  	   return SCIP_OKAY;
119  	}
120