本文整理汇总了Python中pyomo.environ.value函数的典型用法代码示例。如果您正苦于以下问题:Python value函数的具体用法?Python value怎么用?Python value使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了value函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_acos
def test_acos(self):
m = pe.Block(concrete=True)
m.x = pe.Var()
m.c = pe.Constraint(expr=pe.inequality(body=pe.acos(m.x), lower=1, upper=2))
fbbt(m)
self.assertAlmostEqual(pe.value(m.x.lb), math.cos(2))
self.assertAlmostEqual(pe.value(m.x.ub), math.cos(1))
开发者ID:mskarha,项目名称:pyomo,代码行数:7,代码来源:test_fbbt.py
示例2: test_GDP_nonlinear_objective
def test_GDP_nonlinear_objective(self):
m = ConcreteModel()
m.x = Var(bounds=(-1, 10))
m.y = Var(bounds=(2, 3))
m.d = Disjunction(expr=[
[m.x + m.y >= 5], [m.x - m.y <= 3]
])
m.o = Objective(expr=m.x ** 2)
SolverFactory('gdpopt').solve(
m, strategy='LOA',
mip_solver=mip_solver,
nlp_solver=nlp_solver
)
self.assertAlmostEqual(value(m.o), 0)
m = ConcreteModel()
m.x = Var(bounds=(-1, 10))
m.y = Var(bounds=(2, 3))
m.d = Disjunction(expr=[
[m.x + m.y >= 5], [m.x - m.y <= 3]
])
m.o = Objective(expr=-m.x ** 2, sense=maximize)
SolverFactory('gdpopt').solve(
m, strategy='LOA',
mip_solver=mip_solver,
nlp_solver=nlp_solver
)
self.assertAlmostEqual(value(m.o), 0)
开发者ID:Pyomo,项目名称:pyomo,代码行数:28,代码来源:test_gdpopt.py
示例3: save_cost_components
def save_cost_components(m, outdir):
"""
Save values for all individual components of total system cost on NPV basis.
"""
cost_dict = dict()
for annual_cost in m.Cost_Components_Per_Period:
cost = getattr(m, annual_cost)
# note: storing value() instead of the expression may save
# some memory while this function runs
cost_dict[annual_cost] = value(sum(
cost[p] * m.bring_annual_costs_to_base_year[p]
for p in m.PERIODS
))
for tp_cost in m.Cost_Components_Per_TP:
cost = getattr(m, tp_cost)
cost_dict[tp_cost] = value(sum(
cost[t] * m.tp_weight_in_year[t]
* m.bring_annual_costs_to_base_year[m.tp_period[t]]
for t in m.TIMEPOINTS
))
write_table(
m,
cost_dict.keys(),
output_file=os.path.join(outdir, "cost_components.tab"),
headings=('component', 'npv_cost'),
values=lambda m, c: (c, cost_dict[c]),
digits=16
)
开发者ID:switch-model,项目名称:switch,代码行数:28,代码来源:__init__.py
示例4: test_atan
def test_atan(self):
m = pe.Block(concrete=True)
m.x = pe.Var()
m.c = pe.Constraint(expr=pe.inequality(body=pe.atan(m.x), lower=-0.5, upper=0.5))
fbbt(m)
self.assertAlmostEqual(pe.value(m.x.lb), math.tan(-0.5))
self.assertAlmostEqual(pe.value(m.x.ub), math.tan(0.5))
开发者ID:mskarha,项目名称:pyomo,代码行数:7,代码来源:test_fbbt.py
示例5: test_ignore_nonlinear
def test_ignore_nonlinear(self):
m = ConcreteModel()
m.v1 = Var()
m.c1 = Constraint(expr=m.v1 * m.v1 >= 2)
self.assertEqual(value(m.c1.lower), 2)
self.assertFalse(m.c1.has_ub())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.lower), 2)
self.assertFalse(m.c1.has_ub())
开发者ID:Pyomo,项目名称:pyomo,代码行数:10,代码来源:test_constraint_tightener.py
示例6: test_pow
def test_pow(self):
m = pe.ConcreteModel()
m.x = pe.Var(initialize=2.0)
m.y = pe.Var(initialize=3.0)
e = m.x ** m.y
derivs = reverse_ad(e)
symbolic = reverse_sd(e)
self.assertAlmostEqual(derivs[m.x], pe.value(symbolic[m.x]), tol+3)
self.assertAlmostEqual(derivs[m.y], pe.value(symbolic[m.y]), tol+3)
self.assertAlmostEqual(derivs[m.x], approx_deriv(e, m.x), tol)
self.assertAlmostEqual(derivs[m.y], approx_deriv(e, m.y), tol)
开发者ID:Pyomo,项目名称:pyomo,代码行数:11,代码来源:test_derivs.py
示例7: approx_deriv
def approx_deriv(expr, wrt, delta=0.001):
numerator = 0
wrt.value += 2*delta
numerator -= pe.value(expr)
wrt.value -= delta
numerator += 8*pe.value(expr)
wrt.value -= 2*delta
numerator -= 8*pe.value(expr)
wrt.value -= delta
numerator += pe.value(expr)
wrt.value += 2*delta
return numerator / (12*delta)
开发者ID:Pyomo,项目名称:pyomo,代码行数:12,代码来源:test_derivs.py
示例8: test_less_than_constraint
def test_less_than_constraint(self):
m = ConcreteModel()
m.v1 = Var(initialize=7, bounds=(7, 10))
m.v2 = Var(initialize=2, bounds=(2, 5))
m.v3 = Var(initialize=6, bounds=(6, 9))
m.v4 = Var(initialize=1, bounds=(1, 1))
m.c1 = Constraint(expr=m.v1 <= m.v2 + m.v3 + m.v4)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.upper), 0)
self.assertEqual(value(m.c1.lower), -8)
开发者ID:Pyomo,项目名称:pyomo,代码行数:13,代码来源:test_constraint_tightener.py
示例9: test_add
def test_add(self):
x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
for xl, xu in x_bounds:
for cl, cu in c_bounds:
m = pe.Block(concrete=True)
m.x = pe.Var(bounds=(xl, xu))
m.y = pe.Var()
m.p = pe.Param(mutable=True)
m.p.value = 1
m.c = pe.Constraint(expr=pe.inequality(body=m.x+m.y+(m.p+1), lower=cl, upper=cu))
new_bounds = fbbt(m)
self.assertEqual(new_bounds[m.x], (pe.value(m.x.lb), pe.value(m.x.ub)))
self.assertEqual(new_bounds[m.y], (pe.value(m.y.lb), pe.value(m.y.ub)))
x = np.linspace(pe.value(m.x.lb), pe.value(m.x.ub), 100)
z = np.linspace(pe.value(m.c.lower), pe.value(m.c.upper), 100)
if m.y.lb is None:
yl = -np.inf
else:
yl = m.y.lb
if m.y.ub is None:
yu = np.inf
else:
yu = m.y.ub
for _x in x:
_y = z - _x - m.p.value - 1
self.assertTrue(np.all(yl <= _y))
self.assertTrue(np.all(yu >= _y))
开发者ID:mskarha,项目名称:pyomo,代码行数:28,代码来源:test_fbbt.py
示例10: test_cos
def test_cos(self):
m = pe.Block(concrete=True)
m.x = pe.Var(bounds=(0, math.pi))
m.c = pe.Constraint(expr=pe.inequality(body=pe.cos(m.x), lower=-0.5, upper=0.5))
fbbt(m)
self.assertAlmostEqual(pe.value(m.x.lb), math.acos(0.5))
self.assertAlmostEqual(pe.value(m.x.ub), math.acos(-0.5))
m = pe.Block(concrete=True)
m.x = pe.Var()
m.c = pe.Constraint(expr=pe.inequality(body=pe.cos(m.x), lower=-0.5, upper=0.5))
fbbt(m)
self.assertEqual(m.x.lb, None)
self.assertEqual(m.x.ub, None)
开发者ID:mskarha,项目名称:pyomo,代码行数:14,代码来源:test_fbbt.py
示例11: test_unbounded_var
def test_unbounded_var(self):
"""test with unbounded variables"""
m = ConcreteModel()
m.v1 = Var(initialize=7)
m.v2 = Var(initialize=2, bounds=(2, 5))
m.v3 = Var(initialize=6, bounds=(6, 9))
m.v4 = Var(initialize=1, bounds=(1, 1))
m.c1 = Constraint(expr=m.v1 <= 2 * m.v2 + m.v3 + m.v4)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
开发者ID:Pyomo,项目名称:pyomo,代码行数:14,代码来源:test_constraint_tightener.py
示例12: test_unbounded_one_direction
def test_unbounded_one_direction(self):
"""Unbounded in one direction"""
m = ConcreteModel()
m.v1 = Var(initialize=7, bounds=(-float('inf'), 10))
m.v2 = Var(initialize=2, bounds=(2, 5))
m.v3 = Var(initialize=6, bounds=(6, 9))
m.v4 = Var(initialize=1, bounds=(1, 1))
m.c1 = Constraint(expr=m.v1 <= 2 * m.v2 + m.v3 + m.v4)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.upper), -1)
self.assertFalse(m.c1.has_lb())
开发者ID:Pyomo,项目名称:pyomo,代码行数:14,代码来源:test_constraint_tightener.py
示例13: test_nested
def test_nested(self):
m = pe.ConcreteModel()
m.x = pe.Var(initialize=2)
m.y = pe.Var(initialize=3)
m.p = pe.Param(initialize=0.5, mutable=True)
e = pe.exp(m.x**m.p + 3.2*m.y - 12)
derivs = reverse_ad(e)
symbolic = reverse_sd(e)
self.assertAlmostEqual(derivs[m.x], pe.value(symbolic[m.x]), tol+3)
self.assertAlmostEqual(derivs[m.y], pe.value(symbolic[m.y]), tol+3)
self.assertAlmostEqual(derivs[m.p], pe.value(symbolic[m.p]), tol+3)
self.assertAlmostEqual(derivs[m.x], approx_deriv(e, m.x), tol)
self.assertAlmostEqual(derivs[m.y], approx_deriv(e, m.y), tol)
self.assertAlmostEqual(derivs[m.p], approx_deriv(e, m.p), tol)
开发者ID:Pyomo,项目名称:pyomo,代码行数:14,代码来源:test_derivs.py
示例14: test_constraint_bound_tightening
def test_constraint_bound_tightening(self):
# Check for no coefficients
m = ConcreteModel()
m.v1 = Var(initialize=7, bounds=(7, 10))
m.v2 = Var(initialize=2, bounds=(2, 5))
m.v3 = Var(initialize=6, bounds=(6, 9))
m.v4 = Var(initialize=1, bounds=(1, 1))
m.c1 = Constraint(expr=m.v1 >= m.v2 + m.v3 + m.v4 + 1)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.upper), 0)
self.assertEqual(value(m.c1.lower), 0)
开发者ID:Pyomo,项目名称:pyomo,代码行数:14,代码来源:test_constraint_tightener.py
示例15: test_constraint_with_coef
def test_constraint_with_coef(self):
"""Test with coefficient on constraint."""
m = ConcreteModel()
m.v1 = Var(initialize=7, bounds=(7, 10))
m.v2 = Var(initialize=2, bounds=(2, 5))
m.v3 = Var(initialize=6, bounds=(6, 9))
m.v4 = Var(initialize=1, bounds=(1, 1))
m.c1 = Constraint(expr=m.v1 <= 2 * m.v2 + m.v3 + m.v4)
self.assertEqual(value(m.c1.upper), 0)
self.assertFalse(m.c1.has_lb())
TransformationFactory('core.tighten_constraints_from_vars').apply_to(m)
self.assertEqual(value(m.c1.upper), -1)
self.assertEqual(value(m.c1.lower), -13)
开发者ID:Pyomo,项目名称:pyomo,代码行数:14,代码来源:test_constraint_tightener.py
示例16: propagate_solution
def propagate_solution(self, augmented_model, original_model):
for avar in augmented_model.component_objects(ctype=aml.Var, descend_into=True):
cuid = aml.ComponentUID(avar)
original_v = cuid.find_component_on(original_model)
for k in avar:
original_v[k].value = aml.value(avar[k])
开发者ID:Pyomo,项目名称:pyomo,代码行数:7,代码来源:test_nlp_transformations.py
示例17: write_table_old
def write_table_old(model, *indexes, **kwargs):
# there may be a way to accept specific named keyword arguments and also an
# open-ended list of positional arguments (*indexes), but I don't know what that is.
output_file = kwargs["output_file"]
headings = kwargs["headings"]
values = kwargs["values"]
print "Writing {file} ...".format(file=output_file),
sys.stdout.flush() # display the part line to the user
start=time.time()
# create a master indexing set
# this is a list of lists, even if only one list was specified
idx = itertools.product(*indexes)
with open(output_file, 'wb') as f:
w = csv.writer(f, dialect="ampl-tab")
# write header row
w.writerow(list(headings))
# write the data
w.writerows(
tuple(value(v) for v in values(model, *x))
for x in idx
)
print "time taken: {dur:.2f}s".format(dur=time.time()-start)
开发者ID:shoibalc,项目名称:switch-hawaii-studies,代码行数:25,代码来源:util.py
示例18: test_linear
def test_linear(self):
m = ConcreteModel()
m.x = Var()
m.c = Constraint(expr=5*m.x == 10)
calculate_variable_from_constraint(m.x, m.c)
self.assertEqual(value(m.x), 2)
开发者ID:Pyomo,项目名称:pyomo,代码行数:7,代码来源:test_calc_var_value.py
示例19: test_as_good_with_iteration_rand
def test_as_good_with_iteration_rand(self):
# initialize model with data
m = build_model()
# create ipopt solver
optsolver = SolverFactory('ipopt')
optsolver.solve(m)
for i in range(10):
m2 = build_model()
SolverFactory('multistart').solve(m2, iterations=10)
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
开发者ID:Pyomo,项目名称:pyomo,代码行数:16,代码来源:test_multi.py
示例20: get_value
def get_value(obj):
"""
Retrieve value of one element of a Variable or Expression, converting
division-by-zero to nan and uninitialized values to None.
"""
try:
val = value(obj)
except ZeroDivisionError:
# diagnostic expressions sometimes have 0 denominator,
# e.g., AverageFuelCosts for unused fuels;
val = float("nan")
except ValueError:
# If variables are not used in constraints or the
# objective function, they will never get values, and
# give a ValueError at this point.
# Note: for variables this could instead use 0 if allowed, or
# otherwise the closest bound.
if getattr(obj, 'value', 0) is None:
val = None
# Pyomo will print an error before it raises the ValueError,
# but we say more here to help users figure out what's going on.
print (
"WARNING: variable {} has not been assigned a value. This "
"usually indicates a coding error: either the variable is "
"not needed or it has accidentally been omitted from all "
"constraints and the objective function.".format(obj.name)
)
else:
# Caught some other ValueError
raise
return val
开发者ID:switch-model,项目名称:switch,代码行数:31,代码来源:__init__.py
注:本文中的pyomo.environ.value函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论