From 356378dc8e441f59b128f085b7be7a4b24b58ec9 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Wed, 31 Jul 2024 12:58:48 +0200 Subject: [PATCH 01/13] Created singleshot files --- singleshot_bench/ooasp.lp | 31 +++++ singleshot_bench/ooasp_aux_config.lp | 80 +++++++++++++ singleshot_bench/ooasp_aux_kb.lp | 56 +++++++++ singleshot_bench/ooasp_cautious.opt.lp | 23 ++++ singleshot_bench/ooasp_check.lp | 159 +++++++++++++++++++++++++ singleshot_bench/ooasp_check_user.lp | 0 singleshot_bench/ooasp_guess.lp | 49 ++++++++ singleshot_bench/ooasp_symmetry.lp | 20 ++++ singleshot_bench/racks/constraints.lp | 27 +++++ singleshot_bench/racks/fb.lp | 75 ++++++++++++ singleshot_bench/singleshot.lp | 8 ++ 11 files changed, 528 insertions(+) create mode 100644 singleshot_bench/ooasp.lp create mode 100644 singleshot_bench/ooasp_aux_config.lp create mode 100644 singleshot_bench/ooasp_aux_kb.lp create mode 100644 singleshot_bench/ooasp_cautious.opt.lp create mode 100644 singleshot_bench/ooasp_check.lp create mode 100644 singleshot_bench/ooasp_check_user.lp create mode 100644 singleshot_bench/ooasp_guess.lp create mode 100644 singleshot_bench/ooasp_symmetry.lp create mode 100644 singleshot_bench/racks/constraints.lp create mode 100644 singleshot_bench/racks/fb.lp create mode 100644 singleshot_bench/singleshot.lp diff --git a/singleshot_bench/ooasp.lp b/singleshot_bench/ooasp.lp new file mode 100644 index 00000000..b00ab49f --- /dev/null +++ b/singleshot_bench/ooasp.lp @@ -0,0 +1,31 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +% External Substitutes +guess. +check_potential_cv. +check_permanent_cv. +% ------------ Base programs +#const config_name = default_config_name. +#const kb_name = default_kb_name. +ooasp_configuration(kb_name,config_name). +#include "ooasp_aux_kb.lp". % Auxiliary predicates for the KB +%#external guess. % The guessing part is active +%#external check_potential_cv. % Checks for potential constraints apply +%#external check_permanent_cv. % Checks for permanent constraints apply + + +% ------------ Incremental programs +%#include "ooasp_user_input.lp". % User input with externals +#include "ooasp_aux_config.lp". % Auxiliay predicates fot the config +#include "ooasp_guess.lp". % Guess section +%#include "ooasp_guess_fclingo.lp". % Guess section +#include "ooasp_check.lp". % Check section +%#include "ooasp_check_fclingo.lp". % Check section +%#include "ooasp_check_user.lp". % Check section +%#include "ooasp_cautious_opt.lp". % Optimizations used for cautious reasoning + +%#include "ooasp_symmetry.lp". % Symmetry breaking constraints THIS BREAKS IT...for now (in original it is not replaced by the variable) + +:- user(ooasp_isa(C,new_object)), +not ooasp_isa(C,new_object). \ No newline at end of file diff --git a/singleshot_bench/ooasp_aux_config.lp b/singleshot_bench/ooasp_aux_config.lp new file mode 100644 index 00000000..5723437d --- /dev/null +++ b/singleshot_bench/ooasp_aux_config.lp @@ -0,0 +1,80 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +%#program domain(new_object, cls). + +%#external active(OBJECT). % Active id for arity constraints +%ooasp_domain(cls,OBJECT). + +% Transitive closure when a leaf class is selected via choice +ooasp_isa(LEAFCLASS,OBJECT) :- + ooasp_isa_leaf(LEAFCLASS,OBJECT). + +% Transitive closure +ooasp_isa(SUPER,OBJECT) :- + ooasp_isa(SUB,OBJECT), + ooasp_subclass(SUB,SUPER). + +% Generation of leafs +ooasp_isa_leaf(C,OBJECT) :- + ooasp_isa(C,OBJECT), + ooasp_leafclass(C). + +ooasp_isa_smallest(SUPER,OBJECT) :- + ooasp_isa(SUPER,OBJECT), + not ooasp_isa(SUB,OBJECT) :ooasp_subclass(SUB,SUPER). + +% Auxiliary predicate to avoid duplicated rules +% ooasp_assoc_gen(ASSOC,POS,ID1,ID2) +% Object ID1 apearing in position POS of association ASSOC is associated to object ID2 +% Example: ooasp_assoc_gen(c1,ass,2,23,10): : object 23 of the second class of association ass is associated with object 10 + +ooasp_assoc_gen(ASSOC,1,ID1,OBJECT) :- + ooasp_associated(ASSOC,ID1,OBJECT). + +ooasp_assoc_gen(ASSOC,1,OBJECT,ID2) :- + ooasp_associated(ASSOC,OBJECT,ID2). + +ooasp_assoc_gen(ASSOC,2,OBJECT,ID1) :- + ooasp_associated(ASSOC,ID1,OBJECT). + +ooasp_assoc_gen(ASSOC,2,ID2,OBJECT) :- + ooasp_associated(ASSOC,OBJECT,ID2). + +% Association specialization +ooasp_associated(ASSOC,OBJECT,ID2) :- + ooasp_associated(ASSOC_S,OBJECT,ID2), + ooasp_assoc_specialization(ASSOC_S,ASSOC), + ooasp_assoc(ASSOC,C1,_,_,C2,_,_), + ooasp_isa(C1,OBJECT), + ooasp_isa(C2,ID2). + +ooasp_associated(ASSOC,ID1,OBJECT) :- + ooasp_associated(ASSOC_S,ID1,OBJECT), + ooasp_assoc_specialization(ASSOC_S,ASSOC), + ooasp_assoc(ASSOC,C1,_,_,C2,_,_), + ooasp_isa(C1,ID1), + ooasp_isa(C2,OBJECT). + +ooasp_associated(ASSOC_S,OBJECT,ID2) :- + ooasp_associated(ASSOC,OBJECT,ID2), + ooasp_assoc_specialization(ASSOC_S,ASSOC), + ooasp_assoc(ASSOC_S,C1,_,_,C2,_,_), + ooasp_isa(C1,OBJECT), + ooasp_isa(C2,ID2). + +ooasp_associated(ASSOC_S,ID1,OBJECT) :- + ooasp_associated(ASSOC,ID1,OBJECT), + ooasp_assoc_specialization(ASSOC_S,ASSOC), + ooasp_assoc(ASSOC_S,C1,_,_,C2,_,_), + ooasp_isa(C1,ID1), + ooasp_isa(C2,OBJECT). + + +% Int attributes + +ooasp_attr_int(OBJECT, A, MIN, MAX):- + ooasp_isa(C,OBJECT), + ooasp_attr_minInclusive(C,A,MIN), + ooasp_attr_maxInclusive(C,A,MAX), + ooasp_attr(C,A,int). diff --git a/singleshot_bench/ooasp_aux_kb.lp b/singleshot_bench/ooasp_aux_kb.lp new file mode 100644 index 00000000..0485782b --- /dev/null +++ b/singleshot_bench/ooasp_aux_kb.lp @@ -0,0 +1,56 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +#program base. + +#const use_dl = false. + +% transitive closure +ooasp_subclass(SUB,SUPER2) :- + ooasp_subclass(SUB,SUPER1), + ooasp_subclass(SUPER1,SUPER2). + +ooasp_subclass_ref(C1,C2):-ooasp_subclass(C1,C2). +ooasp_subclass_ref(C,C):-ooasp_class(C). + + +% derive domain for boolean attributes +ooasp_attr_enum(C,N,"true") :- + ooasp_attr(C,N,"boolean"). +ooasp_attr_enum(C,N,"false") :- + ooasp_attr(C,N,"boolean"). + +% true, if attribute has domain +ooasp_attr_hasdomain(C,N) :- + ooasp_attr_enum(C,N,D). + +ooasp_attr_enum(C,N,MIN..MAX) :- + ooasp_attr(C,N,enumint), + ooasp_attr_minInclusive(C,N,MIN), + ooasp_attr_maxInclusive(C,N,MAX). + +ooasp_attr_fdom(C,N,MIN..MAX) :- + ooasp_attr(C,N,int), + ooasp_attr_minInclusive(C,N,MIN), + ooasp_attr_maxInclusive(C,N,MAX). + +% leafclasses are classes without subclasses +ooasp_leafclass(C) :- + ooasp_class(C), + not ooasp_subclass(_,C). + + +% ooasp_assoc_limit(ASSOC,min,1,C1,C2MIN,C2) +% The association ASSOC must be from a C1 object to at least C2MIN objects of class C2 + +ooasp_assoc_limit(ASSOC,min,1,C1,C2MIN,C2):- + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX). + +ooasp_assoc_limit(ASSOC,max,1,C1,C2MAX,C2):- + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX). + +ooasp_assoc_limit(ASSOC,min,2,C2,C1MIN,C1):- + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX). + +ooasp_assoc_limit(ASSOC,max,2,C2,C1MAX,C1):- + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX). diff --git a/singleshot_bench/ooasp_cautious.opt.lp b/singleshot_bench/ooasp_cautious.opt.lp new file mode 100644 index 00000000..3df59736 --- /dev/null +++ b/singleshot_bench/ooasp_cautious.opt.lp @@ -0,0 +1,23 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + + +%#program domain(OBJECT, cls). + +% Used when getting inferences to minimize on lowerbound errors +:~ ooasp_cv(lowerbound,ID,_,(ASSOC,CMIN,N,C,OPT,OBJECT)). [CMIN-N@3,(ASSOC,ID)] + +% % Penalize the bigger associations + +% :~ ooasp_isa_leaf(C1,ID3), % Instanciates ID3 +% ooasp_isa_leaf(C2,ID1), +% not user(ooasp_associated(A, ID3, ID1)), +% not ooasp_assoc_specialization(A, _), % Only count the super +% ooasp_associated(A, ID3, ID1). [ID1@2,(A,ID3)] + + +% :~ ooasp_isa_leaf(C1,ID3), % Instanciates ID3 +% ooasp_isa_leaf(C2,ID1), +% not user(ooasp_associated(A, ID1, ID3)), +% not ooasp_assoc_specialization(A, _), % Only count the super +% ooasp_associated(A, ID1, ID3). [ID1@1,(A,ID3)] diff --git a/singleshot_bench/ooasp_check.lp b/singleshot_bench/ooasp_check.lp new file mode 100644 index 00000000..8eae747e --- /dev/null +++ b/singleshot_bench/ooasp_check.lp @@ -0,0 +1,159 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +% ooasp_cv(NAME,OBJECT,STR,ARGS) + +%#program domain(OBJECT, cls). + + +% Integrity constraints for constraint violations, activated based on the externals +:- ooasp_cv(CV,_,_,_), + not ooasp_potential_cv(CV), + check_permanent_cv. +:- ooasp_cv(CV,_,_,_), + ooasp_potential_cv(CV), + check_potential_cv. + +%======================================= +% Potential constraint violations +%======================================= + +ooasp_potential_cv(lowerbound). +ooasp_potential_cv(no_value). + +%======================================= +% Instances +%======================================= + +% ------ (USER INPUT) Cant be instance of two different leaf classes +ooasp_cv(notinstanceofsubclass,OBJECT,"Has two leaf classes {} and {}",(C1,C2)) :- + ooasp_isa_leaf(C1,OBJECT), + ooasp_isa_leaf(C2,OBJECT), + C1 CMAX. + %active(OBJECT). + + % SAFETY VIOLATIONS +lb_violation(lowerbound,ID1,"Lowerbound for {} is {} but has {} {}",(ASSOC,CMIN,N,C2,OPT)):- + ooasp_assoc_limit(ASSOC,min,OPT,C,CMIN,C2), + ooasp_isa(C,ID1), + #count { ID2:ooasp_assoc_gen(ASSOC,OPT,ID1,ID2) } = N, + N < CMIN. + %active(OBJECT). + +ooasp_cv(lowerbound,ID1,M,(ASSOC,CMIN,N,C,OPT)):- + lb_violation(lowerbound,ID1,M,(ASSOC,CMIN,N,C,OPT)), + not lb_violation(lowerbound,ID1,_,(SUBASSOC,_,_,_,_)):ooasp_assoc_specialization(SUBASSOC,ASSOC). + + +%--- Associations are only allowed to something instantiated + +ooasp_cv(no_instance_for_association,OBJECT,"Associated by {} to {} but not is not instantiated",(ASSOC,ID2)) :- + ooasp_associated(ASSOC,OBJECT,ID2), + not ooasp_isa(_,OBJECT). + +ooasp_cv(no_instance_for_association,OBJECT,"Associated by {} to {} but not instantiated",(ASSOC,ID1)) :- + ooasp_associated(ASSOC,ID1,OBJECT), + not ooasp_isa(_,OBJECT). + +ooasp_cv(no_instance_for_association,OBJECT,"Associated by {} to {} which is not instantiated",(ASSOC,ID2)) :- + ooasp_associated(ASSOC,OBJECT,ID2), + not ooasp_isa(_,ID2). + +ooasp_cv(no_instance_for_association,OBJECT,"Associated by {} to {} which is not instantiated",(ASSOC,ID1)) :- + ooasp_associated(ASSOC,ID1,OBJECT), + not ooasp_isa(_,ID1). + + +%--- (USER INPUT) Type of association + +ooasp_cv(wrongtypeinassoc,ID,"Associated by {} but is not of class {}",(ASSOC,C1)) :- + %active(OBJECT), + ooasp_associated(ASSOC,ID,_), + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX), + not ooasp_isa(C1,ID). + +ooasp_cv(wrongtypeinassoc,ID,"Associated by {} but is not of class {}",(ASSOC,C2)) :- + %active(OBJECT), + ooasp_associated(ASSOC,_,ID), + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX), + not ooasp_isa(C2,ID). + + +%======================================= +% Values +%======================================= + + +%--- Missing attribute value +ooasp_cv(no_value,OBJECT,"Missing value for {}",(A,)) :- + ooasp_attr(C,A,enumint), + ooasp_attr_enum(C,A,_), + ooasp_isa(C,OBJECT), + not ooasp_attr_value(A,OBJECT,_). + + +%======================================= +% Special Constraint violations +%======================================= + + +%--- Unique attributes (Enumeration) + +% The class C of the main object is in position 1 + +% The new object is of that class C +ooasp_cv(unique_attr, (OBJECT;ID1), "Attribute {} must be unique among all {}s associated by {} to {}",(ATTR,C,ASSOC,ID2,OBJECT )):- + ooasp_special_cv(unique,(ASSOC, C, ATTR)), + ooasp_assoc(ASSOC,C,_,_,_,_,_), + ooasp_associated(ASSOC,OBJECT,ID2), + ooasp_associated(ASSOC,ID1,ID2), + OBJECT>ID1, + ooasp_attr_value(ATTR,OBJECT,V), + ooasp_attr_value(ATTR,ID1,V). + +% The new object is of the other class +ooasp_cv(unique_attr, (ID3;ID1), "Attribute {} must be unique among all {}s associated by {} to {}",(ATTR,C,ASSOC,OBJECT)):- + ooasp_special_cv(unique,(ASSOC, C, ATTR)), + ooasp_assoc(ASSOC,C,_,_,_,_,_), + ooasp_associated(ASSOC,ID3,OBJECT), + ooasp_associated(ASSOC,ID1,OBJECT), + ID3>ID1, + ooasp_attr_value(ATTR,ID3,V), + ooasp_attr_value(ATTR,ID1,V). + +% The class C of the main object is in position 2 + +ooasp_cv(unique_attr, (OBJECT;ID1), "Attribute {} must be unique among all {}s associated by {} to {}",(ATTR,C,ASSOC,ID2,OBJECT )):- + ooasp_special_cv(unique,(ASSOC, C, ATTR)), + ooasp_assoc(ASSOC,_,_,_,C,_,_), + ooasp_associated(ASSOC,ID2,OBJECT), + ooasp_associated(ASSOC,ID2,ID1), + OBJECT>ID1, + ooasp_attr_value(ATTR,OBJECT,V), + ooasp_attr_value(ATTR,ID1,V). + +ooasp_cv(unique_attr, (ID3;ID1), "Attribute {} must be unique among all {}s associated by {} to {}",(ATTR,C,ASSOC,OBJECT )):- + ooasp_special_cv(unique,(ASSOC, C, ATTR)), + ooasp_assoc(ASSOC,_,_,_,C,_,_), + ooasp_associated(ASSOC,OBJECT,ID3), + ooasp_associated(ASSOC,OBJECT,ID1), + ID3>ID1, + ooasp_attr_value(ATTR,ID3,V), + ooasp_attr_value(ATTR,ID1,V). + + + diff --git a/singleshot_bench/ooasp_check_user.lp b/singleshot_bench/ooasp_check_user.lp new file mode 100644 index 00000000..e69de29b diff --git a/singleshot_bench/ooasp_guess.lp b/singleshot_bench/ooasp_guess.lp new file mode 100644 index 00000000..3bdccb8a --- /dev/null +++ b/singleshot_bench/ooasp_guess.lp @@ -0,0 +1,49 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + + +%#program domain(OBJECT, cls). + +% If an object has a domain, then it could be instanciated +{ ooasp_isa_leaf(LEAFCLASS,OBJECT): + ooasp_leafclass(LEAFCLASS), + ooasp_subclass_ref(LEAFCLASS,C) } 1:- + ooasp_domain(C,OBJECT), + guess. + +% If is_a appears then it has to be instanciated as leaf +1{ ooasp_isa_leaf(LEAFCLASS,OBJECT): + ooasp_leafclass(LEAFCLASS), + ooasp_subclass_ref(LEAFCLASS,C) } 1:- + ooasp_isa(C,OBJECT), + guess. + +% % Associations from OBJECT +{ ooasp_associated(ASSOC,OBJECT,ID2) } :- + ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX), + ooasp_isa(C1,OBJECT), + ID24. + +ooasp_cv(frame_must_be_within_range, F, "Frame position is outside of range {} ",(F,new_object)):- + ooasp_isa(rackSingle,new_object), + ooasp_associated(rack_frames,new_object,F), + ooasp_attr_value(frame_position,F,P), + P>4. + +% % moduleII requires moduleV +ooasp_cv(moduleII_requires_moduleV,F,"ModuleII requires moduleV in frame {}",(F,new_object)):- + active(new_object), + ooasp_isa(frame,F), + ooasp_isa(moduleII,M), + ooasp_associated(frame_modules,F,M), + not ooasp_isa(moduleV,M5): ooasp_associated(frame_modules,F,M5). \ No newline at end of file diff --git a/singleshot_bench/racks/fb.lp b/singleshot_bench/racks/fb.lp new file mode 100644 index 00000000..d3ee2380 --- /dev/null +++ b/singleshot_bench/racks/fb.lp @@ -0,0 +1,75 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +% EXAMPLE racks +% classes +ooasp_class(object). + +ooasp_class(rack). +ooasp_subclass(rack,object). + +ooasp_class(rackSingle). +ooasp_subclass(rackSingle,rack). + +ooasp_class(rackDouble). +ooasp_subclass(rackDouble,rack). + +ooasp_class(frame). +ooasp_subclass(frame,object). + +ooasp_class(element). +ooasp_class(elementA). +ooasp_class(elementB). +ooasp_class(elementC). +ooasp_class(elementD). +ooasp_subclass(element,object). +ooasp_subclass(elementA,element). +ooasp_subclass(elementB,element). +ooasp_subclass(elementC,element). +ooasp_subclass(elementD,element). + +ooasp_class(module). +ooasp_class(moduleI). +ooasp_class(moduleII). +ooasp_class(moduleIII). +ooasp_class(moduleIV). +ooasp_class(moduleV). +ooasp_subclass(module,object). +ooasp_subclass(moduleI,module). +ooasp_subclass(moduleII,module). +ooasp_subclass(moduleIII,module). +ooasp_subclass(moduleIV,module). +ooasp_subclass(moduleV,module). + +% associations +% -1 means * i.e. unrestricted + +ooasp_assoc(rack_frames,rack,1,1,frame,4,8). +ooasp_assoc(frame_modules,frame,1,1,module,0,5). +ooasp_assoc(element_modules,element,0,1,module,1,4). + + +ooasp_assoc(rack_framesS,rackSingle,0,1,frame,4,4). +ooasp_assoc(rack_framesD,rackDouble,0,1,frame,8,8). + +ooasp_assoc(element_modules1,elementA,1,1,moduleI,1,1). +ooasp_assoc(element_modules2,elementB,1,1,moduleII,2,2). +ooasp_assoc(element_modules3,elementC,1,1,moduleIII,3,3). +ooasp_assoc(element_modules4,elementD,1,1,moduleIV,4,4). + +ooasp_assoc_specialization(rack_framesS,rack_frames). +ooasp_assoc_specialization(rack_framesD,rack_frames). +ooasp_assoc_specialization(element_modules1,element_modules). +ooasp_assoc_specialization(element_modules2,element_modules). +ooasp_assoc_specialization(element_modules3,element_modules). +ooasp_assoc_specialization(element_modules4,element_modules). + + +ooasp_attr(frame,frame_position,enumint). +ooasp_attr_minInclusive(frame,frame_position,1). +ooasp_attr_maxInclusive(frame,frame_position,8). + + +ooasp_special_cv(unique, (rack_frames, frame, frame_position)). + +% ooasp_special_cv(requires, (frame_modules, moduleII, moduleV)). diff --git a/singleshot_bench/singleshot.lp b/singleshot_bench/singleshot.lp new file mode 100644 index 00000000..8278dc7b --- /dev/null +++ b/singleshot_bench/singleshot.lp @@ -0,0 +1,8 @@ +#include "ooasp.lp". + +#include "instances/assumptions.lp". + +#include "racks/constraints.lp". +#include "racks/fb.lp". + + From 2a9e3f09d43fe6ec7124cce3a2bc7fccb81e1c90 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Wed, 31 Jul 2024 12:59:11 +0200 Subject: [PATCH 02/13] Created singleshot benchmarking script --- singleshot_bench/autorun.py | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 singleshot_bench/autorun.py diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py new file mode 100644 index 00000000..a3cbd45a --- /dev/null +++ b/singleshot_bench/autorun.py @@ -0,0 +1,127 @@ +import os +import datetime + +from typing import List + +from clingo import Control + +ELEMENT_TYPES = 4 +ELEMENT_NAMES = 'ABCD' + +domain_sizes = [19, 33] # known domain sizes +o_instances = [x+1 for x in range(2)] + + +def generate_ids(n) -> List: + """ + Generates combinations of element definitions + with IDs such that they would cause no issue (THIS DOCSTRING NEEDS TO BE CHANGED) + """ + + # Note: this can easily be done just as ranges for the actual elements, but I think this is much clearer + + ids = [id+1 for id in range(ELEMENT_TYPES*n)] + assigned = [ids[i*n:i*n+n] for i in range(ELEMENT_TYPES)] + terms = [] + + for letter, category in enumerate(assigned): + for id in category: + terms.append(f"ooasp_isa(element{ELEMENT_NAMES[letter]},{id}).") + + return terms + + +def rewrite_assumptions(content: List[str], save: bool = True) -> None: + """ + Rewrites the assumptions.lp file to the list of content passed. + If save is set to true, creates a legacy directory and saves a copy of existing assumptions to it. + """ + + if os.path.isfile('instances/assumptions.lp'): + if save: + os.makedirs('instances/outdated', exist_ok=True) + new_file = f"instances/outdated/assumptions{len(content)}{str(datetime.datetime.now()).replace(' ', '_').replace('.', '-').replace(':', '-')}.lp" + os.rename('instances/assumptions.lp', new_file) + with open('instances/assumptions.lp', 'w+') as file: + print(" >>Rewriting assumptions.") + for t in content: + file.write(t+'\n') + + +def add_domain(n) -> None: + """ + Adds domain size constraints to the assumption file + """ + with open('instances/assumptions.lp', 'a') as file: + t = f'ooasp_domain(object,1..{n}).' + file.write(t) + + +def build_assumptions(n, save: bool = False) -> None: + """ + Builds the new assumptions file in its entirety. + """ + ts = generate_ids(n) + rewrite_assumptions(ts, save=save) + add_domain(domain_sizes[n-1] if len(domain_sizes) >= n else 100) + + +def reset_solving() -> Control: + """ + Reinitialises the clingo control. + Returns the set up instance. + """ + ctl = Control(["--opt-mode=ignore", + "--warn=none"]) + ctl.load("singleshot.lp") + ctl.ground([("base", [])]) + return ctl + + +def log_model(model, out=False): + """ + Creates a model directory (if it does not exist) + And logs resulting models into files + """ + os.makedirs('results/models', exist_ok=True) + + global timestr + new_file = f"results/models/M{iteration}{timestr}.txt" + with open(new_file, 'w') as mfile: + mfile.write(str(model)) + if out: + print('MODEL:') + print(model) + + +def log_results(stats,iteration, out=False): + """ + Creates a results directory (if it does not exist) + And logs times and results into text files. + """ + os.makedirs('results/times', exist_ok=True) + global timestr + new_file = f"results/times/R{iteration}{timestr}.txt" + with open(new_file, 'w') as mfile: + mfile.write(stats) + if out: + print('RESULTS:') + print(stats) + +def on_model(m): + """ + Helper function to control resulting model + """ + log_model(m) + +if __name__ == "__main__": + global model + model = None + global timestr + timestr = str(datetime.datetime.now()).replace(' ', '_').replace('.', '-').replace(':', '-') + for iteration in o_instances: + print(f">>Solving for:{iteration}") + build_assumptions(iteration) + ctl = reset_solving() + ctl.solve(on_model=on_model) + log_results(str(ctl.statistics['summary']['times']),iteration, out=True) From 52b8543318ea12acd759632d065820a727342645 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Wed, 31 Jul 2024 13:02:31 +0200 Subject: [PATCH 03/13] Fixed missing instances directory bug --- singleshot_bench/autorun.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index a3cbd45a..0cdfa5c1 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -42,7 +42,8 @@ def rewrite_assumptions(content: List[str], save: bool = True) -> None: os.makedirs('instances/outdated', exist_ok=True) new_file = f"instances/outdated/assumptions{len(content)}{str(datetime.datetime.now()).replace(' ', '_').replace('.', '-').replace(':', '-')}.lp" os.rename('instances/assumptions.lp', new_file) - with open('instances/assumptions.lp', 'w+') as file: + os.makedirs('instances', exist_ok=True) + with open('instances/assumptions.lp', 'w') as file: print(" >>Rewriting assumptions.") for t in content: file.write(t+'\n') From eccc7fd3b9f82bd5dd85f9a9811fb6b197390eed Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Wed, 31 Jul 2024 13:07:17 +0200 Subject: [PATCH 04/13] Added type hints --- singleshot_bench/autorun.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index 0cdfa5c1..e08b09c9 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -12,7 +12,7 @@ o_instances = [x+1 for x in range(2)] -def generate_ids(n) -> List: +def generate_ids(n: int) -> List: """ Generates combinations of element definitions with IDs such that they would cause no issue (THIS DOCSTRING NEEDS TO BE CHANGED) @@ -49,7 +49,7 @@ def rewrite_assumptions(content: List[str], save: bool = True) -> None: file.write(t+'\n') -def add_domain(n) -> None: +def add_domain(n: int) -> None: """ Adds domain size constraints to the assumption file """ @@ -58,7 +58,7 @@ def add_domain(n) -> None: file.write(t) -def build_assumptions(n, save: bool = False) -> None: +def build_assumptions(n: int, save: bool = False) -> None: """ Builds the new assumptions file in its entirety. """ @@ -95,7 +95,7 @@ def log_model(model, out=False): print(model) -def log_results(stats,iteration, out=False): +def log_results(stats, iteration, out=False): """ Creates a results directory (if it does not exist) And logs times and results into text files. @@ -108,13 +108,15 @@ def log_results(stats,iteration, out=False): if out: print('RESULTS:') print(stats) - + + def on_model(m): """ Helper function to control resulting model """ log_model(m) + if __name__ == "__main__": global model model = None @@ -125,4 +127,4 @@ def on_model(m): build_assumptions(iteration) ctl = reset_solving() ctl.solve(on_model=on_model) - log_results(str(ctl.statistics['summary']['times']),iteration, out=True) + log_results(str(ctl.statistics['summary']['times']), iteration, out=True) From 364256da150cc6e832805f041f0f5c8d5a0511be Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Wed, 31 Jul 2024 15:03:08 +0200 Subject: [PATCH 05/13] Added rough domain size estimations --- singleshot_bench/autorun.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index e08b09c9..824667b8 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -8,8 +8,9 @@ ELEMENT_TYPES = 4 ELEMENT_NAMES = 'ABCD' -domain_sizes = [19, 33] # known domain sizes -o_instances = [x+1 for x in range(2)] +# for now just minimal 1elem config * number of elements +domain_sizes = [19*(x+1) for x in range(20)] # known minimmum domain sizes : [19,33] +o_instances = [x+1 for x in range(20)] def generate_ids(n: int) -> List: @@ -95,7 +96,7 @@ def log_model(model, out=False): print(model) -def log_results(stats, iteration, out=False): +def log_results(stats: str, iteration: int, out: bool=False): """ Creates a results directory (if it does not exist) And logs times and results into text files. @@ -108,15 +109,13 @@ def log_results(stats, iteration, out=False): if out: print('RESULTS:') print(stats) - - + def on_model(m): """ Helper function to control resulting model """ log_model(m) - if __name__ == "__main__": global model model = None @@ -127,4 +126,4 @@ def on_model(m): build_assumptions(iteration) ctl = reset_solving() ctl.solve(on_model=on_model) - log_results(str(ctl.statistics['summary']['times']), iteration, out=True) + log_results(str(ctl.statistics['summary']['times']),iteration, out=True) From f821a3d95e0967947dc0655a8980eb4fe5fb70f5 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Mon, 5 Aug 2024 09:58:05 +0200 Subject: [PATCH 06/13] Added README.md with usage instructions to subdirectory --- singleshot_bench/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 singleshot_bench/README.md diff --git a/singleshot_bench/README.md b/singleshot_bench/README.md new file mode 100644 index 00000000..66a13fd2 --- /dev/null +++ b/singleshot_bench/README.md @@ -0,0 +1,17 @@ +# Singleshot benchmarking for OOASP + +In this directory is collection of `.lp` files defining ooasp for purposes of singleshot benchmarking, automated python script for running them and resulting outputs of the script. + +## Automated Script + +The benchmarking is meant to be run using automated script `autorun.py`, which is to be run from the `singleshot_bench` directory. +The script works by automatically rewriting the `assumptions.lp` file and then using the Clingo python API to run solving and log times. + +### Domain sizes + +As of now, the domain sizes (which need to be specified for singleshot) are only roughly approximated by formula: `n*19`, as the smallest configuration given one element of each type is 19. *Better approximation is to be further developed*. + +### Logging + +The script currently creates logs of found models in `results/models` and times per iteration in `results/times`. +Outdated `assumptions.lp` files are not logged by default, but this logging can be switch on by changing the `save` flag in `build_assumptions` function to `True`. \ No newline at end of file From 33cab90a13a9f80e81888140c8c446cdf708077b Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Thu, 8 Aug 2024 11:27:56 +0200 Subject: [PATCH 07/13] Changed domain_sizes --- singleshot_bench/autorun.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index 824667b8..ae327bed 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -8,19 +8,16 @@ ELEMENT_TYPES = 4 ELEMENT_NAMES = 'ABCD' -# for now just minimal 1elem config * number of elements -domain_sizes = [19*(x+1) for x in range(20)] # known minimmum domain sizes : [19,33] +domain_sizes = [19, 33, 51, 65, 84, 98, 116, 130, 149, 163, 181, 195, 214, 228, 246, 260, 279, 293, 311, 325] o_instances = [x+1 for x in range(20)] def generate_ids(n: int) -> List: """ Generates combinations of element definitions - with IDs such that they would cause no issue (THIS DOCSTRING NEEDS TO BE CHANGED) + with suitable IDs. """ - # Note: this can easily be done just as ranges for the actual elements, but I think this is much clearer - ids = [id+1 for id in range(ELEMENT_TYPES*n)] assigned = [ids[i*n:i*n+n] for i in range(ELEMENT_TYPES)] terms = [] From 77dd9fc73ec5636af2c3127bac944e8db3984ee7 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Thu, 8 Aug 2024 11:30:30 +0200 Subject: [PATCH 08/13] Added license header to autorun.py --- singleshot_bench/autorun.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index ae327bed..a78c7f51 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -1,3 +1,6 @@ +# Copyright (c) 2022-2024 Siemens AG Oesterreich +# SPDX-License-Identifier: MIT + import os import datetime From ee0d852b327e9262b445c0ed54b4513e5025f4e6 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Mon, 12 Aug 2024 11:57:47 +0200 Subject: [PATCH 09/13] Fixed symmetry-breaking encoding --- singleshot_bench/ooasp_check_user.lp | 45 ++++++++++++++++++++++++++++ singleshot_bench/ooasp_user_input.lp | 20 +++++++++++++ singleshot_bench/singleshot.lp | 5 ++-- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 singleshot_bench/ooasp_user_input.lp diff --git a/singleshot_bench/ooasp_check_user.lp b/singleshot_bench/ooasp_check_user.lp index e69de29b..40f451fb 100644 --- a/singleshot_bench/ooasp_check_user.lp +++ b/singleshot_bench/ooasp_check_user.lp @@ -0,0 +1,45 @@ + +% ============================================================================== +% User input Values (General), when guessing values this should not be needed +% ============================================================================== + +% %--- (USER INPUT) Attribute value must belong to instance class +ooasp_cv(no_instance_for_attribute,OBJECT,"Attribute {} not of selected class",(A,)) :- + ooasp_attr(C1,A,T), + user(ooasp_attr_value(A,OBJECT,V)), + not ooasp_isa(C1,OBJECT). + +%--- (USER INPUT) Attribute value must belong to instance +ooasp_cv(no_instance_for_attribute,OBJECT,"Value for {} assigned but instanciated",(A,)) :- + user(ooasp_attr_value(A,OBJECT,V)), + not ooasp_isa(_,OBJECT). + +%--- (USER INPUT) Atribute value not from domain +ooasp_cv(value_not_domain,OBJECT,"Value {} for {} not from domain",(V,A)) :- + ooasp_attr(C,A,enumint), + ooasp_attr_enum(C,A,_), + ooasp_isa(C,OBJECT), + user(ooasp_attr_value(A,OBJECT,V)), + not ooasp_attr_enum(C,A,V). + +%--- (USER INPUT) Multiple values for a value +ooasp_cv(conflicting_values,OBJECT,"Multiple values for {}: {},{}",(A,V1,V2)) :- + ooasp_attr(C,A,T), + user(ooasp_attr_value(A,OBJECT,V1)), + user(ooasp_attr_value(A,OBJECT,V2)), + V1{}",(A,V,MAX)) :- + ooasp_attr(C,A,T), + ooasp_isa(C,OBJECT), + user(ooasp_attr_value(A,OBJECT,V)), + ooasp_attr_maxInclusive(C,A,MAX), + V>MAX. \ No newline at end of file diff --git a/singleshot_bench/ooasp_user_input.lp b/singleshot_bench/ooasp_user_input.lp new file mode 100644 index 00000000..6e1b4865 --- /dev/null +++ b/singleshot_bench/ooasp_user_input.lp @@ -0,0 +1,20 @@ +% Copyright (c) 2022 Siemens AG Oesterreich +% SPDX-License-Identifier: MIT + +%-------- +% Externals for user selections to allow checks of partial rations +%-------- + +% ---- Classes +ooasp_isa(C,OBJECT) :- + user(ooasp_isa(C,OBJECT)). + +% ---- Associations +ooasp_associated(AS,OBJECT,ID2) :- + user(ooasp_associated(AS,OBJECT,ID2)). +ooasp_associated(AS,ID1,OBJECT) :- + user(ooasp_associated(AS,ID1,OBJECT)). + +% ---- Attribute values +ooasp_attr_value(A,OBJECT,V) :- + user(ooasp_attr_value(A,OBJECT,V)). \ No newline at end of file diff --git a/singleshot_bench/singleshot.lp b/singleshot_bench/singleshot.lp index 8278dc7b..f3f2b11e 100644 --- a/singleshot_bench/singleshot.lp +++ b/singleshot_bench/singleshot.lp @@ -1,8 +1,9 @@ #include "ooasp.lp". +#include "ooasp_symmetry.lp". +#include "ooasp_user_input.lp". +#include "ooasp_check_user.lp". #include "instances/assumptions.lp". #include "racks/constraints.lp". #include "racks/fb.lp". - - From 188893d40073f81dd6f90f75b1ccf416095863cb Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Mon, 12 Aug 2024 11:59:04 +0200 Subject: [PATCH 10/13] Updated autorun.py to user specified objects --- singleshot_bench/autorun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/singleshot_bench/autorun.py b/singleshot_bench/autorun.py index a78c7f51..c24cc815 100644 --- a/singleshot_bench/autorun.py +++ b/singleshot_bench/autorun.py @@ -27,7 +27,7 @@ def generate_ids(n: int) -> List: for letter, category in enumerate(assigned): for id in category: - terms.append(f"ooasp_isa(element{ELEMENT_NAMES[letter]},{id}).") + terms.append(f"user(ooasp_isa(element{ELEMENT_NAMES[letter]},{id})).") return terms From 62410962d136818c5ea26f2df7e1fcbc3f913019 Mon Sep 17 00:00:00 2001 From: Lucia Balazova Date: Tue, 13 Aug 2024 13:08:10 +0200 Subject: [PATCH 11/13] Moved 'singleshot_bench' to 'benchmarks/singleshot' --- {singleshot_bench => benchmarks/singleshot}/README.md | 0 {singleshot_bench => benchmarks/singleshot}/autorun.py | 8 +++++--- {singleshot_bench => benchmarks/singleshot}/ooasp.lp | 0 .../singleshot}/ooasp_aux_config.lp | 0 .../singleshot}/ooasp_aux_kb.lp | 0 .../singleshot}/ooasp_cautious.opt.lp | 0 .../singleshot}/ooasp_check.lp | 0 .../singleshot}/ooasp_check_user.lp | 0 .../singleshot}/ooasp_guess.lp | 0 .../singleshot}/ooasp_symmetry.lp | 0 .../singleshot}/ooasp_user_input.lp | 0 .../singleshot}/racks/constraints.lp | 0 {singleshot_bench => benchmarks/singleshot}/racks/fb.lp | 0 {singleshot_bench => benchmarks/singleshot}/singleshot.lp | 0 14 files changed, 5 insertions(+), 3 deletions(-) rename {singleshot_bench => benchmarks/singleshot}/README.md (100%) rename {singleshot_bench => benchmarks/singleshot}/autorun.py (96%) rename {singleshot_bench => benchmarks/singleshot}/ooasp.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_aux_config.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_aux_kb.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_cautious.opt.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_check.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_check_user.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_guess.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_symmetry.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/ooasp_user_input.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/racks/constraints.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/racks/fb.lp (100%) rename {singleshot_bench => benchmarks/singleshot}/singleshot.lp (100%) diff --git a/singleshot_bench/README.md b/benchmarks/singleshot/README.md similarity index 100% rename from singleshot_bench/README.md rename to benchmarks/singleshot/README.md diff --git a/singleshot_bench/autorun.py b/benchmarks/singleshot/autorun.py similarity index 96% rename from singleshot_bench/autorun.py rename to benchmarks/singleshot/autorun.py index c24cc815..64e662c8 100644 --- a/singleshot_bench/autorun.py +++ b/benchmarks/singleshot/autorun.py @@ -96,7 +96,7 @@ def log_model(model, out=False): print(model) -def log_results(stats: str, iteration: int, out: bool=False): +def log_results(stats: str, iteration: int, out: bool = False): """ Creates a results directory (if it does not exist) And logs times and results into text files. @@ -109,13 +109,15 @@ def log_results(stats: str, iteration: int, out: bool=False): if out: print('RESULTS:') print(stats) - + + def on_model(m): """ Helper function to control resulting model """ log_model(m) + if __name__ == "__main__": global model model = None @@ -126,4 +128,4 @@ def on_model(m): build_assumptions(iteration) ctl = reset_solving() ctl.solve(on_model=on_model) - log_results(str(ctl.statistics['summary']['times']),iteration, out=True) + log_results(str(ctl.statistics['summary']['times']), iteration, out=True) diff --git a/singleshot_bench/ooasp.lp b/benchmarks/singleshot/ooasp.lp similarity index 100% rename from singleshot_bench/ooasp.lp rename to benchmarks/singleshot/ooasp.lp diff --git a/singleshot_bench/ooasp_aux_config.lp b/benchmarks/singleshot/ooasp_aux_config.lp similarity index 100% rename from singleshot_bench/ooasp_aux_config.lp rename to benchmarks/singleshot/ooasp_aux_config.lp diff --git a/singleshot_bench/ooasp_aux_kb.lp b/benchmarks/singleshot/ooasp_aux_kb.lp similarity index 100% rename from singleshot_bench/ooasp_aux_kb.lp rename to benchmarks/singleshot/ooasp_aux_kb.lp diff --git a/singleshot_bench/ooasp_cautious.opt.lp b/benchmarks/singleshot/ooasp_cautious.opt.lp similarity index 100% rename from singleshot_bench/ooasp_cautious.opt.lp rename to benchmarks/singleshot/ooasp_cautious.opt.lp diff --git a/singleshot_bench/ooasp_check.lp b/benchmarks/singleshot/ooasp_check.lp similarity index 100% rename from singleshot_bench/ooasp_check.lp rename to benchmarks/singleshot/ooasp_check.lp diff --git a/singleshot_bench/ooasp_check_user.lp b/benchmarks/singleshot/ooasp_check_user.lp similarity index 100% rename from singleshot_bench/ooasp_check_user.lp rename to benchmarks/singleshot/ooasp_check_user.lp diff --git a/singleshot_bench/ooasp_guess.lp b/benchmarks/singleshot/ooasp_guess.lp similarity index 100% rename from singleshot_bench/ooasp_guess.lp rename to benchmarks/singleshot/ooasp_guess.lp diff --git a/singleshot_bench/ooasp_symmetry.lp b/benchmarks/singleshot/ooasp_symmetry.lp similarity index 100% rename from singleshot_bench/ooasp_symmetry.lp rename to benchmarks/singleshot/ooasp_symmetry.lp diff --git a/singleshot_bench/ooasp_user_input.lp b/benchmarks/singleshot/ooasp_user_input.lp similarity index 100% rename from singleshot_bench/ooasp_user_input.lp rename to benchmarks/singleshot/ooasp_user_input.lp diff --git a/singleshot_bench/racks/constraints.lp b/benchmarks/singleshot/racks/constraints.lp similarity index 100% rename from singleshot_bench/racks/constraints.lp rename to benchmarks/singleshot/racks/constraints.lp diff --git a/singleshot_bench/racks/fb.lp b/benchmarks/singleshot/racks/fb.lp similarity index 100% rename from singleshot_bench/racks/fb.lp rename to benchmarks/singleshot/racks/fb.lp diff --git a/singleshot_bench/singleshot.lp b/benchmarks/singleshot/singleshot.lp similarity index 100% rename from singleshot_bench/singleshot.lp rename to benchmarks/singleshot/singleshot.lp From 929b3d0bea3c5b91812bc91945db94ca8e96651f Mon Sep 17 00:00:00 2001 From: "Balazova, Lucia (T DAI CON-AT)" Date: Wed, 14 Aug 2024 11:57:34 +0200 Subject: [PATCH 12/13] Updated documentation --- benchmarks/singleshot/README.md | 8 ++--- benchmarks/singleshot/autorun.py | 53 ++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/benchmarks/singleshot/README.md b/benchmarks/singleshot/README.md index 66a13fd2..7c6ed830 100644 --- a/benchmarks/singleshot/README.md +++ b/benchmarks/singleshot/README.md @@ -1,15 +1,15 @@ # Singleshot benchmarking for OOASP -In this directory is collection of `.lp` files defining ooasp for purposes of singleshot benchmarking, automated python script for running them and resulting outputs of the script. +In this directory is a collection of `.lp` files defining ooasp for the purposes of singleshot benchmarking, automated python script for running them and resulting outputs of the script. ## Automated Script -The benchmarking is meant to be run using automated script `autorun.py`, which is to be run from the `singleshot_bench` directory. -The script works by automatically rewriting the `assumptions.lp` file and then using the Clingo python API to run solving and log times. +The benchmarking is meant to be run using the automated script `autorun.py`, which is to be run from the `singleshot_bench` directory. +The script works by automatically rewriting the `assumptions.lp` file and then using the Clingo python API to run solving and log the times. ### Domain sizes -As of now, the domain sizes (which need to be specified for singleshot) are only roughly approximated by formula: `n*19`, as the smallest configuration given one element of each type is 19. *Better approximation is to be further developed*. +The minimal domain sizes are calculated using a python script provided to team members. For now these should be considered the minimal solvable domain sizes. ### Logging diff --git a/benchmarks/singleshot/autorun.py b/benchmarks/singleshot/autorun.py index 64e662c8..ef067efd 100644 --- a/benchmarks/singleshot/autorun.py +++ b/benchmarks/singleshot/autorun.py @@ -4,38 +4,45 @@ import os import datetime -from typing import List +from typing import List, Annotated from clingo import Control -ELEMENT_TYPES = 4 ELEMENT_NAMES = 'ABCD' +ELEMENT_TYPES = len(ELEMENT_NAMES) -domain_sizes = [19, 33, 51, 65, 84, 98, 116, 130, 149, 163, 181, 195, 214, 228, 246, 260, 279, 293, 311, 325] -o_instances = [x+1 for x in range(20)] +domain_sizes: Annotated[List[int], "Currently known minimal domain sizes calculated by count.py script"] = [ + 19, 33, 51, 65, 84, 98, 116, 130, 149, 163, 181, 195, 214, 228, 246, 260, 279, 293, 311, 325 + ] +instances: Annotated[List[int], "Iteration identifications."] = [x+1 for x in range(20)] -def generate_ids(n: int) -> List: + +def generate_ids(n: int) -> List[str]: """ Generates combinations of element definitions with suitable IDs. + :param n: int, number of elements of each type (e.g. for iteration 4, n=4) + :return: list of fact representations strings """ ids = [id+1 for id in range(ELEMENT_TYPES*n)] assigned = [ids[i*n:i*n+n] for i in range(ELEMENT_TYPES)] - terms = [] + partial_config_facts = [] - for letter, category in enumerate(assigned): - for id in category: - terms.append(f"user(ooasp_isa(element{ELEMENT_NAMES[letter]},{id})).") + for element_index, object_ids in enumerate(assigned): + for id in object_ids: + partial_config_facts.append(f"user(ooasp_isa(element{ELEMENT_NAMES[element_index]},{id})).") - return terms + return partial_config_facts def rewrite_assumptions(content: List[str], save: bool = True) -> None: """ Rewrites the assumptions.lp file to the list of content passed. If save is set to true, creates a legacy directory and saves a copy of existing assumptions to it. + :param content: list of configuration facts + :param save: if active, saves all the generated files iin the outdated instances directory """ if os.path.isfile('instances/assumptions.lp'): @@ -50,22 +57,25 @@ def rewrite_assumptions(content: List[str], save: bool = True) -> None: file.write(t+'\n') -def add_domain(n: int) -> None: +def add_domain(size: int) -> None: """ Adds domain size constraints to the assumption file + :param size: size of the domain """ with open('instances/assumptions.lp', 'a') as file: - t = f'ooasp_domain(object,1..{n}).' + t = f'ooasp_domain(object,1..{size}).' file.write(t) def build_assumptions(n: int, save: bool = False) -> None: """ Builds the new assumptions file in its entirety. + :param n: number of iteration + :param save: flag to save old assumption files before rewriting them """ - ts = generate_ids(n) - rewrite_assumptions(ts, save=save) - add_domain(domain_sizes[n-1] if len(domain_sizes) >= n else 100) + partial_config_facts = generate_ids(n) + rewrite_assumptions(partial_config_facts, save=save) + add_domain(domain_sizes[n-1] if len(domain_sizes) >= n else 100) # 100 is a placeholder value to avoid potential errors def reset_solving() -> Control: @@ -80,10 +90,12 @@ def reset_solving() -> Control: return ctl -def log_model(model, out=False): +def log_model(model, out: bool=False) -> None: """ Creates a model directory (if it does not exist) And logs resulting models into files + :param model: representation of the model + :param out: flag to turn on stdout prints of the model """ os.makedirs('results/models', exist_ok=True) @@ -96,10 +108,13 @@ def log_model(model, out=False): print(model) -def log_results(stats: str, iteration: int, out: bool = False): +def log_results(stats: str, iteration: int, out: bool = False) -> None: """ Creates a results directory (if it does not exist) And logs times and results into text files. + :param stats: statistics to write into the file + :param iteration: identificator of the iteration + :param out: flag to activate stdout prints of the results """ os.makedirs('results/times', exist_ok=True) global timestr @@ -119,11 +134,9 @@ def on_model(m): if __name__ == "__main__": - global model - model = None global timestr timestr = str(datetime.datetime.now()).replace(' ', '_').replace('.', '-').replace(':', '-') - for iteration in o_instances: + for iteration in instances: print(f">>Solving for:{iteration}") build_assumptions(iteration) ctl = reset_solving() From dc1f236b1f2264a83d21025059f7525e303a95ba Mon Sep 17 00:00:00 2001 From: "Balazova, Lucia (T DAI CON-AT)" Date: Wed, 14 Aug 2024 13:31:26 +0200 Subject: [PATCH 13/13] Removed commented-out code in .lp files --- benchmarks/singleshot/ooasp.lp | 3 --- benchmarks/singleshot/ooasp_aux_config.lp | 12 +----------- benchmarks/singleshot/ooasp_aux_kb.lp | 6 +----- benchmarks/singleshot/ooasp_cautious.opt.lp | 20 +------------------- benchmarks/singleshot/ooasp_check.lp | 7 +------ benchmarks/singleshot/ooasp_guess.lp | 5 +---- benchmarks/singleshot/ooasp_symmetry.lp | 4 +--- benchmarks/singleshot/ooasp_user_input.lp | 2 +- benchmarks/singleshot/racks/constraints.lp | 4 +--- 9 files changed, 8 insertions(+), 55 deletions(-) diff --git a/benchmarks/singleshot/ooasp.lp b/benchmarks/singleshot/ooasp.lp index b00ab49f..7e7f6ed8 100644 --- a/benchmarks/singleshot/ooasp.lp +++ b/benchmarks/singleshot/ooasp.lp @@ -10,9 +10,6 @@ check_permanent_cv. #const kb_name = default_kb_name. ooasp_configuration(kb_name,config_name). #include "ooasp_aux_kb.lp". % Auxiliary predicates for the KB -%#external guess. % The guessing part is active -%#external check_potential_cv. % Checks for potential constraints apply -%#external check_permanent_cv. % Checks for permanent constraints apply % ------------ Incremental programs diff --git a/benchmarks/singleshot/ooasp_aux_config.lp b/benchmarks/singleshot/ooasp_aux_config.lp index 5723437d..e365989e 100644 --- a/benchmarks/singleshot/ooasp_aux_config.lp +++ b/benchmarks/singleshot/ooasp_aux_config.lp @@ -1,11 +1,6 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT -%#program domain(new_object, cls). - -%#external active(OBJECT). % Active id for arity constraints -%ooasp_domain(cls,OBJECT). - % Transitive closure when a leaf class is selected via choice ooasp_isa(LEAFCLASS,OBJECT) :- ooasp_isa_leaf(LEAFCLASS,OBJECT). @@ -24,11 +19,6 @@ ooasp_isa_smallest(SUPER,OBJECT) :- ooasp_isa(SUPER,OBJECT), not ooasp_isa(SUB,OBJECT) :ooasp_subclass(SUB,SUPER). -% Auxiliary predicate to avoid duplicated rules -% ooasp_assoc_gen(ASSOC,POS,ID1,ID2) -% Object ID1 apearing in position POS of association ASSOC is associated to object ID2 -% Example: ooasp_assoc_gen(c1,ass,2,23,10): : object 23 of the second class of association ass is associated with object 10 - ooasp_assoc_gen(ASSOC,1,ID1,OBJECT) :- ooasp_associated(ASSOC,ID1,OBJECT). diff --git a/benchmarks/singleshot/ooasp_aux_kb.lp b/benchmarks/singleshot/ooasp_aux_kb.lp index 0485782b..e8f5c08b 100644 --- a/benchmarks/singleshot/ooasp_aux_kb.lp +++ b/benchmarks/singleshot/ooasp_aux_kb.lp @@ -1,4 +1,4 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT #program base. @@ -39,10 +39,6 @@ ooasp_leafclass(C) :- ooasp_class(C), not ooasp_subclass(_,C). - -% ooasp_assoc_limit(ASSOC,min,1,C1,C2MIN,C2) -% The association ASSOC must be from a C1 object to at least C2MIN objects of class C2 - ooasp_assoc_limit(ASSOC,min,1,C1,C2MIN,C2):- ooasp_assoc(ASSOC,C1,C1MIN,C1MAX,C2,C2MIN,C2MAX). diff --git a/benchmarks/singleshot/ooasp_cautious.opt.lp b/benchmarks/singleshot/ooasp_cautious.opt.lp index 3df59736..5de6fbc0 100644 --- a/benchmarks/singleshot/ooasp_cautious.opt.lp +++ b/benchmarks/singleshot/ooasp_cautious.opt.lp @@ -1,23 +1,5 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT - -%#program domain(OBJECT, cls). - % Used when getting inferences to minimize on lowerbound errors :~ ooasp_cv(lowerbound,ID,_,(ASSOC,CMIN,N,C,OPT,OBJECT)). [CMIN-N@3,(ASSOC,ID)] - -% % Penalize the bigger associations - -% :~ ooasp_isa_leaf(C1,ID3), % Instanciates ID3 -% ooasp_isa_leaf(C2,ID1), -% not user(ooasp_associated(A, ID3, ID1)), -% not ooasp_assoc_specialization(A, _), % Only count the super -% ooasp_associated(A, ID3, ID1). [ID1@2,(A,ID3)] - - -% :~ ooasp_isa_leaf(C1,ID3), % Instanciates ID3 -% ooasp_isa_leaf(C2,ID1), -% not user(ooasp_associated(A, ID1, ID3)), -% not ooasp_assoc_specialization(A, _), % Only count the super -% ooasp_associated(A, ID1, ID3). [ID1@1,(A,ID3)] diff --git a/benchmarks/singleshot/ooasp_check.lp b/benchmarks/singleshot/ooasp_check.lp index 8eae747e..fe3b234b 100644 --- a/benchmarks/singleshot/ooasp_check.lp +++ b/benchmarks/singleshot/ooasp_check.lp @@ -1,11 +1,6 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT -% ooasp_cv(NAME,OBJECT,STR,ARGS) - -%#program domain(OBJECT, cls). - - % Integrity constraints for constraint violations, activated based on the externals :- ooasp_cv(CV,_,_,_), not ooasp_potential_cv(CV), diff --git a/benchmarks/singleshot/ooasp_guess.lp b/benchmarks/singleshot/ooasp_guess.lp index 3bdccb8a..5891a6c5 100644 --- a/benchmarks/singleshot/ooasp_guess.lp +++ b/benchmarks/singleshot/ooasp_guess.lp @@ -1,9 +1,6 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT - -%#program domain(OBJECT, cls). - % If an object has a domain, then it could be instanciated { ooasp_isa_leaf(LEAFCLASS,OBJECT): ooasp_leafclass(LEAFCLASS), diff --git a/benchmarks/singleshot/ooasp_symmetry.lp b/benchmarks/singleshot/ooasp_symmetry.lp index 21c8cf0f..b8c08dcc 100644 --- a/benchmarks/singleshot/ooasp_symmetry.lp +++ b/benchmarks/singleshot/ooasp_symmetry.lp @@ -1,8 +1,6 @@ -% Copyright (c) 2022 Siemens AG Oesterreich +% Copyright (c) 2022-2024 Siemens AG Oesterreich % SPDX-License-Identifier: MIT -%#program domain(OBJECT, cls). - :- ooasp_isa_leaf(C1,OBJECT), ooasp_isa_leaf(C2,ID), ID