Execute preliminary calculations in a conservation problem and store the results for later use. This function is useful when creating slightly different versions of the same conservation planning problem that involve the same pre-processing steps (e.g. calculating boundary data), because means that the same calculations will not be run multiple times.

run_calculations(x)

Arguments

x

ConservationProblem-class object

Value

Invisible TRUE indicating success.

Details

This function is used for the effect of modifying the input ConservationProblem-class object. As such, it does not return anything. To use this function with pipe operators, use the %T>% operator and not the %>% operator.

Examples

# Let us imagine a scenario where we wanted to understand the effect of # setting different targets on our solution. # create a conservation problem with no targets p <- problem(sim_pu_raster, sim_features) %>% add_min_set_objective() %>% add_boundary_penalties(10, 0.5) # create a copies of p and add targets p1 <- p %>% add_relative_targets(0.1) p2 <- p %>% add_relative_targets(0.2) p3 <- p %>% add_relative_targets(0.3) # now solve each of the different problems and record the time spent # solving them s1 <- system.time({solve(p1); solve(p2); solve(p3)})
#> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [3e+00, 8e+00] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 2352.9617505 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 1.945078e+03, 97 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 1945.07827 0 12 2352.96175 1945.07827 17.3% - 0s #> H 0 0 2006.3478307 1945.07827 3.05% - 0s #> #> Explored 1 nodes (97 simplex iterations) in 0.00 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 2006.35 2352.96 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 2.006347830733e+03, best bound 1.945078273075e+03, gap 3.0538% #> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [6e+00, 2e+01] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 4565.4850483 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 3.927147e+03, 92 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 3927.14693 0 15 4565.48505 3927.14693 14.0% - 0s #> H 0 0 4014.0258012 3927.14693 2.16% - 0s #> #> Explored 1 nodes (92 simplex iterations) in 0.00 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 4014.03 4565.49 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 4.014025801209e+03, best bound 3.927146930435e+03, gap 2.1644% #> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [9e+00, 2e+01] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 6599.7631720 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 5.932506e+03, 121 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 5932.50572 0 16 6599.76317 5932.50572 10.1% - 0s #> H 0 0 6096.9115786 5932.50572 2.70% - 0s #> #> Explored 1 nodes (121 simplex iterations) in 0.01 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 6096.91 6599.76 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 6.096911578554e+03, best bound 5.932505717873e+03, gap 2.6965%
# This approach is inefficient. Since these problems all share the same # planning units it is actually performing the same calculations three times. # To avoid this, we can use the "run_calculations" function before creating # the copies. Normally, R runs the calculations just before solving the # problem # recreate a conservation problem with no targets and tell R run the # preliminary calculations. Note how we use the \%T>\% operator here. p <- problem(sim_pu_raster, sim_features) %>% add_min_set_objective() %>% add_boundary_penalties(10, 0.5) %T>% run_calculations() # create a copies of p and add targets just like before p1 <- p %>% add_relative_targets(0.1) p2 <- p %>% add_relative_targets(0.2) p3 <- p %>% add_relative_targets(0.3) # solve each of the different problems and record the time spent # solving them s2 <- system.time({solve(p1); solve(p2); solve(p3)})
#> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [3e+00, 8e+00] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 2352.9617505 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 1.945078e+03, 97 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 1945.07827 0 12 2352.96175 1945.07827 17.3% - 0s #> H 0 0 2006.3478307 1945.07827 3.05% - 0s #> #> Explored 1 nodes (97 simplex iterations) in 0.01 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 2006.35 2352.96 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 2.006347830733e+03, best bound 1.945078273075e+03, gap 3.0538% #> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [6e+00, 2e+01] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 4565.4850483 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 3.927147e+03, 92 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 3927.14693 0 15 4565.48505 3927.14693 14.0% - 0s #> H 0 0 4014.0258012 3927.14693 2.16% - 0s #> #> Explored 1 nodes (92 simplex iterations) in 0.01 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 4014.03 4565.49 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 4.014025801209e+03, best bound 3.927146930435e+03, gap 2.1644% #> Optimize a model with 293 rows, 234 columns and 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Coefficient statistics: #> Matrix range [2e-01, 1e+00] #> Objective range [2e+00, 2e+02] #> Bounds range [1e+00, 1e+00] #> RHS range [9e+00, 2e+01] #> Found heuristic solution: objective 18523.196992 #> Found heuristic solution: objective 6599.7631720 #> Presolve time: 0.00s #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> Variable types: 0 continuous, 234 integer (234 binary) #> Presolved: 293 rows, 234 columns, 1026 nonzeros #> #> #> Root relaxation: objective 5.932506e+03, 121 iterations, 0.00 seconds #> #> Nodes | Current Node | Objective Bounds | Work #> Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time #> #> 0 0 5932.50572 0 16 6599.76317 5932.50572 10.1% - 0s #> H 0 0 6096.9115786 5932.50572 2.70% - 0s #> #> Explored 1 nodes (121 simplex iterations) in 0.01 seconds #> Thread count was 1 (of 4 available processors) #> #> Solution count 3: 6096.91 6599.76 18523.2 #> #> Optimal solution found (tolerance 1.00e-01) #> Best objective 6.096911578554e+03, best bound 5.932505717873e+03, gap 2.6965%
# now lets compare the times print(s1) # time spent without running preliminary calculations
#> user system elapsed #> 0.669 0.383 1.049
print(s2) # time spent after running preliminary calculations
#> user system elapsed #> 0.565 0.332 0.887
# As we can see, we can save a lot of time by running the preliminary # calculations before making copies of the problem with slightly # different constraints.