From ebe1e20dab26119959b7986527bb491beb255f8c Mon Sep 17 00:00:00 2001 From: Jakob Krebs Date: Wed, 12 Aug 2020 13:22:20 +0200 Subject: [PATCH] add ugly rule generator --- generate_testset.py | 81 +++++++++++++++++++++++++++++++++++++++++++++ rules.pl | 31 +++++------------ 2 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 generate_testset.py diff --git a/generate_testset.py b/generate_testset.py new file mode 100644 index 0000000..41af1e3 --- /dev/null +++ b/generate_testset.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import random +import math +import sys +import subprocess +import json +from pprint import pprint as pprint + +def generate_random_set(f, s, g): + for i in range(1,s): + f.write(f"schedule('sched{i}').\n") + + for j in range(1,g): + r = math.floor(random.gauss(4, 2)) + f.write(f"group('group{j}', {r}).\n") + + for group in range(1,g): + for sched in range(1,s): + rank = random.randrange(s) + if rank == 0: + break + f.write(f"rank('group{group}', 'sched{sched}', {rank}).\n") + +def parse_counts(counts): + ret = {} + for count in counts: + c = count[6:-2] + g, s = c.split(',\'') + ret[g] = s + return ret + + +def parse_matchings(matchings): + ret = {} + for match in matchings: + c = match[10:-2] + g, s = c.split('\',\'') + ret[g] = s + return ret + + + + +schedules = int(sys.argv[1]) +groups = int(sys.argv[2]) +max = sys.argv[3] +mode = sys.argv[4] +f = open('.rules.pl', 'w') +if mode == 'random': + generate_random_set(f, schedules, groups) + +base = open('rules.pl', 'r').read() +f.write(base.replace("{max}", max)) +f.close() +# create subprocess definition +process = ['clingo', ".rules.pl", '--outf=2', '-n 3', '-t 3', '--configuration=tweety'] # output json, n number of schedules, t threads + +# run subprocess, not sure, if busy-waiting.. +completed_process = subprocess.run(process, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + +output = completed_process.stdout + +open('solver_output_simple.json', 'w').write(output) + +results = json.loads(output) + +matchings = [] +counts = [] + +for r in results["Call"]: + w = r["Witnesses"][-1] + for m in w["Value"]: + if 'count' in m: + counts.append(m) + if 'matching' in m: + matchings.append(m) + +matchings = parse_matchings(matchings) +counts = parse_counts(counts) +pprint(counts) diff --git a/rules.pl b/rules.pl index b7e0c00..cae29ed 100644 --- a/rules.pl +++ b/rules.pl @@ -1,30 +1,15 @@ -schedule("BA1"). -schedule("BA2"). - -group("G1", 5). -group("G2", 3). -group("G3", 2). -group("G4", 1). - -rank("G1", "BA1", 2). -rank("G2", "BA1", 3). -rank("G3", "BA1", 1). -rank("G4", "BA1", 1). - -rank("G1", "BA2", 3). -rank("G2", "BA2", 2). -rank("G3", "BA2", 4). -rank("G4", "BA2", 4). - 1 = {matching(G,S) : schedule(S)} :- group(G, _). -count(Sum,S) :- Sum = #sum{ A,G : group(G,A), matching(G,S)}, schedule(S). -:- count(Sum, _), Sum > 6. +count(Sum,S) :- Sum = #sum{ A : group(G,A), matching(G,S)}, schedule(S). +:- count(Sum, _), Sum > {max}. -penalty(P) :- P = #sum{ R,G,S : rank(G,S,R), matching(G,S)}. +%penalty(P) :- P = #sum{ R : rank(G,S,R), matching(G,S)}. +penalty(G,S,R) :- matching(G,S), rank(G,S,R). +sum_pen(S) :- S = #sum{ R : penalty(_,_,R)}. -#minimize{ S : penalty(S)}. +#minimize{ S : sum_pen(S)}. #show matching/2. #show count/2. -#show penalty/1. \ No newline at end of file +%#show penalty/3. +%#show sum_pen/1.