1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*                  This file is part of the program and library             */
4 /*         SCIP --- Solving Constraint Integer Programs                      */
5 /*                                                                           */
6 /*    Copyright (C) 2002-2021 Konrad-Zuse-Zentrum                            */
7 /*                            fuer Informationstechnik Berlin                */
8 /*                                                                           */
9 /*  SCIP is distributed under the terms of the ZIB Academic License.         */
10 /*                                                                           */
11 /*  You should have received a copy of the ZIB Academic License              */
12 /*  along with SCIP; see the file COPYING. If not visit scipopt.org.         */
13 /*                                                                           */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file   scip_bandit.c
17  * @ingroup OTHER_CFILES
18  * @brief  public functions for bandit algorithms
19  * @author Gregor Hendel
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/bandit.h"
25 #include "scip/pub_message.h"
26 #include "scip/scip_bandit.h"
27 #include "scip/scip_mem.h"
28 #include "scip/scip_randnumgen.h"
29 #include "scip/set.h"
30 #include "scip/struct_scip.h"
31 #include "scip/pub_message.h"
32 #include "scip/scip_bandit.h"
33 #include "scip/scip_mem.h"
34 #include "scip/scip_randnumgen.h"
35 #include "scip/set.h"
36 #include "scip/struct_scip.h"
37 
38 /** includes a bandit algorithm virtual function table  */
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)))39 SCIP_RETCODE SCIPincludeBanditvtable(
40    SCIP*                 scip,               /**< SCIP data structure */
41    SCIP_BANDITVTABLE**   banditvtable,       /**< bandit algorithm virtual function table */
42    const char*           name,               /**< a name for the algorithm represented by this vtable */
43    SCIP_DECL_BANDITFREE  ((*banditfree)),    /**< callback to free bandit specific data structures */
44    SCIP_DECL_BANDITSELECT((*banditselect)),  /**< selection callback for bandit selector */
45    SCIP_DECL_BANDITUPDATE((*banditupdate)),  /**< update callback for bandit algorithms */
46    SCIP_DECL_BANDITRESET ((*banditreset))    /**< update callback for bandit algorithms */
47    )
48 {
49    SCIP_BANDITVTABLE* vtableptr;
50 
51    assert(scip != NULL);
52    assert(banditvtable != NULL);
53 
54    if( SCIPfindBanditvtable(scip, name) != NULL )
55    {
56       SCIPerrorMessage("bandit VTable <%s> already included.\n", name);
57        return SCIP_INVALIDDATA;
58    }
59 
60    SCIP_CALL( SCIPbanditvtableCreate(&vtableptr, name,
61          banditfree, banditselect, banditupdate, banditreset) );
62 
63    SCIP_CALL( SCIPsetIncludeBanditvtable(scip->set, vtableptr) );
64 
65    *banditvtable = vtableptr;
66 
67    return SCIP_OKAY;
68 }
69 
70 /** returns the bandit virtual function table of the given name, or NULL if not existing */
SCIPfindBanditvtable(SCIP * scip,const char * name)71 SCIP_BANDITVTABLE* SCIPfindBanditvtable(
72    SCIP*                 scip,               /**< SCIP data structure */
73    const char*           name                /**< name of bandit algorithm virtual function table */
74    )
75 {
76    assert(scip != NULL);
77 
78    return SCIPsetFindBanditvtable(scip->set, name);
79 }
80 
81 /** reset the bandit algorithm */
SCIPresetBandit(SCIP * scip,SCIP_BANDIT * bandit,SCIP_Real * priorities,unsigned int seed)82 SCIP_RETCODE SCIPresetBandit(
83    SCIP*                 scip,               /**< SCIP data structure */
84    SCIP_BANDIT*          bandit,             /**< pointer to bandit algorithm data structure */
85    SCIP_Real*            priorities,         /**< priorities for every action, or NULL if not needed */
86    unsigned int          seed                /**< initial random seed for bandit selection */
87    )
88 {
89    assert(scip != NULL);
90    assert(bandit != NULL);
91 
92    SCIP_CALL( SCIPbanditReset(SCIPbuffer(scip), bandit, priorities, SCIPinitializeRandomSeed(scip, seed)) );
93 
94    return SCIP_OKAY;
95 }
96 
97 /** calls destructor and frees memory of bandit algorithm */
SCIPfreeBandit(SCIP * scip,SCIP_BANDIT ** bandit)98 SCIP_RETCODE SCIPfreeBandit(
99    SCIP*                 scip,               /**< SCIP data structure */
100    SCIP_BANDIT**         bandit              /**< pointer to bandit algorithm data structure */
101    )
102 {
103    assert(scip != NULL);
104    assert(bandit != NULL);
105    assert(*bandit != NULL);
106 
107    SCIP_CALL( SCIPbanditFree(SCIPblkmem(scip), bandit) );
108 
109    return SCIP_OKAY;
110 }
111