Skip to content
Snippets Groups Projects
Commit 2ab901a9 authored by aastorg2's avatar aastorg2
Browse files

adding code that generates names( features) file

parent 4810bdc3
No related merge requests found
......@@ -3,25 +3,28 @@ import numpy as np
import os
from learner_base import LearnerBase
import csv
import grammar_generation_utils
class DTreeLearner(LearnerBase):
def __init__(self, state_dim: int, perc_dim: int,
timeout: int = 10000) -> None:
super().__init__()
#learners/C50exact/c5.0dbg -I 1 -m 1 -f FileLocation/pre
#C50exact/c5.0dbg -I 1 -m 1 -f tempLocation/pre
self._state_dim: int = state_dim
self._perc_dim: int = perc_dim
self._s2f_func_list: List = []
dir_name = "tempLocation"
check_temp_dir: bool = os.path.isdir(dir_name)
#check tempLocation dir exists, if not create it.
self.dir_name = "tempLocation"
check_temp_dir: bool = os.path.isdir(self.dir_name)
if not check_temp_dir:
os.makedirs(dir_name)
os.makedirs(self.dir_name)
self.data_file = dir_name+"/pre.data"
self.names_file = dir_name+"/pre.names"
open(self.data_file,'w').close() # create empty file
# create empty files
self.data_file = self.dir_name+"/pre.data"
self.names_file = self.dir_name+"/pre.names"
open(self.data_file,'w').close()
open(self.names_file,'w').close()
@property
......@@ -37,6 +40,9 @@ class DTreeLearner(LearnerBase):
self._s2f_func_list.append(
construct_sample_to_feature_func(a_mat, b_vec))
num_vars = ["dbar", "psibar"]
grammar_generation_utils.generateInputLanguageFile(self.names_file, num_vars, [])
def add_implication_examples(self, *args) -> None:
return super().add_implication_examples(*args)
......@@ -86,29 +92,6 @@ def construct_sample_to_feature_func(a_mat: np.ndarray, b_vec: np.ndarray):
return perc_bar
return sample_to_feature_vec
def test_sample_to_feature():
# tuple
a_mat = np.array([[0., -1., 0.],
[0., 0., -1]])
b_vec = np.zeros(2)
#construct_sample_to_feature_func: returns a function
#map: lin_trans(a_mat and b_vec pair) -> func
sample_to_feature_func = construct_sample_to_feature_func(a_mat, b_vec)
#map = {name1:sample_to_feature_func}
sample = np.array([1., 2., 3., -2., -3.])
# sample_to_feature_func will compute dBar and psiBar
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([0., 0.]))
sample = np.array([1., 2., 3., -1., -2.])
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([1., 1.]))
def test_dtree_learner():
a_mat = np.array([[0., -1., 0.],
[0., 0., -1]])
......@@ -132,6 +115,28 @@ def test_dtree_learner():
# learner.learn()
def test_sample_to_feature():
# tuple
a_mat = np.array([[0., -1., 0.],
[0., 0., -1]])
b_vec = np.zeros(2)
#construct_sample_to_feature_func: returns a function
#map: lin_trans(a_mat and b_vec pair) -> func
sample_to_feature_func = construct_sample_to_feature_func(a_mat, b_vec)
#map = {name1:sample_to_feature_func}
sample = np.array([1., 2., 3., -2., -3.])
# sample_to_feature_func will compute dBar and psiBar
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([0., 0.]))
sample = np.array([1., 2., 3., -1., -2.])
feature_vec = sample_to_feature_func(sample)
print("sample: "+ str(feature_vec))
assert np.array_equal(feature_vec, np.array([1., 1.]))
if __name__ == "__main__":
#test_sample_to_feature()
......
import itertools
from typing import List, Tuple
def generateInputLanguageFile(nameOfFile:str, intVariables: List[str], boolVariables: List[str]):
fileContents= generateExpressionsBools(intVariables, boolVariables)
try:
with open(nameOfFile, 'wt') as outfile:
outfile.write(fileContents)
except Exception as e:
raise Exception(str(e) + ": " + "error writing files---line 12 grammar_generation_utils.py")
def generateExpressionsBools(intVariables: List[str], boolVariables: List[str])-> str:
coeff_combination = make_coeff_linear_combination(len(intVariables), -1, 1)
names_file = 'precondition.'
for var in intVariables:
names_file += '\n' + var + ': continuous.'
# adding boolean observer method features
for var in boolVariables:
names_file += '\n' + var + ': true, false.'
# check equality of integer variables
if len(intVariables) >= 2:
all_combination = itertools.combinations(intVariables, 2)
for (var1, var2) in all_combination:
expr = "(" + var1 + " = " + var2 + ")"
name_expr = "( = " + var1 + " " + var2 + " )"
names_file += '\n' + name_expr + ' := ' + expr + ' .' #needs change
for coeff in coeff_combination:
# old way of generating all possible combinations
expr = ' + '.join(map(lambda x,y: "(" + str(x) + "*" + y + ")", coeff, intVariables))
name_expr = ' ( + ' + ' '.join(map(lambda x,y: "( * " + str(x) + " " + y + " )", coeff, intVariables)) + ' )'
names_file += '\n' + name_expr + ' := ' + expr + ' .'
names_file += '\nprecondition: true,false.'
return names_file
def make_coeff_linear_combination( number_of_variables, low, high):
init_list = [[i] for i in range(low, high + 1)]
a = init_list
for i in range(1, number_of_variables):
a = [x + y for x in a for y in init_list] # x + y -- concatenation of lists
return a
if __name__ == '__main__':
intVariables = ["dbar", "psibar"]
boolVariables = []
fullPathLocation = "/home/aastorg2/intelligibleAbs/cs598mp-fall2021-proj/tempLocation/pre.names"
generateInputLanguageFile(fullPathLocation, intVariables)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment