Skip to content

Instantly share code, notes, and snippets.

@pelly-ryu
Created July 19, 2019 12:23
Show Gist options
  • Save pelly-ryu/6c75d95158368ac9d380d1732ad7d576 to your computer and use it in GitHub Desktop.
Save pelly-ryu/6c75d95158368ac9d380d1732ad7d576 to your computer and use it in GitHub Desktop.
void reinvestment(double *a, double *b, /*int dh,*/ int re) {
double * arr1;
arr1 = (double *)malloc(sizeof(double) *Dimension);
double * arr2;
arr2 = (double *)malloc(sizeof(double) *Dimension);
for (int i = 0; i < Dimension; i++) {
arr1[i] = *(a + i);
arr2[i] = *(b + i);
}
double * arr3;
arr3 = (double *)malloc(sizeof(double) *Dimension);
double chance;
double maximum = 0;
//int hash_ratio[3] = { 1, 5, 10 };
double wealth_ratio[4] = { 0.001, 0.01, 0.1, 0.5 };
for (int i = 0; i < Dimension; i++) {
arr3[i] = 0;
}
for (int i = 0; i < Dimension; i++) {
if (maximum < arr2[i]) {
maximum = arr2[i];
}
}
for (int i = 0; i < Dimension; i++) {
chance = rand() / (double)RAND_MAX;
if (/*arr2[i] > 5 &&*/ chance <= arr2[i] / maximum) {
arr1[i] += /*hash_ratio[dh]* */ wealth_ratio[re] * arr2[i];
arr3[i] = arr2[i] * wealth_ratio[re];
arr2[i] -= arr2[i] * wealth_ratio[re];
}
}
for (int i = 0; i < Dimension; i++) {
for (int j = 0; j < Dimension; j++) {
if (i != j) {
arr2[j] += arr3[i] / (Dimension - 1);
}
}
}
for (int i = 0; i < Dimension; i++) {
*(a + i) = arr1[i];
*(b + i) = arr2[i];
}
free(arr1);
free(arr2);
free(arr3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment