This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hill-climbing.c
57 lines (48 loc) · 1.63 KB
/
hill-climbing.c
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
//
// Created by Mael Kerichard on 02/12/2023.
//
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "constants.c"
// Function to calculate the sum of the current subset
unsigned long calculateSum(const SSP *instance) {
unsigned long sum = 0;
for (int i = 0; i < instance->n; i++) {
if (instance->solution[i] == 1) {
sum += instance->set[i];
}
}
return sum;
}
// Hill Climbing function to find a subset sum
HillClimbingResult hillClimbing(const SSP *instance) {
clock_t end;
const clock_t start = clock(); // Start timing
HillClimbingResult result;
int i;
for (int restart = 0; restart < RESTART; restart++) {
// Randomly initializing solution
for (i = 0; i < instance->n; i++) {
instance->solution[i] = rand() % 2;
}
for (i = 0; i < MAX_ITERATION; i++) {
const unsigned long currentSum = calculateSum(instance);
// Check if we have found a solution
if (currentSum == instance->target) {
// printf("Solution found in restart %d, iteration %d\n", restart, i);
end = clock();
result.execTime = ((double) (end - start)) / CLOCKS_PER_SEC;
result.solutionFound = 1;
return result;
}
// Randomly tweak a bit in the solution
int randIndex = rand() % instance->n;
instance->solution[randIndex] = !instance->solution[randIndex];
}
}
end = clock();
result.execTime = ((double) (end - start)) / CLOCKS_PER_SEC;
result.solutionFound = 0;
return result;
}