SCIP Doxygen Documentation
Loading...
Searching...
No Matches
heur_trivial.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-2026 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 heur_trivial.c
26 * @ingroup DEFPLUGINS_HEUR
27 * @brief trivial primal heuristic
28 * @author Timo Berthold
29 */
30
31/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32
33#include "scip/heur_trivial.h"
34#include "scip/pub_heur.h"
35#include "scip/pub_message.h"
36#include "scip/pub_var.h"
37#include "scip/scip_heur.h"
38#include "scip/scip_message.h"
39#include "scip/scip_numerics.h"
40#include "scip/scip_prob.h"
41#include "scip/scip_sol.h"
43
44
45#define HEUR_NAME "trivial"
46#define HEUR_DESC "start heuristic which tries some trivial solutions"
47#define HEUR_DISPCHAR SCIP_HEURDISPCHAR_TRIVIAL
48#define HEUR_PRIORITY 10000
49#define HEUR_FREQ 0
50#define HEUR_FREQOFS 0
51#define HEUR_MAXDEPTH -1
52#define HEUR_TIMING SCIP_HEURTIMING_BEFOREPRESOL | SCIP_HEURTIMING_BEFORENODE
53#define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
54
55/*
56 * Local methods
57 */
58
59/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
60static
61SCIP_DECL_HEURCOPY(heurCopyTrivial)
62{ /*lint --e{715}*/
63 assert(scip != NULL);
64 assert(heur != NULL);
65
67
68 /* call inclusion method of primal heuristic */
70
71 return SCIP_OKAY;
72}
73
74
75/** execution method of primal heuristic */
76static
77SCIP_DECL_HEUREXEC(heurExecTrivial)
78{ /*lint --e{715}*/
79 SCIP_VAR** vars;
80 SCIP_SOL* zerosol; /* solution where all variables are set next to zero within bounds */
81 SCIP_SOL* lbsol; /* solution where all variables are set to their lower bounds */
82 SCIP_SOL* ubsol; /* solution where all variables are set to their upper bounds */
83 SCIP_SOL* locksol; /* solution where all variables are set to the bound with the fewer locks */
84 SCIP_Real large;
85 SCIP_Bool difflb;
86 SCIP_Bool diffub;
87 SCIP_Bool difflock;
88 SCIP_Bool success;
89 int nvars;
90 int i;
91
93
94 /* initialize data structure */
95 SCIP_CALL( SCIPcreateSol(scip, &zerosol, heur) );
96 SCIP_CALL( SCIPcreateSol(scip, &lbsol, heur) );
97 SCIP_CALL( SCIPcreateSol(scip, &ubsol, heur) );
98 SCIP_CALL( SCIPcreateSol(scip, &locksol, heur) );
99
100 /* determine large value to set variables to */
101 large = SCIPround(scip, MIN(1.0 / SCIPfeastol(scip), SCIPgetHugeValue(scip)) / 10.0); /*lint !e666 */
102
103 /* check zero solution once */
104 difflb = FALSE;
105 diffub = FALSE;
106 difflock = FALSE;
107
109 assert(vars != NULL || nvars == 0);
110
111 for( i = 0; i < nvars; ++i )
112 {
113 SCIP_Real lb;
114 SCIP_Real ub;
115 SCIP_Real zeroval;
116 SCIP_Real solval;
117
118 assert(vars != NULL); /* this assert is needed for flexelint */
119
120 lb = SCIPvarGetLbLocal(vars[i]);
121 ub = SCIPvarGetUbLocal(vars[i]);
122
123 /* if problem is obviously infeasible due to empty domain, stop */
124 if( SCIPisFeasGT(scip, lb, ub) )
125 goto TERMINATE;
126
127 /* set bounds to sufficient large value */
128 if( SCIPisInfinity(scip, -lb) )
129 lb = MIN(-large, ub);
130 if( SCIPisInfinity(scip, ub) )
131 ub = MAX(large, lb);
132
133 /* set value next to zero within bounds */
134 zeroval = MAX(MIN(0.0, ub), lb);
135
136 /* set value to the bound with fewer locks, if tie choose an average value */
138 solval = lb;
140 solval = ub;
141 else
142 {
143 solval = (lb+ub)/2.0;
144
145 /* if a tie occurs, roughly every third integer variable will be rounded up */
146 if( SCIPvarIsIntegral(vars[i]) )
147 solval = i % 3 == 0 ? SCIPceil(scip,solval) : SCIPfloor(scip,solval);
148
150 }
151
152 if( !SCIPisEQ(scip, lb, zeroval) )
153 difflb = TRUE;
154
155 if( !SCIPisEQ(scip, ub, zeroval) )
156 diffub = TRUE;
157
158 if( !SCIPisEQ(scip, solval, zeroval) )
159 difflock = TRUE;
160
161 /* set variable to values */
162 SCIP_CALL( SCIPsetSolVal(scip, zerosol, vars[i], zeroval) );
163 SCIP_CALL( SCIPsetSolVal(scip, lbsol, vars[i], lb) );
164 SCIP_CALL( SCIPsetSolVal(scip, ubsol, vars[i], ub) );
165 SCIP_CALL( SCIPsetSolVal(scip, locksol, vars[i], solval) );
166 }
167
168 /* try zero solution */
169 SCIPdebugMsg(scip, "try zero solution\n");
170 SCIP_CALL( SCIPtrySol(scip, zerosol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
171
172 if( success )
173 {
174 SCIPdebugMsg(scip, "found feasible zero solution:\n");
175 SCIPdebug( SCIP_CALL( SCIPprintSol(scip, zerosol, NULL, FALSE) ) );
176
178 }
179
180 /* try lower bound solution */
181 if( difflb )
182 {
183 SCIPdebugMsg(scip, "try lower bound solution\n");
184 SCIP_CALL( SCIPtrySol(scip, lbsol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
185
186 if( success )
187 {
188 SCIPdebugMsg(scip, "found feasible lower bound solution:\n");
190
192 }
193 }
194
195 /* try upper bound solution */
196 if( diffub )
197 {
198 SCIPdebugMsg(scip, "try upper bound solution\n");
199 SCIP_CALL( SCIPtrySol(scip, ubsol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
200
201 if( success )
202 {
203 SCIPdebugMsg(scip, "found feasible upper bound solution:\n");
205
207 }
208 }
209
210 /* try lock solution */
211 if( difflock )
212 {
213 SCIPdebugMsg(scip, "try lock solution\n");
214 SCIP_CALL( SCIPtrySol(scip, locksol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
215
216 if( success )
217 {
218 SCIPdebugMsg(scip, "found feasible lock solution:\n");
219 SCIPdebug( SCIP_CALL( SCIPprintSol(scip, locksol, NULL, FALSE) ) );
220
222 }
223 }
224
225TERMINATE:
226 /* free solutions */
227 SCIP_CALL( SCIPfreeSol(scip, &locksol) );
228 SCIP_CALL( SCIPfreeSol(scip, &ubsol) );
229 SCIP_CALL( SCIPfreeSol(scip, &lbsol) );
230 SCIP_CALL( SCIPfreeSol(scip, &zerosol) );
231
232 return SCIP_OKAY;
233}
234
235
236/*
237 * primal heuristic specific interface methods
238 */
239
240/** creates the trivial primal heuristic and includes it in SCIP */
242 SCIP* scip /**< SCIP data structure */
243 )
244{
245 SCIP_HEUR* heur;
246
247 /* include primal heuristic */
250 HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecTrivial, NULL) );
251
252 assert(heur != NULL);
253
254 /* primal heuristic is safe to use in exact solving mode */
255 SCIPheurMarkExact(heur);
256
257 /* set non-NULL pointers to callback methods */
258 SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyTrivial) );
259
260 return SCIP_OKAY;
261}
#define NULL
Definition def.h:257
#define SCIP_Bool
Definition def.h:100
#define MIN(x, y)
Definition def.h:233
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define SCIP_Real
Definition def.h:165
#define TRUE
Definition def.h:102
#define FALSE
Definition def.h:103
#define MAX(x, y)
Definition def.h:229
#define SCIP_CALL(x)
Definition def.h:364
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition scip_prob.c:2115
#define SCIPdebugMsg
SCIP_RETCODE SCIPincludeHeurTrivial(SCIP *scip)
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition scip_heur.c:122
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur,)
Definition scip_heur.c:167
void SCIPheurMarkExact(SCIP_HEUR *heur)
Definition heur.c:1457
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition heur.c:1467
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition scip_sol.c:2351
SCIP_RETCODE SCIPtrySol(SCIP *scip, SCIP_SOL *sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition scip_sol.c:4017
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
Definition scip_sol.c:1569
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPround(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPfeastol(SCIP *scip)
SCIP_Real SCIPgetHugeValue(SCIP *scip)
SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
int SCIPvarGetNLocksUpType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition var.c:4380
SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
Definition var.c:24300
SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
Definition var.c:23522
SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
Definition var.c:24266
int SCIPvarGetNLocksDownType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition var.c:4322
#define HEUR_TIMING
return SCIP_OKAY
#define HEUR_FREQOFS
#define HEUR_DESC
#define HEUR_DISPCHAR
#define HEUR_MAXDEPTH
#define HEUR_PRIORITY
SCIPfreeSol(scip, &heurdata->sol))
#define HEUR_NAME
#define HEUR_FREQ
#define HEUR_USESSUBSCIP
SCIPcreateSol(scip, &heurdata->sol, heur))
assert(minobj< SCIPgetCutoffbound(scip))
int nvars
static SCIP_VAR ** vars
trivial primal heuristic
public methods for primal heuristics
public methods for message output
#define SCIPdebug(x)
Definition pub_message.h:93
public methods for problem variables
public methods for primal heuristic plugins and divesets
public methods for message handling
public methods for numerical tolerances
public methods for global and local (sub)problems
public methods for solutions
public methods for querying solving statistics
#define SCIP_DECL_HEURCOPY(x)
Definition type_heur.h:97
struct SCIP_Heur SCIP_HEUR
Definition type_heur.h:76
#define SCIP_DECL_HEUREXEC(x)
Definition type_heur.h:163
@ SCIP_DIDNOTFIND
Definition type_result.h:44
@ SCIP_FOUNDSOL
Definition type_result.h:56
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39
struct SCIP_Sol SCIP_SOL
Definition type_sol.h:57
struct SCIP_Var SCIP_VAR
Definition type_var.h:166
@ SCIP_LOCKTYPE_MODEL
Definition type_var.h:141