def test_get_value_and_get_approx_value_nonlin_constr(self): """ min x^2 -2x + 1 st. x^2 == 4 when convexified at x = 1, min x^2 -2x + 1 + penalty_coeff*|2x-5| when penalty_coeff == 0.5, solution is x = 1.5 and the value is 1.25 (according to Wolfram Alpha) approx value should be 1.25 value should be 1.125 """ quad = QuadExpr(2*np.eye(1), -2*np.ones((1,1)), np.ones((1,1))) quad_cnt = QuadExpr(2*np.eye(1), np.zeros((1,1)), np.zeros((1,1))) eq = EqExpr(quad_cnt, np.array([[4]])) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars, np.array([[1.0]])) model.update() obj = BoundExpr(quad, var) prob.add_obj_expr(obj) bexpr = BoundExpr(eq, var) prob.add_cnt_expr(bexpr) prob.convexify() prob.update_obj(penalty_coeff=0.5) prob.optimize() self.assertTrue(np.allclose(var.get_value(), np.array([[1.5]]))) self.assertTrue(np.allclose(prob.get_approx_value(0.5), np.array([[1.25]]))) self.assertTrue(np.allclose(prob.get_value(0.5), np.array([[1.125]])))
def test_abs_expr_to_grb_expr(self): """ min |x + 1| s.t. x <= -4 """ aff = AffExpr(np.ones((1,1)), np.ones((1,1))) abs_expr = AbsExpr(aff) aff = AffExpr(np.ones((1,1)), np.zeros((1,1))) comp = LEqExpr(aff, np.array([[-4]])) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) model.update() abs_grb_expr, abs_grb_cnt = prob._abs_expr_to_grb_expr(abs_expr, var) model.update() model.setObjective(abs_grb_expr[0,0]) bexpr = BoundExpr(comp, var) prob.add_cnt_expr(bexpr) model.optimize() var.update() self.assertTrue(np.allclose(var.get_value(), np.array([[-4]]))) # makes assumption about the construction of the Gurobi variable, needs # to be changed TODO pos = abs_grb_expr[0,0].getVar(0).X neg = abs_grb_expr[0,0].getVar(1).X self.assertTrue(np.allclose(pos, 0.0)) self.assertTrue(np.allclose(neg, 3.0))
def build_variables(data): variables = [] index = 0 for direction in data: for i in range(len(direction['specs'])): # New instance of variable variable = Variable(str(direction['prefix']) + str(i)) # Get length of block values block_length = sum(direction['specs'][i]) # Stuff we need to do to find the different permutations for this block initial_specs = Builder.initial_specs(direction['specs'][i]) combinations = Builder.specs_combinations(initial_specs, direction['length']) permutations = Builder.create_permutations(combinations, direction['length'], block_length) # Set the final domain variable.domain = Builder.permutation_to_domain(direction['specs'][i], direction['length'], permutations) # Add variable to list variables.append(variable) # Increase index value index += 1 return variables
def version(self): """Retrieve the version of the database and return it. Note that this function also places the result in the associated dictionary so it is only calculated once.""" # if version has already been determined, no need to determine again if self.version_cache: return self.version_cache # allocate a cursor to retrieve the version cursor = self.cursor() # TODO: Use proper subclass # allocate version variable version_var = Variable(cursor, cursor.arraysize, vt_String, vt_String.size) # allocate compatibility variable compat_var = Variable(cursor, cursor.arraysize, vt_String, vt_String.size) # create the list of arguments list_of_arguments = [version_var, compat_var] # call stored procedure cursor.callproc("dbms_utility.db_version", list_of_arguments) # retrieve value self.version_cache = version_var.getvalue(0) return self.version_cache
def int_to_bool(self, var: Variable): var.type = 'BOOL' if var.value == 0: var.value = False else: var.value = True return var
def from_midi(stream) : import midi m = midi.MidiFile() m.open(stream) m.read() m.close() trackvar = [] for track in m.tracks : curvar = Variable() trackvar.append(curvar) curpitch = Pitch() for ev in track.events : if ev.type == "NOTE_OFF" or ev.type == "NOTE_ON" : if curpitch.havePitches() : if curpitch.duration_ : print curpitch.duration_ curvar.addPitch(curpitch.copy()) curpitch.duration_ = 0 if ev.type == "DeltaTime": curpitch.duration_ = ev.time#curpitch.plusDuration(durFromTime(ev.time)) elif ev.type == "NOTE_OFF" or ev.type == "NOTE_ON" and ev.velocity==0 : if curpitch.havePitches() : curpitch.removePitch(ev.pitch-60) elif ev.type == "NOTE_ON": curpitch.addPitch(ev.pitch-60) elif ev.type == "SET_TEMPO": print "BUGAGA", ev# TODO (?) ... else: print "midi warning: event", ev, "ignored"
def __init__(self, operator: str = None, left: Union['Expression', Variable] = None, right: Union['Expression', Variable] = None, has_not: bool = False, *, json: dict = None): # Check if the json is given if json is not None: if "operator" not in json and "left" not in json and "right" not in json: raise KeyError("Operator, left, and right keys must exist") operator = json["operator"] has_not = json.get("has_not", False) if "variable" in json["left"]: left = Variable(json=json["left"]) else: left = Expression(json=json["left"]) if "variable" in json["right"]: right = Variable(json=json["right"]) else: right = Expression(json=json["right"]) self.__operator = operator self.__left = left self.__right = right self.__has_not = has_not
def array_initialization(self, node: parser.SyntaxTreeNode, flag: bool): type = node.value.value name = node.children[0].value dim_var = self.interpreter_node(node.children[1]) if dim_var.type == 'CELL': sys.stderr.write( f'ERROR: cannot convert CELL to INT to get the dimensions count in {node.lineno}\n' ) return dim = int(dim_var.value) dimensions = self.interpreter_node(node.children[2]) if dimensions is None or len(dimensions) != dim: sys.stderr.write( f'ERROR: DIMENSIONS count shoul be equal to number of DIMENSION blocks in {node.lineno}\n' ) return values = self.interpreter_node(node.children[3]) for i in range(len(values)): values[i] = self.type_conversion(Variable(type, None, False), values[i]) if values[i] is None: sys.stderr.write( f'ERROR: initialization error of variable {name} in {node.lineno}\n' ) return dimension = 1 for i in dimensions: dimension *= i if dimension != len(values): sys.stderr.write( f'ERROR: dimension should be equal to number of VALUE blocks in {node.lineno}\n' ) return self.variable_table[self.scope][name] = Variable( type, values, flag, dim, dimensions)
def bool_to_int(self, var: Variable): var.type = 'INT' if var.value is True: var.value = 1 else: var.value = 0 return var
def test_hinge_expr_to_grb_expr2(self): """ min max(0, x+1) st. x == 1 """ aff = AffExpr(np.ones((1,1)), np.ones((1,1))) hinge = HingeExpr(aff) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) model.update() hinge_grb_expr, hinge_grb_cnt = prob._hinge_expr_to_grb_expr(hinge, var) model.update() obj = hinge_grb_expr[0,0] model.setObjective(obj) aff = AffExpr(np.ones((1,1)), np.zeros((1,1))) comp = EqExpr(aff, np.array([[1.0]])) bound_expr = BoundExpr(comp, var) prob.add_cnt_expr(bound_expr) model.optimize() var.update() self.assertTrue(np.allclose(var.get_value(), np.array([[1.0]]))) self.assertTrue(np.allclose(obj.X, 2.0))
def new_var(self, var_name, var_eqn=None): ''' Creates a new variable as part of the current model. This is the only safe way to create a new variable; they should not be created on their own and 'added' to the model somehow. ''' # create the variable first, because we do name and equation # validation in the constructor var_name = var_name.replace(' ', '_') new_var = Variable(self, var_name, var_eqn) # make sure it is actually new if self.__vars.has_key(var_name): raise ValueError, 'variable \'%s\' already exists' % var_name # keep track of the new variable self.__vars[var_name] = new_var self.__vars_list.append(new_var) new_var.connect('equation_changed', self.var_changed) if not new_var.props.valid: self.__vars_invalid.append(new_var) self.__update_model(new_var) return new_var
def createSynthesizedGenotypeDist(motherGeneVar, fatherGeneVar, genotypeVar): V = Variable(motherGeneVar, genes) W = Variable(fatherGeneVar, genes) X = Variable(genotypeVar, genotypes) return MultivariateFunction([V, W, X], { ('A', 'A', 'AA'): 1.0, ('A', 'A', 'AB'): 0.0, ('A', 'A', 'AO'): 0.0, ('A', 'A', 'BB'): 0.0, ('A', 'A', 'BO'): 0.0, ('A', 'A', 'OO'): 0.0, ('A', 'B', 'AA'): 0.0, ('A', 'B', 'AB'): 1.0, ('A', 'B', 'AO'): 0.0, ('A', 'B', 'BB'): 0.0, ('A', 'B', 'BO'): 0.0, ('A', 'B', 'OO'): 0.0, ('A', 'O', 'AA'): 0.0, ('A', 'O', 'AB'): 0.0, ('A', 'O', 'AO'): 1.0, ('A', 'O', 'BB'): 0.0, ('A', 'O', 'BO'): 0.0, ('A', 'O', 'OO'): 0.0, ('B', 'A', 'AA'): 0.0, ('B', 'A', 'AB'): 1.0, ('B', 'A', 'AO'): 0.0, ('B', 'A', 'BB'): 0.0, ('B', 'A', 'BO'): 0.0, ('B', 'A', 'OO'): 0.0, ('B', 'B', 'AA'): 0.0, ('B', 'B', 'AB'): 0.0, ('B', 'B', 'AO'): 0.0, ('B', 'B', 'BB'): 1.0, ('B', 'B', 'BO'): 0.0, ('B', 'B', 'OO'): 0.0, ('B', 'O', 'AA'): 0.0, ('B', 'O', 'AB'): 0.0, ('B', 'O', 'AO'): 0.0, ('B', 'O', 'BB'): 0.0, ('B', 'O', 'BO'): 1.0, ('B', 'O', 'OO'): 0.0, ('O', 'A', 'AA'): 0.0, ('O', 'A', 'AB'): 0.0, ('O', 'A', 'AO'): 1.0, ('O', 'A', 'BB'): 0.0, ('O', 'A', 'BO'): 0.0, ('O', 'A', 'OO'): 0.0, ('O', 'B', 'AA'): 0.0, ('O', 'B', 'AB'): 0.0, ('O', 'B', 'AO'): 0.0, ('O', 'B', 'BB'): 0.0, ('O', 'B', 'BO'): 1.0, ('O', 'B', 'OO'): 0.0, ('O', 'O', 'AA'): 0.0, ('O', 'O', 'AB'): 0.0, ('O', 'O', 'AO'): 0.0, ('O', 'O', 'BB'): 0.0, ('O', 'O', 'BO'): 0.0, ('O', 'O', 'OO'): 1.0})
def __init__(self, dimension, index=None, fields=None, double=False, profiling=False, pluto=False, fission=False, omp=True, ivdep=True, simd=False, io=False, expand=True, eval_const=True, grid_size=(10, 10, 10), domain_size=None, output_vts=False): super(RegularGrid, self).__init__() self.dimension = dimension self.double = double self.real_t = 'double' if self.double else 'float' self.dt = Variable('dt', 0.01, self.real_t, True) self.ntsteps = Variable('ntsteps', 100, 'int', True) self.alignment = mmap.PAGESIZE # default alignment for malloc # number of ghost cells for boundary self.margin = Variable('margin', 2, 'int', True) # grid size symbols, dim1, dim2, ... self.dim = [Variable('dim'+str(k+1), 10+1+self.margin.value*2, 'int', True) for k in range(self.dimension)] self.size = [1.0] * dimension # default domain size # default 2nd order in time, 4th order in space, i.e. (2,4) scheme default_order = [2] + [4]*self.dimension self.t = Symbol('_t') self.grid_size = grid_size self.max_derivative_order = 1 if fields is not None: self.fields = fields self.set_order(default_order) self.set_grid_size(grid_size) # spacing symbols, dx1, dx2, ... self.spacing = [Variable('dx'+str(k+1), int(self.size[k] / (self.dim[k].value-1-self.margin.value*2)), self.real_t, True) for k in range(self.dimension)] self.set_field_spacing() self.set_index(index) # user defined variables # use dictionary because of potential duplication self.defined_variable = {} self.defined_variable # Switches self.pluto = pluto self.omp = omp self.ivdep = ivdep self.simd = simd self.output_vts = output_vts self.expand = expand self.eval_const = eval_const self.profiling = profiling self.fission = fission if domain_size: self.set_domain_size(domain_size) self.read = False self.initialize_template()
def test_variable_functions(): ''' ensures that supported functions don't traceback ''' v = Variable("MyType", "MyName", "MyValue") assert str(v) assert repr(v) assert Variable("MyType", "MyName", "MyValue") == Variable("MyType", "MyName", "MyValue")
def load_vars(): names = datastr.get_var_names() for name in names: if 'GAUGE' in name: variable = Variable(name, 'word', 'Gauge') variable.meta['min'] = 0 variable.meta['max'] = 20 variables[name] = variable
def _load_state(self, state): for var_id, var_obj in state.iteritems(): if var_id in self._site_variables: uncommitted_values = var_obj.dump_uncommitted() committed_values = var_obj.dump_committed() new_var_obj = Variable(var_id, committed_values[0]) new_var_obj.load_state(committed_values, uncommitted_values) self._site_variables[var_id] = new_var_obj
def p_single_type_variable_list(p): '''single_type_variable_list : variable_list DASH ID''' result = [] for variable in p[1]: new_name = Variable(variable.name) new_name.type = p[3] result.append(new_name) p[0] = result
def _generate_token_list(): return [ Connector.build(TerminalSymbol.OPEN), Variable.build('x'), Connector.build(TerminalSymbol.PLUS), Variable.build('y'), Connector.build(TerminalSymbol.CLOSE) ]
def loadrule(self, row): if row["used"] == 0 or row["deleted"] > 0: return { "id": row["id"], "used": row["used"], "deleted": row["deleted"] } conf = json.loads(row["configurations"]) kid = {} kv = {} for datum in conf["basic"]: kv[datum["key"]] = Variable(datum["value"]) for datum in conf["advanced"]: kv[datum["key"]] = Variable(datum["value"]) if "priority" not in kv: kv["priority"] = Variable(RuleManager._DEF_PRIORITY) else: priority = kv["priority"].getvalue() if priority < 0: priority = 0 if priority > 5: priority = 6 if "period" not in kv: kv["period"] = Variable(RuleManager._DEF_PERIOD) if row["inputs"] is not None: inputs = json.loads(row["inputs"]) for datum in inputs: kv[datum["key"]] = self._data.getdata(datum["dataid"]) kid[datum["key"]] = datum["dataid"] outputs = json.loads(row["outputs"]) for (did, key) in self.getoutputdata(row["id"], outputs): #self._data.updatedata(did, 0) # temporary kv[key] = self._data.getdata(did) kid[key] = did print "rule inputs", kv ctrls = json.loads(row["controllers"]) for proc in ctrls['processors']: proc["pvalues"] = None return { "id": row["id"], "name": row["name"], "field": row["field_id"], "used": row["used"], "deleted": row["deleted"], "timespan": conf["timespan"], "constraints": json.loads(row["constraints"]), "controllers": ctrls, "inputs": kv, "_inputs": kid, "outputs": outputs, "executed": 0 }
def createTransmissionFunction(messageBit, transmittedBit, noiseProb): M1 = Variable(messageBit, ('0', '1')) A1 = Variable(transmittedBit, ('0', '1')) result = MultivariateFunction([M1, A1], { ('0', '0'): 1.0 - noiseProb, ('0', '1'): noiseProb, ('1', '0'): noiseProb, ('1', '1'): 1.0 - noiseProb}) return result
def test_score(self): k1 = random.random() k2 = random.random() scorer = LineScorer(k1, k2) self.assertEqual(scorer.score('a', 'a'), k1) self.assertEqual(scorer.score('a', 'b'), 0) self.assertEqual(scorer.score(Variable('a'), 'b'), 0) self.assertEqual(scorer.score(Variable('a'), Variable('b')), 0) self.assertEqual(scorer.score(Variable('b'), Variable('b')), k2)
def move_lo_from_var(self, var: Variable): assert len(self.__vars) <= 1 opers = var.get_operators() # type: List[Union[LOLambda, LOMu]] while len(opers) > 0: lo = opers.pop(-1) if var.is_zero() and isinstance(lo, LOLambda) and lo.is_inverse(): raise SideException("Move operato lambda from zero") lo.make_inverse() self.apply_loperator(lo)
def createIdentityFunction(messageBit, encodedBit): M1 = Variable(messageBit, ('0', '1')) A1 = Variable(encodedBit, ('0', '1')) result = MultivariateFunction([M1, A1], { ('0', '0'): 1.0, ('0', '1'): 0.0, ('1', '0'): 0.0, ('1', '1'): 1.0}) return result
def test_case_1(): # This test case should return False a = Variable("a") b = Variable("b") test = Proof([a, b], [a, b, Implies(a, b)]) return test.verify()
def test_extract_solution(self): simplex = Simplex() simplex.coefficients = np.array([[1, 1, 1, 0], [1, -1, 0, 1]], dtype='float') simplex.basis_variables = [Variable(index=0, is_slack=True), Variable(index=1, is_slack=False)] simplex.basis_solution = np.array([[9], [4]], dtype='float') simplex.obtain_solution() assert np.array_equal(simplex.solution, np.array([[0], [4]], dtype='float'))
def test_variable_equality(self): """Test that variables can be checked for equality through the equality operator.""" variable0 = Variable(index=2, is_slack=True) variable1 = Variable(index=2, is_slack=True) variable2 = Variable(index=1, is_slack=True) variable3 = Variable(index=2, is_slack=False) assert variable0 == variable1 assert not variable0 == variable2 assert not variable0 == variable3
def loaddata(self): query = "select * from current_observations" self._cur.execute(query, []) for row in self._cur.fetchall(): if row["nvalue"] is not None: v = Variable() v.setfromdb(row) self._data[row['data_id']] = v else: self._logger.warn(str(row['data_id']) + " is not available.")
def _init_variables(self): self._site_variables = {} for i in xrange(1, 21): var_id = 'x' + str(i) # value = int('10' + str(i)) value = 10 * i if i % 2 == 0: # Even numbered variables are replicated self._site_variables[var_id] = Variable(var_id, value) elif (i + 1) % 10 == self._id: self._site_variables[var_id] = Variable(var_id, value)
def test_save_and_restore(self): ## test to ensure saves sets self._saved_value correctly and modifying ## self._value doesn't change self._saved_value ## test to ensure that restore sets self._value to self._saved_value model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() var.save() self.assertTrue(np.allclose(var._value, np.array([2.0]))) obj = grb_var*grb_var -2*grb_var + 1 model.setObjective(obj) model.optimize() var.update() self.assertTrue(np.allclose(var._value, np.array([1.0]))) self.assertTrue(np.allclose(var._saved_value, np.array([2.0]))) var.restore() self.assertTrue(np.allclose(var._value, np.array([2.0])))
def setup_funcs(): scope = Scope() scope.add_to_scope( Function('floor', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('round', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('ceil', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('sin', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('cos', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('sin', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function('sqrt', DataType.FLOAT, [Variable('x', DataType.FLOAT)], False)) scope.add_to_scope( Function( 'pow', DataType.FLOAT, [Variable('x', DataType.FLOAT), Variable('y', DataType.FLOAT)], False)) scope.get_functions() return scope
def swap_basis_variable(self): """Moves a new variable into the basis.""" self.basis_objective[self.pivot_row_index][0] = self.objective[self.pivot_column_index] variable = Variable() if self.pivot_column_index >= self.basis_size: variable.is_slack = True variable.number = self.pivot_column_index - self.basis_size else: variable.is_slack = False variable.number = self.pivot_column_index self.basis_variables[self.pivot_row_index] = variable
def test_Pronosticos_E(self): print("test_Pronosticos_E") tenant = 'BE' varname = 'mnav_total' print_var(tenant, varname) var7 = Variable(tenant, varname) var7.get_criterio() ts = util.get_seg_epoch_now() prono = var7.get_pronostico(ts) print("Valor Pronosticado:", prono) self.assertTrue(int(prono) > 100, True)
def test_Pronosticos(self): print("test_Pronosticos") tenant = 'ES' varname = 'tot_docs' var6 = Variable(tenant, varname) print_var(tenant, varname) var6.get_criterio() ts = util.get_seg_epoch_now() prono = var6.get_pronostico(ts) print("Valor Pronosticado:", prono) self.assertTrue(int(prono) > 10000, True)
def test_get_grb_vars(self): ## test to ensure that modifying returned Gurobi Variables won't modify ## the Gurobi variables (self._grb_vars) in the Variable class model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([grb_var]) var = Variable(grb_vars) self.assertEqual(grb_vars, var.get_grb_vars()) grb_vars[0] = 0.0 self.assertNotEqual(grb_vars, var.get_grb_vars())
def calculate_loss(model, dataset, position): w = np.random.randint(args.window - 1 ) + 1 context = [] for offset in range(-w, w + 1): if offset == 0: continue c_data = xp.asarray(dataset[position + offset]) c = Variable(c_data) context.append(c) x_data = xp.asanyarray(dataset[position]) x = Variable(x_data) return model(x, context)
def test_is_valid(self): goal = Goal("New Goal") self.assertTrue(goal.is_valid()) goal.add_neighbor(Variable("New Variable"), Constraint([])) self.assertFalse(goal.is_valid()) goal = Goal("New Goal") constraint = Constraint([[Equals(3)]]) variable = Variable("New Variable", 5) goal.add_neighbor(variable, constraint) self.assertFalse(goal.is_valid())
def test_post_process_node_many_children(): b = Builder() node = InternalNode([ LeafNode.build(Variable.build('a')), LeafNode.build(Variable.build('b')), LeafNode.build(Variable.build('c')) ]) as_list = node.to_list() result = b._post_process_node(node, False) assert result == [node] assert result[0].to_list() == as_list
def test_case_2(): # This test case should return True a = Variable("a") b = Variable("b") assumptions = [a, b] proof = [a, b, Implies(a, Implies(b, a))] test = Proof(assumptions, proof) return test.verify()
def test_add_cnt_expr_eq_aff(self): aff = AffExpr(np.ones((1,1)), np.zeros((1,1))) comp = EqExpr(aff, np.array([[2]])) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) model.update() bexpr = BoundExpr(comp, var) prob.add_cnt_expr(bexpr) prob.optimize() self.assertTrue(np.allclose(var.get_value(), np.array([[2]])))
def parse(self): ds, df_summ = self.process_netcdf(netcdf=self.file_location) self.source = ds.attrs['source'] self.logger = ds.attrs['logger'] self.program_name = ds.attrs['program'] self.datafile = ds.attrs['datafile'] program_content = self.get_program() [ self.frequency, self.frequency_flag, self.timestep_count ] = self.get_programmed_frequency( program_content=program_content, datafile=self.datafile, ) for var in df_summ: self.variables.append( Variable.generate_variable( var=var, ds=ds[var], df=df_summ[var], ts=self.timestep_count ) ) return self
def generateProblemStatement(self, defaultVarDomain, questionType, highlightAnswer, format): if not self.varDomains: variables = Variable.all().ancestor(self) self.varDomains = {} for v in variables: self.varDomains[Symbol(v.name)] = v.domain + defaultVarDomain else: for v in self.varDomains.keys(): self.varDomains[v] += defaultVarDomain modelProblemClass = Category.allProblems[self.problemName] modelProblem = modelProblemClass(self.varDomains) modelProblem.generateProblem(questionType) name2Value = {} for x in modelProblem.variableValues.keys(): name2Value[x.name] = modelProblem.variableValues[x].value name2Value[modelProblem.unknown.name] = "?" distractors = name2Value[self._DISTRACTORS] answers = name2Value[self._ANSWERS] body = self.renderBody(name2Value) answerForm = self.renderAnswers( answers, distractors, modelProblem, modelProblem.unknown, questionType, highlightAnswer, format ) return body + answerForm
def test_find_closest_feasible_point_eq_cnts(self): model = grb.Model() prob = Prob(model) grb_var1 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x1') grb_var2 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x2') grb_vars = np.array([[grb_var1],[grb_var2]]) var = Variable(grb_vars, np.zeros((2,1))) model.update() val = np.array([[5.0],[-10.0]]) aff_expr = AffExpr(np.eye(2), np.zeros((2,1))) eq_expr = EqExpr(aff_expr, val) bexpr = BoundExpr(eq_expr, var) prob.add_cnt_expr(bexpr) prob.find_closest_feasible_point() self.assertTrue(np.allclose(var.get_value(), val))
def run_gauges(self): n_gauges = len(self.gauges) i = 0 while i < n_gauges: name = 'GAUGE_' + str(i).zfill(2) if name in variables: variable = variables[name] else: variable = Variable(name, 'word', name) variable.meta['min'] = 0 variable.meta['max'] = 20 variables[name] = variable sections = self.find_sections(self.gauges[i].pos) variable.value = 0 for section in sections: variable.value += section.content datastr.write(variable) i += 1
def generateVariables(specs, dimension, rowOrColumn): variables = {} for row_specIndex in range(len(specs)): domain = [] variable = Variable(rowOrColumn, row_specIndex, domain) emptyAlternative = [0 for i in range(dimension)] insertPossibilities(emptyAlternative, specs[row_specIndex], 0, domain) key = Node.getVariableKey(rowOrColumn, row_specIndex) variables[key] = variable if len(variable.domain) == 1: variable.value = variable.domain[0] return variables
def test_update(self): ## test that update updates self._value to values in Variable's Gurobi ## variables and that a GurobiError is raised if Variable's Gurobi ## variables do not have valid values model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) with self.assertRaises(grb.GurobiError) as cm: var.update() obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() self.assertTrue(np.allclose(var._value, np.array([2.0])))
def test_convexify_leq(self): model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) model.update() grb_cnt = model.addConstr(grb_var, GRB.EQUAL, 0) model.optimize() var.update() e = Expr(f) eq = LEqExpr(e, np.array([[4]])) bexpr = BoundExpr(eq, var) prob.add_cnt_expr(bexpr) prob.convexify() self.assertTrue(len(prob._penalty_exprs) == 1) self.assertTrue(isinstance(prob._penalty_exprs[0].expr, HingeExpr))
def test_optimize_just_quad_obj(self): quad = QuadExpr(2*np.eye(1), -2*np.ones((1,1)), np.zeros((1,1))) aff = AffExpr(-2*np.ones((1,1)), np.zeros((1,1))) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) bexpr_quad = BoundExpr(quad, var) bexpr_aff = BoundExpr(aff, var) prob.add_obj_expr(bexpr_quad) prob.add_obj_expr(bexpr_aff) self.assertTrue(bexpr_aff in prob._quad_obj_exprs) self.assertTrue(bexpr_quad in prob._quad_obj_exprs) self.assertTrue(var in prob._vars) prob.update_obj(penalty_coeff=0) prob.optimize() self.assertTrue(np.allclose(var.get_value(), np.array([[2.0]])))
def generate_fake(): from random import choice from faker import Faker fake = Faker() this_file = File( source=fake.word(), logger=fake.word(), filename=fake.word(), frequency=choice([.1, 60, 600, 1800]), variables=[Variable.generate_fake() for i in range(1, 10)] ) return this_file
def test_prob(ut, x0, x_true, f=zerofunc, g=neginffunc, h=zerofunc, Q=np.zeros((N,N)), q=np.zeros((1,N)), A_ineq=np.zeros((1,N)), b_ineq=np.zeros((1,1)), A_eq=np.zeros((1,1)), b_eq=np.zeros((1,1))): if not np.allclose(A_eq, np.zeros((1,1)))\ or not np.allclose(b_eq, np.zeros((1,1))): raise NotImplementedError model = grb.Model() prob = Prob(model) grb_var1 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x1') grb_var2 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x2') grb_vars = np.array([[grb_var1], [grb_var2]]) var = Variable(grb_vars, value=x0) model.update() quad_obj = BoundExpr(QuadExpr(Q, q, np.zeros((1,1))), var) prob.add_obj_expr(quad_obj) nonquad_obj = BoundExpr(Expr(f), var) prob.add_obj_expr(nonquad_obj) cnts = [] lin_ineq = LEqExpr(AffExpr(A_ineq, -b_ineq), np.zeros(b_ineq.shape)) lin_ineq = BoundExpr(lin_ineq, var) cnts.append(lin_ineq) nonlin_ineq = LEqExpr(Expr(g), np.zeros(g(np.zeros((2,1))).shape)) nonlin_ineq = BoundExpr(nonlin_ineq, var) cnts.append(nonlin_ineq) nonlin_eq = EqExpr(Expr(h), np.zeros(g(np.zeros((2,1))).shape)) nonlin_eq = BoundExpr(nonlin_eq, var) cnts.append(nonlin_eq) for cnt in cnts: prob.add_cnt_expr(cnt) solv.solve(prob, method='penalty_sqp') x_sol = var.get_value() ut.assertTrue(np.allclose(x_sol, x_true, atol=1e-4))
def test_find_closest_feasible_point_leq_cnts(self): cnt_vals = [np.ones((2,1)), np.array([[-1.0],[1.0]]), \ np.array([[-1.0],[-1.0]])] true_var_vals = [np.zeros((2,1)), np.array([[-1.0],[0.0]]), \ -1*np.ones((2,1))] for true_var_val, cnt_val in zip(true_var_vals, cnt_vals): model = grb.Model() prob = Prob(model) grb_var1 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x1') grb_var2 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x2') grb_vars = np.array([[grb_var1],[grb_var2]]) var = Variable(grb_vars, np.zeros((2,1))) model.update() aff_expr = AffExpr(np.eye(2), np.zeros((2,1))) leq_expr = LEqExpr(aff_expr, cnt_val) bexpr = BoundExpr(leq_expr, var) prob.add_cnt_expr(bexpr) prob.find_closest_feasible_point() self.assertTrue(np.allclose(var.get_value(), true_var_val))
def test_get_approx_value_lin_constr(self): """ min x^2 st. x == 4 when convexified, min x^2 + penalty_coeff*|x-4| when penalty_coeff == 1, solution is x = 0.5 and the value is 3.75 (according to Wolfram Alpha) when penalty_coeff == 2, solution is x = 1.0 and the value is 7.0 (according to Wolfram Alpha) """ quad = QuadExpr(2*np.eye(1), np.zeros((1,1)), np.zeros((1,1))) e = Expr(f) eq = EqExpr(e, np.array([[4]])) model = grb.Model() prob = Prob(model) grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') grb_vars = np.array([[grb_var]]) var = Variable(grb_vars) model.update() obj = BoundExpr(quad, var) prob.add_obj_expr(obj) bexpr = BoundExpr(eq, var) prob.add_cnt_expr(bexpr) prob.optimize() # needed to set an initial value prob.convexify() prob.update_obj(penalty_coeff=1.0) prob.optimize() self.assertTrue(np.allclose(var.get_value(), np.array([[0.5]]))) self.assertTrue(np.allclose(prob.get_approx_value(1.0), np.array([[3.75]]))) prob.update_obj(penalty_coeff=2.0) prob.optimize() self.assertTrue(np.allclose(var.get_value(), np.array([[1.0]]))) self.assertTrue(np.allclose(prob.get_approx_value(2.0), np.array([[7]])))
def test_optimize_multidim_quad_obj(self): Q = np.array([[2,0], [0,0]]) A = np.array([[-4, 0]]) quad = QuadExpr(Q, A, np.zeros((1,1))) model = grb.Model() prob = Prob(model) grb_var1 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x1') grb_var2 = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x2') grb_vars = np.array([[grb_var1], [grb_var2]]) var = Variable(grb_vars) bexpr_quad = BoundExpr(quad, var) prob.add_obj_expr(bexpr_quad) self.assertTrue(bexpr_quad in prob._quad_obj_exprs) self.assertTrue(var in prob._vars) prob.update_obj(penalty_coeff=0) prob.optimize() var_value = var.get_value() value = np.zeros((2,1)) value[0,0] = 2 self.assertTrue(np.allclose(var_value, value))
def generate_fake(): """Generate a fake File object for testing and development.""" from random import choice from faker import Faker fake = Faker() this_file = File( datafile=choice(DATA_FILES), source=fake.word(), logger=fake.word(), filename=fake.word(), frequency=choice([.1, 60, 600, 1800]), variables=[Variable.generate_fake() for i in range(1, 10)] ) return this_file
def test_add_trust_region(self): ## test trust region being computed correctly and having the correct ## effect on the Gurobi Optimization problem model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) var._saved_value = np.array([4.0]) var.add_trust_region(1.0) model.update() # upper and lower bounds aren't set until model update self.assertTrue(grb_var.lb == 3.0) self.assertTrue(grb_var.ub == 5.0) obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() self.assertTrue(np.allclose(var._value, np.array([3.0])))
def test_get_value(self): ## test that get_value returns None when self._value has no value ## test that get_value returns the correct value after update ## test to ensure that modifying returned value won't modify self._value ## in the Variable class model = grb.Model() grb_var = model.addVar(lb=-1 * GRB.INFINITY, ub=GRB.INFINITY, name='x') model.update() grb_vars = np.array([grb_var]) var = Variable(grb_vars) self.assertEqual(var.get_value(), None) obj = grb.QuadExpr() obj += grb_var*grb_var -4*grb_var + 4 model.setObjective(obj) model.optimize() var.update() val = var.get_value() self.assertTrue(np.allclose(val, np.array([2.0]))) val[0] = 1.0 self.assertTrue(np.allclose(var.get_value(), np.array([2.0])))
def has_units(func=None, summary='', doc='', inputs=None, outputs=None): r"""Function decorator: Wrap a standard python function for unit conversion. Note that conversion arguments must be supplied through the decorator aguments or in a formatted docstring as shown below. Parameters ---------- func : function, optional The function to wrap. Usually, has_units will be used as a bare "@has_units" decorator, so this argument will usually be supplied by the interpreter as it interprets that syntax. summary : str, optional A short string describing the function. doc : str, optional A longer string describing the function in detail. inputs : str, optional A string containing information about unit conversion, etc. for arguments in the function. The argument names in this string must match those in the python signature. The order the arguments are specified in does not matter as the order of arguments from the wrapped function is always used. The format of the string is as follows:: "a: notes on a:units=m/s;b: notes on b:units=m/s" That is, information about each argument is separated by a semi-colon (';'). Each argument has three fields that are separated by colons (':'). The first is the name of the variable. The 2nd is a string. The 3rd specified the units. Other fields may be added later. outputs : str, optional A string with the same format as the 'inputs' string that specifies the output variables. This *is* an ordered list as there is no way to determine the functions outputs from the function object. Description ----------- This decorator adds a unit conversion step to input and output variables of a python function. The resulting function can be used as a standard python function and has an identical calling signature to the wrapped function. If passed standard scalars and arrays as input, it will behave identically. If "unitted" objects, such as UnitArray, are handed in, however, they will be unit converted from their given units to the units expected by the function. In this case, output variables are converted from arrays or scalars to UnitArray or UnitScalar so that the results carry the units with them. Note that these objects are derived from standard numpy.ndarray and float objects, so they will behave exactly like them. The only caveat to this is that you should use isinstance(value, ndarray) instead of "type(array) is ndarray" when testing for their type, but you should be doing that anyways. Regardless of whether the inputs have units or not, the actual values passed to the function will be stripped of units. The wrapped function will only deal with regular numpy arrays and scalars. If units are not assigned to a variable, absolutely no conversion is applied to that variable. Example definition of a unitted addition function:: >>> from scimath.units.api import has_units, UnitArray >>> @has_units ... def add(a,b): ... ''' Add two arrays in ft and convert them to m. ... ... Parameters ... ---------- ... a : array : units=ft ... An array ... b : array : units=ft ... Another array ... ... Returns ... ------- ... c : array : units=m ... c = a + b ... ''' ... return (a+b)*0.3048 >>> from numpy import array >>> a = array((1,2,3)) >>> add(a,a) array([ 0.6096, 1.2192, 1.8288]) >>> from scimath.units.length import m >>> a = UnitArray((1,2,3), units=m) >>> add(a,a) # (Converts m -> ft -> m) UnitArray([ 2., 4., 6.], units='1.0*m+0.0') >>> add(a,a).units 1.0*m+0.0 Alternatively, parameter information can be specified in the decorator: >>> from numpy import array >>> from scimath.units.api import has_units >>> @has_units(inputs="a:an array:units=ft;b:array:units=ft", ... outputs="result:an array:units=m") ... def add(a,b): ... " Add two arrays in ft and convert them to m. " ... return (a+b)*0.3048 The returned function has several attributes attached to it: summary: A short description of the function. This is taken from the 'summary' argument to the decorator. doc: A longer description of the function. This is taken from the 'doc' argument to the decorator. inputs: A list of Variable objects, for each argument to the function *in the order they are defined in the python function signature*. If you did not supply any information about the argument in "inputs", then a default Variable object is created. outputs: A list of Variable objects, for each output of the function in the order they you specify them in the "outputs" variable in the argument list. """ # If has_units is applied on a function directly to make use of the # function's docstrings if func is not None: ## Strip indentation/whitespace before and after each line of docstring stripped_lines = [line.strip() for line in func.__doc__.expandtabs().splitlines()] unitted_inputlines, unitted_outputlines = simple_parser(stripped_lines) # Process inputs and outputs to pass as parameters to _has_units # function inputs_dict = {} outputs_list = [] for input_string in unitted_inputlines: input = Variable.from_string(input_string) inputs_dict[input.name] = input for output_string in unitted_outputlines: outputs_list.append(Variable.from_string(output_string)) return _has_units(summary, doc, inputs_dict, outputs_list)(func) else: # func is None inputs_dict = {} if inputs is not None: # fixme: extremely lame -- no error detection. for input_string in inputs.strip().split(';'): if input_string: input = Variable.from_string(input_string) inputs_dict[input.name] = input outputs_list = [] if outputs is not None: for output_string in outputs.strip().split(';'): if output_string: outputs_list.append(Variable.from_string(output_string)) if not outputs_list: outputs_list = [Variable(name="result")] return _has_units(summary, doc, inputs_dict, outputs_list)