1
#include "functions.h"
2

3 4
std::vector<double> calculateConnectivity(
4
  std::vector<std::size_t> &id1,
5
  std::vector<std::size_t> &id2,
6
  std::vector<double> &boundary,
7
  IntegerMatrix &selection
8
) {
9 4
  std::vector<double> x(3,0.0);
10 4
  for (std::size_t i=0; i<id1.size(); ++i) {
11 4
    if (selection(0,id1[i]) + selection(0,id2[i]) == 2) {
12 4
      x[0]+=boundary[i];
13 4
    } else if (selection(0,id1[i]) + selection(0,id2[i]) == 0) {
14 4
      x[1]+=boundary[i];
15
    } else {
16 4
      x[2]+=boundary[i];
17
    }
18
  }
19 4
  return(x);
20
}
21

22
// unreliable - best space value
23 4
double unreliable_space_value(Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX) {
24 4
  return(weightdistMTX.rowwise().minCoeff().sum());
25
}
26

27
// unreliable - space value for a prioritisation with 1 pu
28 0
double unreliable_space_value(
29
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX,
30
  std::size_t pu_id
31
) {
32 0
  return(weightdistMTX.col(pu_id).sum());
33
}
34

35
// unreliable - space value for a prioritisation with n pu's
36 0
double unreliable_space_value(Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX,std::vector<std::size_t> &pu_ids) {
37 0
  double value=0.0;
38
  double minWDist;
39 0
  for (std::size_t k=0; k<static_cast<std::size_t>(weightdistMTX.rows()); ++k) {
40
    // get index for planning unit closest to demand point
41 0
    minWDist=weightdistMTX(k,pu_ids[0]);
42 0
    for (std::size_t m=1; m<pu_ids.size(); ++m) {
43 0
      minWDist=std::min(minWDist, weightdistMTX(k,pu_ids[m]));
44
    }
45
    // calculate weighted distance value for closest pu
46 0
    value+=minWDist;
47
  }
48 0
  return(value);
49
}
50

51

52
// reliable - best space value
53 4
double reliable_space_value(
54
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX,
55
  Rcpp::NumericVector &pu_probs,
56
  std::size_t maxrlevelINT) {
57 4
  double value=0.0;
58
  double curr_value;
59
  double currProb;
60 4
  std::vector<std::size_t> pu_ids(weightdistMTX.cols()-1);
61 4
  std::iota(pu_ids.begin(), pu_ids.end(), 0);
62 4
  for (std::size_t k=0; k<static_cast<std::size_t>(weightdistMTX.rows()); ++k) {
63
    // sort pus in order of distance
64 4
    std::partial_sort(
65 4
      pu_ids.begin(), pu_ids.begin()+maxrlevelINT, pu_ids.end(),
66 4
      [&](const std::size_t p1, const std::size_t p2) {
67 4
        return(weightdistMTX(k,p1) < weightdistMTX(k,p2));
68
      }
69
    );
70
    // calculate expected weighted distances for real pus
71 4
    currProb=1.0;
72 4
    curr_value=0.0;
73 4
    for (std::size_t r=0; r<maxrlevelINT; ++r) {
74 4
      curr_value+=(pu_probs[pu_ids[r]] * currProb * weightdistMTX(k,pu_ids[r]));
75 4
      currProb*=(1.0 - pu_probs[pu_ids[r]]);
76
    }
77
    // calculate expected weighted distance for failure PU
78 4
    curr_value+=(currProb*weightdistMTX(k, weightdistMTX.cols()-1));
79
    // update value
80 4
    value+=curr_value;
81
  }
82 4
  return(value);
83
}
84

85
// reliable - space value for a prioritisation containing 1 pu
86 0
double reliable_space_value(
87
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX,
88
  std::size_t pu_id,
89
  double pu_prob,
90
  double sum_failure_wdists
91
) {
92
  return(
93 0
      (weightdistMTX.col(pu_id).sum() * pu_prob) +
94 0
      (sum_failure_wdists * (1.0-pu_prob))
95
  );
96
}
97

98
// reliable - space value for a prioritisation containing n pu's
99 0
double reliable_space_value(
100
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> &weightdistMTX,
101
  std::vector<std::size_t> pu_ids,
102
  Rcpp::NumericVector &pu_probs,
103
  std::size_t maxrlevelINT) {
104 0
  double value=0.0;
105
  double currProb;
106
  double curr_value;
107
  
108
  
109 0
  for (std::size_t k=0; k<static_cast<std::size_t>(weightdistMTX.rows()); ++k) {
110
    // sort pus in order of distance
111 0
    std::partial_sort(
112 0
      pu_ids.begin(), pu_ids.begin()+maxrlevelINT, pu_ids.end(),
113 0
      [&](const std::size_t p1, const std::size_t p2) {
114 0
        return(weightdistMTX(k,p1) < weightdistMTX(k,p2));
115
      }
116
    );
117
    // calculate expected weighted distances for real pus
118 0
    currProb=1.0;
119 0
    curr_value=0.0;
120 0
    for (std::size_t r=0; r<maxrlevelINT; ++r) {
121 0
      curr_value+=(pu_probs[pu_ids[r]] * currProb * weightdistMTX(k,pu_ids[r]));
122 0
      currProb*=(1.0 - pu_probs[pu_ids[r]]);
123
    }
124
    // calculate expected weighted distance for failure PU
125 0
    curr_value+=(currProb*weightdistMTX(k, weightdistMTX.cols()-1));
126
    // update value
127 0
    value += curr_value;
128
  }
129 0
  return(value);
130
}
131

132
// calculate euclidean distance
133 4
double distance(double x0, double y0, double x1, double y1) {
134 4
  return(sqrt(std::abs(Pow<2>(x0-x1)) + std::abs(Pow<2>(y0-y1))));
135
}
136

137

Read our documentation on viewing source code .

Loading