Scippy

SCIP

Solving Constraint Integer Programs

scip_bandit.c
Go to the documentation of this file.
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 */
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 */
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 */
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 */
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 }
SCIP_RETCODE SCIPbanditvtableCreate(SCIP_BANDITVTABLE **banditvtable, const char *name, SCIP_DECL_BANDITFREE((*banditfree)), SCIP_DECL_BANDITSELECT((*banditselect)), SCIP_DECL_BANDITUPDATE((*banditupdate)), SCIP_DECL_BANDITRESET((*banditreset)))
Definition: bandit.c:245
public methods for memory management
SCIP_RETCODE SCIPsetIncludeBanditvtable(SCIP_SET *set, SCIP_BANDITVTABLE *banditvtable)
Definition: set.c:4480
internal methods for bandit algorithms
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
SCIP_RETCODE SCIPbanditReset(BMS_BUFMEM *bufmem, SCIP_BANDIT *bandit, SCIP_Real *priorities, unsigned int seed)
Definition: bandit.c:109
BMS_BUFMEM * SCIPbuffer(SCIP *scip)
Definition: scip_mem.c:72
#define SCIPerrorMessage
Definition: pub_message.h:64
SCIP_BANDITVTABLE * SCIPsetFindBanditvtable(SCIP_SET *set, const char *name)
Definition: set.c:4502
SCIP_RETCODE SCIPfreeBandit(SCIP *scip, SCIP_BANDIT **bandit)
Definition: scip_bandit.c:107
#define SCIP_DECL_BANDITRESET(x)
Definition: type_bandit.h:82
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip_mem.c:57
SCIP_BANDITVTABLE * SCIPfindBanditvtable(SCIP *scip, const char *name)
Definition: scip_bandit.c:80
#define NULL
Definition: lpi_spx1.cpp:164
internal methods for global SCIP settings
#define SCIP_CALL(x)
Definition: def.h:394
SCIP main data structure.
SCIP_RETCODE SCIPincludeBanditvtable(SCIP *scip, SCIP_BANDITVTABLE **banditvtable, const char *name, SCIP_DECL_BANDITFREE((*banditfree)), SCIP_DECL_BANDITSELECT((*banditselect)), SCIP_DECL_BANDITUPDATE((*banditupdate)), SCIP_DECL_BANDITRESET((*banditreset)))
Definition: scip_bandit.c:48
SCIP_RETCODE SCIPbanditFree(BMS_BLKMEM *blkmem, SCIP_BANDIT **bandit)
Definition: bandit.c:80
public methods for bandit algorithms
public methods for random numbers
#define SCIP_DECL_BANDITFREE(x)
Definition: type_bandit.h:63
SCIP_SET * set
Definition: struct_scip.h:72
public methods for message output
#define SCIP_Real
Definition: def.h:186
SCIP_RETCODE SCIPresetBandit(SCIP *scip, SCIP_BANDIT *bandit, SCIP_Real *priorities, unsigned int seed)
Definition: scip_bandit.c:91
#define SCIP_DECL_BANDITUPDATE(x)
Definition: type_bandit.h:75
#define SCIP_DECL_BANDITSELECT(x)
Definition: type_bandit.h:69
unsigned int SCIPinitializeRandomSeed(SCIP *scip, unsigned int initialseedvalue)