Skip to content

Commit

Permalink
ZLT: Support for output variable in percent
Browse files Browse the repository at this point in the history
use zlp instead of zlt
  • Loading branch information
tbarbette committed Aug 3, 2024
1 parent 61fcef7 commit bca0608
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
2 changes: 2 additions & 0 deletions integration/zlt.npf
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ else
fi

t=$(echo "$RATE*(100-$d)/100" | bc)
p=$(echo "$t * 100 / $RATE" | bc)
echo "RESULT-DROPPEDPC $d"
echo "RESULT-GOODPC $p"
echo "RESULT-THROUGHPUT $t"
30 changes: 20 additions & 10 deletions npf/expdesign/zltexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
from npf.types.dataset import Run
from npf.variable import Variable


class ZLTVariableExpander(FullVariableExpander):

def __init__(self, vlist:Dict[str,Variable], results, overriden, input, output, margin, all=False):


class OptVariableExpander(FullVariableExpander):
def __init__(self, vlist:Dict[str,Variable], results, overriden, input, margin, all=False):
if not input in vlist:
raise Exception(f"{input} is not in the variables, please define a variable in the %variable section.")

self.results = results
self.input = input
self.input_values = vlist[input].makeValues()
if len(self.input_values) <= 2:
print(f"WARNING: Doing zero-loss-throughput search on the variable {input} that has only {len(self.input_values)} values. This is useless."
f"You must define a range to search with a variable like {input}=[0-100#5].")
del vlist[input]
self.current = None
self.output = output
self.n_done = 0
self.n_it = 0
self.n_tot_done = 0
self.margin = margin
self.all = all
super().__init__(vlist, overriden)

def __iter__(self):
self.it = self.expanded.__iter__()
self.current = None
Expand All @@ -38,6 +37,15 @@ def __iter__(self):
def __len__(self):
return int(len(self.expanded) * ceil(log2(len(self.input_values)) if (self.n_it <= 1) else self.n_tot_done/(self.n_it - 1)))

class ZLTVariableExpander(OptVariableExpander):

def __init__(self, vlist:Dict[str,Variable], results, overriden, input, output, margin, all=False, perc=False):

self.output = output
self.perc = perc
super().__init__(vlist, results, overriden, input, margin, all)


def __next__(self):

if self.current == None:
Expand All @@ -57,6 +65,8 @@ def __next__(self):
if self.output:
r_out = np.mean(vals[self.output])
r_in = r.variables[self.input]
if self.perc:
r_out = r_out/100 * r_in
vals_for_current[r_in] = r_out
if r_out >= r_in/self.margin:
acceptable_rates.append(r_in)
Expand All @@ -65,7 +75,7 @@ def __next__(self):
except KeyError:
raise Exception(f"{self.output} is not in the results. Sample of last result : {vals}")

#Step 1 : try the max output
#Step 1 : try the max input rate first
if len(vals_for_current) == 0:
next_val = max_r
elif len(vals_for_current) == 1:
Expand All @@ -75,7 +85,7 @@ def __next__(self):
self.current = None
return self.__next__()

#Step 2 : go for the rate below the max output
#Step 2 : go for the rate below the output of the max input
maybe_achievable_inputs = list(filter(lambda x : x <= max_r, self.input_values))
next_val = max(maybe_achievable_inputs)
else:
Expand Down
16 changes: 10 additions & 6 deletions npf/sections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ def replace_all(self, value):
def expand(self, results={}, method="full", overriden=set()):
if method == "shuffle" or method == "rand" or method == "random":
return RandomVariableExpander(self.vlist)
elif method.lower().startswith("zlt"):
params = method[4:-1].split(",")
return ZLTVariableExpander(self.vlist, overriden=overriden, results=results, input=params[0], output=params[1], margin=1.01 if len(params) == 2 else float(params[2]))
elif method.lower().startswith("allzlt"):
params = method[7:-1].split(",")
return ZLTVariableExpander(self.vlist, overriden=overriden, results=results, input=params[0], output=params[1], margin=1.01 if len(params) == 2 else float(params[2]), all=True)
elif "zl" in method.lower():
if method.lower().startswith("allzl"):
params = method[7:-1].split(",")
all = True
perc = method.lower()[5] == 'p'
else:
params = method[4:-1].split(",")
all = False
perc = method.lower()[2] == 'p'
return ZLTVariableExpander(self.vlist, overriden=overriden, results=results, input=params[0], output=params[1], margin=1.01 if len(params) == 2 else float(params[2]), all=all, perc=perc)

else:
return FullVariableExpander(self.vlist, overriden)
Expand Down

0 comments on commit bca0608

Please sign in to comment.