How to use evaluate_raw method in localstack

Best Python code snippet using localstack_python

proximal.py

Source:proximal.py Github

copy

Full Screen

...22 self._scale = scale23 self._shift = shift24 self._is_convex = None # TODO: Instantiate this here?25 @abstractmethod26 def evaluate_raw(self, v):27 pass28 @abstractmethod29 def prox_raw(self, rho, v):30 pass31 @abstractmethod32 def subdiff_raw(self, v):33 pass34 def evaluate(self, v):35 return self._weight * self.evaluate_raw(self._scale * v - self._shift)36 def prox(self, rho, equil_scaling, v):37 new_scale = equil_scaling * self._scale38 new_rho = rho / (self._weight * new_scale**2)39 return (40 self.prox_raw(new_rho, new_scale * v - self._shift) + self._shift41 ) / new_scale42 def subdiff(self, equil_scaling, obj_scale, v):43 g_ls, g_rs = self.subdiff_raw(self._scale * equil_scaling * v - self._shift)44 g_ls *= obj_scale * equil_scaling * self._weight * self._scale45 g_rs *= obj_scale * equil_scaling * self._weight * self._scale46 return g_ls, g_rs47class Zero(G):48 def __init__(self, weight, scale, shift):49 super().__init__(weight, scale, shift)50 self._is_convex = True51 def evaluate_raw(self, v):52 return np.zeros(np.asarray(v).shape)53 def prox_raw(self, rho, v):54 return np.asarray(v)55 def subdiff_raw(self, v):56 v = np.asarray(v)57 return np.zeros(v.shape), np.zeros(v.shape)58class Abs(G):59 def __init__(self, weight, scale, shift):60 super().__init__(weight, scale, shift)61 self._is_convex = True62 def evaluate_raw(self, v):63 return np.abs(v)64 def prox_raw(self, rho, v):65 return np.maximum(v - 1 / rho, 0) - np.maximum(-v - 1 / rho, 0)66 def subdiff_raw(self, v):67 v = np.asarray(v)68 ls = np.zeros(v.shape)69 rs = np.zeros(v.shape)70 ls[v < 0] = -171 rs[v < 0] = -172 ls[v > 0] = 173 rs[v > 0] = 174 ls[np.isclose(v, 0)] = -175 rs[np.isclose(v, 0)] = 176 return ls, rs77class IsPos(G):78 def __init__(self, weight, scale, shift):79 super().__init__(weight, scale, shift)80 self._is_convex = True81 def evaluate_raw(self, v):82 v = np.asarray(v)83 return np.where(v >= 0, 0, np.inf)84 def prox_raw(self, rho, v):85 v = np.asarray(v)86 return np.where(v < 0, 0, v)87 def subdiff_raw(self, v):88 v = np.asarray(v)89 ls = np.zeros(v.shape)90 rs = np.zeros(v.shape)91 ls[v < 0] = np.nan92 rs[v < 0] = np.nan93 ls[v == 0] = -np.inf94 rs[v == 0] = 095 return ls, rs96class IsNeg(G):97 def __init__(self, weight, scale, shift):98 super().__init__(weight, scale, shift)99 self._is_convex = True100 def evaluate_raw(self, v):101 v = np.asarray(v)102 return np.where(v <= 0, 0, np.inf)103 def prox_raw(self, rho, v):104 v = np.asarray(v)105 return np.where(v > 0, 0, v)106 def subdiff_raw(self, v):107 v = np.asarray(v)108 ls = np.zeros(v.shape)109 rs = np.zeros(v.shape)110 ls[v > 0] = np.nan111 rs[v > 0] = np.nan112 ls[v == 0] = 0113 rs[v == 0] = np.inf114 return ls, rs115class IsBound(G):116 def __init__(self, weight, scale, shift, lb, ub):117 super().__init__(weight, scale, shift)118 self._lb = lb119 self._ub = ub120 self._is_convex = True121 def evaluate_raw(self, v):122 return np.where((v >= self._lb) & (v <= self._ub), 0, np.inf)123 def prox_raw(self, rho, v):124 output = np.where(v >= self._ub, self._ub, v)125 output = np.where(output <= self._lb, self._lb, output)126 return output127 def subdiff_raw(self, v):128 v = np.asarray(v)129 ls = np.zeros(v.shape)130 rs = np.zeros(v.shape)131 ls[v > self._ub] = np.nan132 rs[v > self._ub] = np.nan133 ls[v == self._ub] = 0134 rs[v == self._ub] = np.inf135 ls[v < self._lb] = np.nan136 rs[v < self._lb] = np.nan137 ls[v == self._lb] = -np.inf138 rs[v == self._lb] = 0139 return ls, rs140class IsZero(G):141 def __init__(self, weight, scale, shift):142 super().__init__(weight, scale, shift)143 self._is_convex = True144 def evaluate_raw(self, v):145 return np.where(v == 0, 0, np.inf)146 def prox_raw(self, rho, v):147 return np.zeros(np.asarray(v).shape)148 def subdiff_raw(self, v):149 v = np.asarray(v)150 ls = np.nan * np.ones(v.shape)151 rs = np.nan * np.ones(v.shape)152 ls[v == 0] = -np.inf153 rs[v == 0] = np.inf154 return ls, rs155class Pos(G):156 def __init__(self, weight, scale, shift):157 super().__init__(weight, scale, shift)158 self._is_convex = True159 def evaluate_raw(self, v):160 return np.maximum(v, 0)161 def prox_raw(self, rho, v):162 output = np.where(v <= 0, v, 0)163 output = np.where(v > 1 / rho, v - 1 / rho, output)164 return output165 def subdiff_raw(self, v):166 v = np.asarray(v)167 ls = np.zeros(v.shape)168 rs = np.zeros(v.shape)169 ls[v > 0] = 1170 rs[v > 0] = 1171 # TODO: change this to is_close?172 ls[v == 0] = 0173 rs[v == 0] = 1174 return ls, rs175class Neg(G):176 def __init__(self, weight, scale, shift):177 super().__init__(weight, scale, shift)178 self._is_convex = True179 def evaluate_raw(self, v):180 return np.maximum(-v, 0)181 def prox_raw(self, rho, v):182 return np.where(v < -1 / rho, v + 1 / rho, v)183 def subdiff_raw(self, v):184 v = np.asarray(v)185 ls = np.zeros(v.shape)186 rs = np.zeros(v.shape)187 ls[v < 0] = -1188 rs[v < 0] = -1189 # TODO: change this to is_close?190 ls[v == 0] = -1191 rs[v == 0] = 0192 return ls, rs193class Card(G):194 def __init__(self, weight, scale, shift):195 super().__init__(weight, scale, shift)196 self._is_convex = False197 def evaluate_raw(self, v):198 # TODO: change this to isclose?199 return np.where(v == 0, 0, 1)200 def prox_raw(self, rho, v):201 return np.where(np.abs(v) < np.sqrt(2 / rho), 0, v)202 def subdiff_raw(self, v):203 v = np.asarray(v)204 ls = np.nan * np.ones(v.shape)205 rs = np.nan * np.ones(v.shape)206 ls[v == 0] = 0207 rs[v == 0] = 0208 return ls, rs209class Quantile(G):210 def __init__(self, weight, scale, shift, tau):211 super().__init__(weight, scale, shift)212 self._tau = tau213 self._is_convex = True214 def evaluate_raw(self, v):215 return 0.5 * np.abs(v) + (self._tau - 0.5) * v216 def prox_raw(self, rho, v):217 v_mod = np.asarray(v) + 1 / rho * (0.5 - self._tau)218 return np.where(219 np.abs(v_mod) <= 1 / (2 * rho), 0, v_mod - np.sign(v_mod) * 1 / (2 * rho)220 )221 def subdiff_raw(self, v):222 v = np.asarray(v)223 ls = np.zeros(v.shape)224 rs = np.zeros(v.shape)225 ls[v > 0] = self._tau226 rs[v > 0] = self._tau227 ls[v < 0] = self._tau - 1228 rs[v < 0] = self._tau - 1229 # TODO: change to is_close?230 ls[v == 0] = self._tau - 1231 rs[v == 0] = self._tau232 return ls, rs233class Huber(G):234 def __init__(self, weight, scale, shift, M):235 super().__init__(weight, scale, shift)236 self._M = M237 self._is_convex = True238 def evaluate_raw(self, v):239 abs_v = np.abs(v)240 return np.where(241 abs_v <= self._M, abs_v**2, 2 * self._M * abs_v - self._M * self._M242 )243 def prox_raw(self, rho, v):244 return np.where(245 np.abs(v) <= self._M * (rho + 2) / rho,246 rho / (2 + rho) * v,247 v - np.sign(v) * 2 * self._M / rho,248 )249 def subdiff_raw(self, v):250 v = np.asarray(v)251 ls = np.zeros(v.shape)252 rs = np.zeros(v.shape)253 abs_v = np.abs(v)254 ls[abs_v <= self._M] = 2 * v[abs_v <= self._M]255 rs[abs_v <= self._M] = 2 * v[abs_v <= self._M]256 ls[v > self._M] = 2 * self._M257 rs[v > self._M] = 2 * self._M258 ls[v < self._M] = -2 * self._M259 rs[v < self._M] = -2 * self._M260 return ls, rs261class IsInt(G):262 def __init__(self, weight, scale, shift):263 super().__init__(weight, scale, shift)264 self._is_convex = False265 def evaluate_raw(self, v):266 return np.where(267 np.isclose(np.mod(v, 1), 0) | np.isclose(np.mod(v, 1), 1), 0, np.inf268 )269 # TODO: change this to something like np.isclose(v, np.rint(v))270 def prox_raw(self, rho, v):271 return np.rint(v)272 def subdiff_raw(self, v):273 v = np.asarray(v)274 ls = np.nan * np.ones(v.shape)275 rs = np.nan * np.ones(v.shape)276 int_indices = self.evaluate_raw(v)277 ls[int_indices == 0] = 0278 rs[int_indices == 0] = 0279 return ls, rs280class IsFiniteSet(G):281 def __init__(self, weight, scale, shift, S):282 super().__init__(weight, scale, shift)283 self._S = np.array(list(S))284 self._is_convex = False285 def evaluate_raw(self, v):286 v = np.asarray(v)287 is_almost_in_S = np.isclose(v.reshape((-1, 1)), self._S.reshape((1, -1))).any(288 axis=1289 )290 return np.where(is_almost_in_S, 0, np.inf)291 def prox_raw(self, rho, v):292 v = np.asarray(v)293 diffs = np.subtract(v.reshape((-1, 1)), self._S.reshape((1, -1)))294 idx = np.argmin(np.abs(diffs), axis=1)295 return self._S[idx]296 def subdiff_raw(self, v):297 v = np.asarray(v)298 ls = np.nan * np.ones(v.shape)299 rs = np.nan * np.ones(v.shape)300 in_set_indices = self.evaluate_raw(v)301 ls[in_set_indices == 0] = 0302 rs[in_set_indices == 0] = 0303 return ls, rs304class IsBool(G):305 def __init__(self, weight, scale, shift):306 super().__init__(weight, scale, shift)307 self._S = np.array([0, 1])308 self._is_convex = False309 def evaluate_raw(self, v):310 v = np.asarray(v)311 is_almost_in_S = np.isclose(v.reshape((-1, 1)), self._S.reshape((1, -1))).any(312 axis=1313 )314 return np.where(is_almost_in_S, 0, np.inf)315 def prox_raw(self, rho, v):316 v = np.asarray(v)317 diffs = np.subtract(v.reshape((-1, 1)), self._S.reshape((1, -1)))318 idx = np.argmin(np.abs(diffs), axis=1)319 return self._S[idx]320 def subdiff_raw(self, v):321 v = np.asarray(v)322 ls = np.nan * np.ones(v.shape)323 rs = np.nan * np.ones(v.shape)324 is_bool_indices = self.evaluate_raw(v)325 ls[is_bool_indices == 0] = 0326 rs[is_bool_indices == 0] = 0327 return ls, rs328class GCollection:329 def __init__(self, g_list):330 self._g_list = []331 self._is_convex = True332 self._all_zeros = True333 for g in g_list:334 weight = 1335 scale = 1336 shift = 0337 if "args" in g and "weight" in g["args"]:338 weight = g["args"]["weight"]...

Full Screen

Full Screen

day18.py

Source:day18.py Github

copy

Full Screen

...20 return value21# Regex to match open-parens ... closed-parens22# with no parens in between 23rgx = r"\([^\(]+?\)"24def evaluate_raw(raw: str) -> int:25 while (match := re.search(rgx, raw)):26 value = evaluate(match.group()[1:-1].split())27 raw = raw[:match.start()] + str(value) + raw[match.end():]28 else:29 tokens = raw.split()30 return evaluate(tokens)31def evaluate2(expression: Expression) -> int:32 """again assumes expression has no parens"""33 while len(expression) > 1:34 if "+" in expression:35 # find the plus and use it on the surrounding numbers36 i = expression.index("+")37 new_val = int(expression[i-1]) + int(expression[i+1])38 # and then put that result back39 expression = expression[:i-1] + [str(new_val)] + expression[i+2:]40 else:41 return evaluate(expression)42 return int(expression[0])43def evaluate_raw2(raw: str) -> int:44 while (match := re.search(rgx, raw)):45 value = evaluate2(match.group()[1:-1].split())46 raw = raw[:match.start()] + str(value) + raw[match.end():]47 else:48 tokens = raw.split()49 return evaluate2(tokens)50#51# unit tests52#53RAW = "1 + 2 * 3 + 4 * 5 + 6"54assert evaluate_raw(RAW) == 7155assert evaluate_raw2(RAW) == 23156RAW2 = "((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"57assert evaluate_raw(RAW2) == 1363258assert evaluate_raw2(RAW2) == 2334059#60# problem61#62with open('inputs/day18.txt') as f:63 raw = f.read()64 lines = raw.split("\n")65 print(sum(evaluate_raw(line) for line in lines))...

Full Screen

Full Screen

calc.py

Source:calc.py Github

copy

Full Screen

...18 return value19# Regex to match open-parens ... closed-parens20# with no parens in between21rgx = r"\([^\(]+?\)"22def evaluate_raw(raw: str) -> int:23 while (match := re.search(rgx, raw)):24 value = evaluate(match.group()[1:-1].split())25 raw = raw[:match.start()] + str(value) + raw[match.end():]26 else:27 tokens = raw.split()28 return evaluate(tokens)29def evaluate2(expression: Expression) -> int:30 """again assumes expression has no parens"""31 while len(expression) > 1:32 if "+" in expression:33 # find the plus and use it on the surrounding numbers34 i = expression.index("+")35 new_val = int(expression[i-1]) + int(expression[i+1])36 # and then put that result back37 expression = expression[:i-1] + [str(new_val)] + expression[i+2:]38 else:39 return evaluate(expression)40 return int(expression[0])41def evaluate_raw2(raw: str) -> int:42 while (match := re.search(rgx, raw)):43 value = evaluate2(match.group()[1:-1].split())44 raw = raw[:match.start()] + str(value) + raw[match.end():]45 else:46 tokens = raw.split()47 return evaluate2(tokens)48#49# unit tests50#51RAW = "1 + 2 * 3 + 4 * 5 + 6"52assert evaluate_raw(RAW) == 7153assert evaluate_raw2(RAW) == 23154RAW2 = "((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"55assert evaluate_raw(RAW2) == 1363256assert evaluate_raw2(RAW2) == 2334057#58# problem59#60with open('input.txt') as f:61 raw = f.read()62 lines = raw.split("\n")63 print(sum(evaluate_raw(line) for line in lines))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful