-
Notifications
You must be signed in to change notification settings - Fork 25
/
gurobi_installation.html
349 lines (318 loc) · 30.6 KB
/
gurobi_installation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gurobi Installation Guide • prioritizr</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/flatly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><!-- docsearch --><script src="../docsearch.js"></script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.3/docsearch.min.css" integrity="sha256-QOSRU/ra9ActyXkIBbiIB144aDBdtvXBcNc3OTNuX/Q=" crossorigin="anonymous">
<link href="../docsearch.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.min.js" integrity="sha256-4HLtjeVgH0eIB3aZ9mLYF6E8oU5chNdjU6p6rrXpl9U=" crossorigin="anonymous"></script><link href="../extra.css" rel="stylesheet">
<meta property="og:title" content="Gurobi Installation Guide">
<meta property="og:description" content="prioritizr">
<meta property="og:image" content="https://prioritizr.net/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-70466840-4"></script><script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-70466840-4');
</script>
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container template-article">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">prioritizr</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">7.1.1</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="../articles/prioritizr.html">Get started</a>
</li>
<li>
<a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Articles
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="../articles/gurobi_installation.html">Gurobi Installation Guide</a>
</li>
<li>
<a href="../articles/publication_record.html">Publication Record</a>
</li>
<li>
<a href="../articles/saltspring.html">Salt Spring Island Tutorial</a>
</li>
<li>
<a href="../articles/solver_benchmark.html">Solver Benchmarks</a>
</li>
<li>
<a href="../articles/tasmania.html">Tasmania Tutorial</a>
</li>
<li>
<a href="../articles/zones.html">Management Zones</a>
</li>
</ul>
</li>
<li>
<a href="../news/index.html">Changelog</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/prioritizr/prioritizr/" class="external-link">
<span class="fab fa-github fa-lg"></span>
</a>
</li>
</ul>
<form class="navbar-form navbar-right hidden-xs hidden-sm" role="search">
<div class="form-group">
<input type="search" class="form-control" name="search-input" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off">
</div>
</form>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><script src="gurobi_installation_files/header-attrs-2.11/header-attrs.js"></script><div class="row">
<div class="col-md-9 contents">
<div class="page-header toc-ignore">
<h1 data-toc-skip>Gurobi Installation Guide</h1>
<h4 data-toc-skip class="author">Richard Schuster</h4>
<h4 data-toc-skip class="date">2021-10-26</h4>
<small class="dont-index">Source: <a href="https://github.com/prioritizr/prioritizr/blob/master/vignettes/gurobi_installation.Rmd" class="external-link"><code>vignettes/gurobi_installation.Rmd</code></a></small>
<div class="hidden name"><code>gurobi_installation.Rmd</code></div>
</div>
<div id="introduction" class="section level2">
<h2 class="hasAnchor">
<a href="#introduction" class="anchor" aria-hidden="true"></a>Introduction</h2>
<p><em>Gurobi</em> is the most powerful and fastest solver that the <em>prioritizr R</em> package can use to solve conservation planning problems. This vignette will walk you through the process of setting up <em>Gurobi</em> on your computer so that you can use it to solve conservation planning problems. If you encounter any problems while following the instructions below, check out the <a href="https://www.gurobi.com/documentation/" class="external-link">official <em>Gurobi</em> documentation</a>.</p>
</div>
<div id="obtaining-a-license" class="section level2">
<h2 class="hasAnchor">
<a href="#obtaining-a-license" class="anchor" aria-hidden="true"></a>Obtaining a license</h2>
<p><em>Gurobi</em> is a commercial computer program. <a href="https://www.gurobi.com/products/licensing-pricing/licensing-overview" class="external-link">This means that users will need to obtain a license for <em>Gurobi</em> before they can use it</a>. Although academics can obtain a special license at no cost, individuals that are not affiliated with a recognized educational institution may need to purchase a license. If you are an academic that is affiliated with a recognized educational institution, you can take advantage of the <a href="https://www.gurobi.com/downloads/end-user-license-agreement-academic/" class="external-link">special academic license</a> to use <em>Gurobi</em> for no cost. Once you have signed up for a free account you can request a <a href="https://www.gurobi.com/downloads/end-user-license-agreement-academic/" class="external-link">free academic license</a>.</p>
<br><center>
<img src="figures/free-academic-license-page.png" style="width:75.0%">
</center>
<p><br></p>
<p>Once you accept the Terms Of Service you can generate a license.</p>
<br><center>
<img src="figures/actual-license.png" style="width:75.0%">
</center>
<p><br></p>
<p>Now, copy and save the <code>grbgetkey XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX</code> command for later use.</p>
</div>
<div id="downloading-the-software" class="section level2">
<h2 class="hasAnchor">
<a href="#downloading-the-software" class="anchor" aria-hidden="true"></a>Downloading the software</h2>
<p>After obtaining a license, you will need to download the <em>Gurobi</em> installer to your computer. To achieve this, visit the <a href="https://www.gurobi.com/products/gurobi-optimizer/" class="external-link"><em>Gurobi</em> downloads web page</a> and download the correct version of the installer for your operating system.</p>
</div>
<div id="software-installation" class="section level2">
<h2 class="hasAnchor">
<a href="#software-installation" class="anchor" aria-hidden="true"></a>Software installation</h2>
<p>The process for installing the <em>Gurobi</em> software depends on the operating system on your computer. Fortunately, <em>Gurobi</em> provide platform-specific <a href="https://www.gurobi.com/documentation/" class="external-link">“Quick Start Guides”</a> for <a href="https://www.gurobi.com/documentation/9.0/quickstart_windows/software_installation_guid.html#section:Installation" class="external-link">Windows</a>, <a href="https://www.gurobi.com/documentation/9.0/quickstart_mac/software_installation_guid.html" class="external-link">MacOS</a>, and <a href="https://www.gurobi.com/documentation/9.0/quickstart_linux/software_installation_guid.html" class="external-link">Linux</a> systems that should help with this. Briefly, on Windows systems, you just need to double-click on the <em>Gurobi</em> installer, follow the prompts, and the installer will automatically handle everything for you. On Linux and MacOS systems, you will need to manually extract the downloaded file’s contents to a folder, move the extracted contents to a suitable location (typically <em>/opt/gurobi</em>), and update your system’s variables so that it knows where to find <em>Gurobi</em> (i.e. the <code>PATH</code> variable).</p>
<p>Additionally, if you are using <a href="https://www.rstudio.com/products/rstudio/" class="external-link"><em>RStudio</em></a> on a Linux system, you might need to add the following text to a Rstudio configuration file (located at <code>/etc/rstudio/rserver.conf</code>).</p>
<pre><code>rsession-ld-library-path=/opt/gurobi650/linux64/lib</code></pre>
<p>After installing the <em>Gurobi</em> software suite on your computer, you will need to activate your license.</p>
</div>
<div id="license-activation" class="section level2">
<h2 class="hasAnchor">
<a href="#license-activation" class="anchor" aria-hidden="true"></a>License activation</h2>
<p>Now we will activate the <em>Gurobi</em> software using the license you obtained earlier. Please note that the correct set of instructions depends on your system and license. To activate the license, simply copy and paste the <code>grbgetkey</code> command into your computer’s command prompt or terminal (note that Windows users can open the command prompt by typing <code>cmd</code> in the search box and pressing the <code>enter</code> key). After running the <code>grbgetkey</code> command with the correct license code, you should see output that resembles the following screen shot.</p>
<br><center>
<img src="figures/cmd-windows-success.png" style="width:75.0%">
</center>
<p><br></p>
<p>Next, we will now check that the license has been successfully activated. To achieve this, we will try running <em>Gurobi</em> directly from the command line. Note that the following commands assume you are using version 8.0.0 of <em>Gurobi</em>, and so you will need to modify the command if you are using a more recent version (e.g. if using version 9.1.2, then use <code>gurobi912</code> instead of <code>gurobi800</code> below).</p>
<p>On Windows systems, users can type in the following system command to check their license activation.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">gurobi_cl</span> c:<span class="dt">\g</span>urobi800<span class="dt">\w</span>in64<span class="dt">\e</span>xamples<span class="dt">\d</span>ata<span class="dt">\c</span>oins.lp</span></code></pre></div>
<p>On Linux and MacOS systems, users can type in the following system command.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">gurobi_cl</span> /opt/gurobi800/linux64/examples/data/coins.lp</span></code></pre></div>
<p>If the license was successfully activated, you should see output that resembles the screen shot below.</p>
<br><center>
<img src="figures/model-test.png" style="width:75.0%">
</center>
<p><br></p>
<p>After activating the license, you now need to install the <em>gurobi R</em> package. This is so that you can access the <em>Gurobi</em> software from within the R statistical computing environment, and enable the <em>prioritizr</em> package to interface with the <em>Gurobi</em> software.</p>
</div>
<div id="r-package-installation" class="section level2">
<h2 class="hasAnchor">
<a href="#r-package-installation" class="anchor" aria-hidden="true"></a><em>R</em> package installation</h2>
<p>Now we will install the <em>gurobi R</em> package. This package is not available on the Comprehensive R Archive Network and is instead distributed with the <em>Gurobi</em> software suite. Specifically, the <em>gurobi</em> <em>R</em> package should be located within the folder where you installed the <em>Gurobi</em> software suite. We will install the <em>gurobi</em> <em>R</em> package by running the following <em>R</em> code within your <em>R</em> session. Note that the following code assumes that you are using version 8.0.0 of <em>Gurobi</em>, and so you will need to modify the code if you are using a more recent version (e.g. if using version 9.1.2, then use <code>gurobi912</code> instead of <code>gurobi800</code> below).</p>
<p>Assuming you installed <em>Gurobi</em> in the default location, Windows users can install <em>gurobi</em> <em>R</em> package using the following code.</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="st">"c:/gurobi800/win64/R/gurobi_8.0-0.zip"</span>, repos <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></code></pre></div>
<p>Similarly, Linux and MacOS users can install the <em>gurobi</em> <em>R</em> package using the following code.</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/file.path.html" class="external-link">file.path</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/Sys.getenv.html" class="external-link">Sys.getenv</a></span><span class="op">(</span><span class="st">"GUROBI_HOME"</span><span class="op">)</span>,
<span class="st">"R/gurobi_8.0-0_R_x86_64-pc-linux-gnu.tar.gz"</span><span class="op">)</span>,
repos <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></code></pre></div>
<p>Next, you will need to install the <em>slam R</em> package because the <em>gurobi</em> <em>R</em> package needs this package to work. Users of all platforms (i.e. Windows, Linux, and MacOS) can install the package using the following <em>R</em> code.</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="st">"slam"</span>, repos <span class="op">=</span> <span class="st">"https://cloud.r-project.org"</span><span class="op">)</span></code></pre></div>
<p>Let’s check that the <em>gurobi</em> <em>R</em> package has been successfully installed. To do this, we can try using the <em>gurobi R</em> package to solve an optimization problem. Copy and paste the <em>R</em> code below into <em>R</em>.</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># load gurobi package</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://www.gurobi.com" class="external-link">gurobi</a></span><span class="op">)</span></code></pre></div>
<pre><code>## Loading required package: slam</code></pre>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># create optimization problem</span>
<span class="va">model</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="op">)</span>
<span class="va">model</span><span class="op">$</span><span class="va">obj</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fl">1</span>, <span class="fl">2</span><span class="op">)</span>
<span class="va">model</span><span class="op">$</span><span class="va">modelsense</span> <span class="op"><-</span> <span class="st">"max"</span>
<span class="va">model</span><span class="op">$</span><span class="va">rhs</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">4</span>, <span class="fl">1</span><span class="op">)</span>
<span class="va">model</span><span class="op">$</span><span class="va">sense</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"<"</span>, <span class="st">">"</span><span class="op">)</span>
<span class="va">model</span><span class="op">$</span><span class="va">vtype</span> <span class="op"><-</span> <span class="st">"B"</span>
<span class="va">model</span><span class="op">$</span><span class="va">A</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/matrix.html" class="external-link">matrix</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fl">2</span>, <span class="fl">3</span>, <span class="fl">1</span>, <span class="fl">1</span>, <span class="fl">0</span><span class="op">)</span>, nrow <span class="op">=</span> <span class="fl">2</span>, ncol <span class="op">=</span> <span class="fl">3</span>,
byrow <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>
<span class="co"># solve the optimization problem using Gurobi</span>
<span class="va">result</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/pkg/gurobi/man/gurobi.html" class="external-link">gurobi</a></span><span class="op">(</span><span class="va">model</span>, <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<pre><code>## Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (linux64)
## Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
## Optimize a model with 2 rows, 3 columns and 5 nonzeros
## Model fingerprint: 0xba2d0add
## Variable types: 0 continuous, 3 integer (3 binary)
## Coefficient statistics:
## Matrix range [1e+00, 3e+00]
## Objective range [1e+00, 2e+00]
## Bounds range [0e+00, 0e+00]
## RHS range [1e+00, 4e+00]
## Found heuristic solution: objective 2.0000000
## Presolve removed 2 rows and 3 columns
## Presolve time: 0.00s
## Presolve: All rows and columns removed
##
## Explored 0 nodes (0 simplex iterations) in 0.00 seconds
## Thread count was 1 (of 8 available processors)
##
## Solution count 2: 3 2
##
## Optimal solution found (tolerance 1.00e-04)
## Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%</code></pre>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># print the solution</span>
<span class="fu"><a href="../reference/print.html">print</a></span><span class="op">(</span><span class="va">result</span><span class="op">$</span><span class="va">objval</span><span class="op">)</span> <span class="co"># objective</span></code></pre></div>
<pre><code>## [1] 3</code></pre>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="../reference/print.html">print</a></span><span class="op">(</span><span class="va">result</span><span class="op">$</span><span class="va">x</span><span class="op">)</span> <span class="co"># decision variables</span></code></pre></div>
<pre><code>## [1] 1 0 1</code></pre>
<p>If you see the outputs for <code>result$objval</code> and <code>result$x</code> and you don’t see any error messages, then you have (1) successfully installed the <em>Gurobi</em> software suite, (2) activated a valid license, and (3) successfully installed the <em>gurobi R</em> package. If do see an error message, then you might have missed a previous step or something might have gone wrong while installing <em>Gurobi</em> or activating the license. In such cases, try going back through this vignette and repeating the previous steps to see if that fixes the issue.</p>
</div>
<div id="solving-a-prioritzr-problem-with-gurobi" class="section level2">
<h2 class="hasAnchor">
<a href="#solving-a-prioritzr-problem-with-gurobi" class="anchor" aria-hidden="true"></a>Solving a <em>prioritzr</em> problem with <em>Gurobi</em>
</h2>
<p>If you successfully installed the <em>Gurobi</em> software suite and the <em>gurobi</em> <em>R</em> package, you can now try solving conservation planning problems using the <em>prioritzr</em> <em>R</em> package. Although the <em>prioritizr</em> <em>R</em> package should automatically detect that <em>Gurobi</em> has been installed, you can use the function <code>add_gurobi_solver</code> to manually specify that <em>Gurobi</em> should be used to solve problems. This function is also useful because you can use it to customize the optimization process (e.g. specify the desired optimality gap or set a limit on how much time should be spent searching for a solution).</p>
<p>Finally, to check that everything has been installed correctly, we will use the <em>Gurobi</em> software suite to solve a reserve selection problem created using the <em>prioritzr</em> <em>R</em> package.</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://prioritizr.net">prioritizr</a></span><span class="op">)</span>
<span class="co"># formulate the problem</span>
<span class="va">p</span> <span class="op"><-</span> <span class="fu"><a href="../reference/problem.html">problem</a></span><span class="op">(</span><span class="va">sim_pu_raster</span>, <span class="va">sim_features</span><span class="op">)</span> <span class="op"><a href="../reference/pipe.html">%>%</a></span>
<span class="fu"><a href="../reference/add_min_set_objective.html">add_min_set_objective</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="../reference/pipe.html">%>%</a></span>
<span class="fu"><a href="../reference/add_relative_targets.html">add_relative_targets</a></span><span class="op">(</span><span class="fl">0.1</span><span class="op">)</span> <span class="op"><a href="../reference/pipe.html">%>%</a></span>
<span class="fu"><a href="../reference/add_binary_decisions.html">add_binary_decisions</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="../reference/pipe.html">%>%</a></span>
<span class="fu"><a href="../reference/add_gurobi_solver.html">add_gurobi_solver</a></span><span class="op">(</span><span class="op">)</span>
<span class="co"># solve the problem</span>
<span class="va">s</span> <span class="op"><-</span> <span class="fu"><a href="../reference/solve.html">solve</a></span><span class="op">(</span><span class="va">p</span><span class="op">)</span></code></pre></div>
<pre><code>## Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (linux64)
## Thread count: 4 physical cores, 8 logical processors, using up to 1 threads
## Optimize a model with 5 rows, 90 columns and 450 nonzeros
## Model fingerprint: 0x6442bf6e
## Variable types: 0 continuous, 90 integer (90 binary)
## Coefficient statistics:
## Matrix range [2e-01, 9e-01]
## Objective range [2e+02, 2e+02]
## Bounds range [1e+00, 1e+00]
## RHS range [3e+00, 8e+00]
## Found heuristic solution: objective 2337.9617505
## Presolve time: 0.00s
## Presolved: 5 rows, 90 columns, 450 nonzeros
## Variable types: 0 continuous, 90 integer (90 binary)
## Presolved: 5 rows, 90 columns, 450 nonzeros
##
##
## Root relaxation: objective 1.931582e+03, 12 iterations, 0.00 seconds
##
## Nodes | Current Node | Objective Bounds | Work
## Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
##
## 0 0 1931.58191 0 4 2337.96175 1931.58191 17.4% - 0s
## H 0 0 1987.3985265 1931.58191 2.81% - 0s
##
## Explored 1 nodes (12 simplex iterations) in 0.00 seconds
## Thread count was 1 (of 8 available processors)
##
## Solution count 2: 1987.4 2337.96
##
## Optimal solution found (tolerance 1.00e-01)
## Best objective 1.987398526526e+03, best bound 1.931581908865e+03, gap 2.8085%</code></pre>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># plot solution</span>
<span class="fu"><a href="https://rdrr.io/pkg/raster/man/plot.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">s</span>, col <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"grey90"</span>, <span class="st">"darkgreen"</span><span class="op">)</span>, main <span class="op">=</span> <span class="st">"Solution"</span>,
xlim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="op">-</span><span class="fl">0.1</span>, <span class="fl">1.1</span><span class="op">)</span>, ylim <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="op">-</span><span class="fl">0.1</span>, <span class="fl">1.1</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<p><img src="gurobi_installation_files/figure-html/unnamed-chunk-9-1.png" width="336" style="display: block; margin: auto;"></p>
<p>After running this code, hopefully, you should some information printed on-screen about the optimization process and <em>R</em> should produce a map displaying a solution. If this code does not produce any errors, then you have successfully installed everything and can begin using <em>Gurobi</em> and the <em>prioritizr R</em> package to solve your very own conservation planning problems.</p>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
<nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
</nav>
</div>
</div>
<footer><div class="copyright">
<p></p>
<p>Developed by <a href="https://jeffrey-hanson.com" class="external-link external-link">Jeffrey O Hanson</a>, <a href="http://www.richard-schuster.com" class="external-link external-link">Richard Schuster</a>, Nina Morrell, <a href="http://strimas.com" class="external-link external-link">Matthew Strimas-Mackey</a>, Brandon P M Edwards, Matthew E Watts, <a href="https://arcese.forestry.ubc.ca" class="external-link external-link">Peter Arcese</a>, <a href="https://josephrbennett.wordpress.com" class="external-link external-link">Joseph Bennett</a>, <a href="http://www.possinghamlab.org" class="external-link external-link">Hugh P Possingham</a>.</p>
</div>
<div class="pkgdown">
<p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link external-link">pkgdown</a> 1.6.1.9001.</p>
</div>
</footer>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.1/docsearch.min.js" integrity="sha256-GKvGqXDznoRYHCwKXGnuchvKSwmx9SRMrZOTh2g4Sb0=" crossorigin="anonymous"></script><script>
docsearch({
apiKey: '486efa122ea6783724263412c5f28ab3',
indexName: 'prioritizr',
inputSelector: 'input#search-input.form-control',
transformData: function(hits) {
return hits.map(function (hit) {
hit.url = updateHitURL(hit);
return hit;
});
}
});
</script>
</body>
</html>