-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of git@github.com:bearycool11/pmll.git
- Loading branch information
Showing
9 changed files
with
626 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
// Function to generate a random 3-SAT instance | ||
void generate_3sat_instance(int num_variables, int num_clauses, const char* filename) { | ||
FILE *file = fopen(filename, "w"); | ||
if (!file) { | ||
perror("Failed to open file for writing"); | ||
return; | ||
} | ||
|
||
// Write the header for the 3-SAT CNF instance | ||
fprintf(file, "p cnf %d %d\n", num_variables, num_clauses); | ||
|
||
// Randomly generate clauses | ||
srand(time(0)); // Seed the random number generator | ||
|
||
for (int i = 0; i < num_clauses; ++i) { | ||
// Each clause consists of 3 literals, choose them randomly | ||
for (int j = 0; j < 3; ++j) { | ||
int var = rand() % num_variables + 1; // Variable between 1 and num_variables | ||
if (rand() % 2 == 0) { | ||
var = -var; // Randomly negate the variable | ||
} | ||
fprintf(file, "%d ", var); | ||
} | ||
fprintf(file, "0\n"); // End of clause | ||
} | ||
|
||
fclose(file); | ||
printf("Generated random 3-SAT instance: %s\n", filename); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
// Function to run the external MiniSat solver with performance logging | ||
void run_miniSat(const char* filename, FILE *log_file) { | ||
// Start measuring the execution time of MiniSat | ||
clock_t start_time = clock(); | ||
|
||
char command[256]; | ||
sprintf(command, "minisat %s result.txt", filename); // Assuming 'minisat' is in the system path | ||
int result = system(command); | ||
|
||
// End measuring the execution time of MiniSat | ||
clock_t end_time = clock(); | ||
double miniSat_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; | ||
|
||
// Read the result from the result.txt file generated by MiniSat | ||
FILE *result_file = fopen("result.txt", "r"); | ||
if (!result_file) { | ||
perror("Failed to open result file"); | ||
return; | ||
} | ||
|
||
char result_str[20]; | ||
fgets(result_str, sizeof(result_str), result_file); | ||
if (strstr(result_str, "SATISFIABLE") != NULL) { | ||
fprintf(log_file, "MiniSat: Satisfiable (Time: %.4f seconds)\n", miniSat_time); | ||
printf("MiniSat: Satisfiable\n"); | ||
} else { | ||
fprintf(log_file, "MiniSat: Unsatisfiable (Time: %.4f seconds)\n", miniSat_time); | ||
printf("MiniSat: Unsatisfiable\n"); | ||
} | ||
|
||
fclose(result_file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <time.h> | ||
|
||
// Function to evaluate a CNF formula with a given variable assignment | ||
bool evaluate_formula(int **formula, int *assignment, int num_clauses, int num_variables) { | ||
for (int i = 0; i < num_clauses; i++) { | ||
bool clause_satisfied = false; | ||
for (int j = 0; j < 3; j++) { | ||
int var = formula[i][j]; | ||
if (var > 0 && assignment[var - 1] == 1) { | ||
clause_satisfied = true; | ||
break; | ||
} | ||
if (var < 0 && assignment[-var - 1] == 0) { | ||
clause_satisfied = true; | ||
break; | ||
} | ||
} | ||
if (!clause_satisfied) { | ||
return false; // If one clause is not satisfied, formula is unsatisfied | ||
} | ||
} | ||
return true; // If all clauses are satisfied | ||
} | ||
|
||
// Backtracking solver using the PMLL algorithm with performance tracking | ||
bool PMLL_SAT_Solver(int **formula, int num_clauses, int num_variables, FILE *log_file) { | ||
int *assignment = (int *)calloc(num_variables, sizeof(int)); // All variables are initially unassigned (0) | ||
int iterations = 0; | ||
int backtracks = 0; | ||
clock_t start_time = clock(); | ||
|
||
// Recursive backtracking | ||
for (int i = 0; i < num_variables; ++i) { | ||
assignment[i] = 1; // Try true (1) | ||
iterations++; | ||
if (evaluate_formula(formula, assignment, num_clauses, num_variables)) { | ||
// Log data after finding a solution | ||
clock_t end_time = clock(); | ||
fprintf(log_file, "PMLL Algorithm: Satisfiable (Time: %.4f seconds, Iterations: %d, Backtracks: %d)\n", | ||
(double)(end_time - start_time) / CLOCKS_PER_SEC, iterations, backtracks); | ||
free(assignment); | ||
return true; | ||
} | ||
assignment[i] = 0; // Try false (0) | ||
iterations++; | ||
if (evaluate_formula(formula, assignment, num_clauses, num_variables)) { | ||
// Log data after finding a solution | ||
clock_t end_time = clock(); | ||
fprintf(log_file, "PMLL Algorithm: Satisfiable (Time: %.4f seconds, Iterations: %d, Backtracks: %d)\n", | ||
(double)(end_time - start_time) / CLOCKS_PER_SEC, iterations, backtracks); | ||
free(assignment); | ||
return true; | ||
} | ||
backtracks++; // Count backtracks | ||
} | ||
|
||
// If no assignment satisfies the formula, return unsatisfiable | ||
clock_t end_time = clock(); | ||
fprintf(log_file, "PMLL Algorithm: Unsatisfiable (Time: %.4f seconds, Iterations: %d, Backtracks: %d)\n", | ||
(double)(end_time - start_time) / CLOCKS_PER_SEC, iterations, backtracks); | ||
free(assignment); | ||
return false; | ||
} |
Oops, something went wrong.