Changeset 4155


Ignore:
Timestamp:
06/03/11 09:26:35 (3 years ago)
Author:
wehart
Message:

Updating examples to reorder rule arguments to put the model first.

Location:
coopr.pyomo/trunk/examples/pyomo
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/diet.py

    r4119 r4155  
    1919model.f_min = Param(model.FOOD, within=NonNegativeReals) 
    2020 
    21 def f_max_valid (value, j, model): 
     21def f_max_valid (model, value, j): 
    2222    return value > model.f_min[j] 
    2323model.f_max = Param(model.FOOD, validate=f_max_valid) 
     
    2525model.n_min = Param(model.NUTR, within=NonNegativeReals) 
    2626 
    27 def paramn_max(value, i, model): 
     27def paramn_max(model, value, i): 
    2828    return value > model.n_min[i] 
    2929model.n_max = Param(model.NUTR, validate=paramn_max) 
     
    3131model.amt = Param(model.NUTR, model.FOOD, within=NonNegativeReals) 
    3232 
    33 def Buy_bounds(i,model): 
     33def Buy_bounds(model, i): 
    3434    return (model.f_min[i],model.f_max[i]) 
    3535model.Buy = Var(model.FOOD, bounds=Buy_bounds) 
     
    3939model.totalcost = Objective(rule=Objective_rule) 
    4040 
    41 def Diet_rule(i, model): 
     41def Diet_rule(model, i): 
    4242    expr = 0 
    4343    for j in model.FOOD: 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/dieti.py

    r4119 r4155  
    1919model.f_min = Param(model.FOOD, within=NonNegativeReals) 
    2020 
    21 def f_max_valid (value, j, model): 
     21def f_max_valid (model, value, j): 
    2222    return value > model.f_min[j] 
    2323model.f_max = Param(model.FOOD, validate=f_max_valid) 
     
    2525model.n_min = Param(model.NUTR, within=NonNegativeReals) 
    2626 
    27 def paramn_max (value, i, model): 
     27def paramn_max (model, value, i): 
    2828    return value > model.n_min[i] 
    2929model.n_max = Param(model.NUTR, validate=paramn_max) 
     
    3131model.amt = Param(model.NUTR, model.FOOD, within=NonNegativeReals) 
    3232 
    33 def Buy_bounds(i,model): 
     33def Buy_bounds(model,i): 
    3434    return (model.f_min[i],model.f_max[i]) 
    3535model.Buy = Var(model.FOOD, bounds=Buy_bounds, domain=Integers) 
     
    3939model.totalcost = Objective(rule=Objective_rule) 
    4040 
    41 def Diet_rule(i, model): 
     41def Diet_rule(model, i): 
    4242    expr = 0 
    4343    for j in model.FOOD: 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/econ2min.py

    r4119 r4155  
    3131# *********************************** 
    3232 
    33 def Level_bounds(i, model): 
     33def Level_bounds(model, i): 
    3434    return (model.level_min[i], model.level_max[i]) 
    3535model.Level = Var(model.ACT, bounds=Level_bounds, doc='level for each activity') 
     
    4141model.Total_Cost = Objective(rule=Total_Cost_rule, doc='minimize total cost') 
    4242 
    43 def Demand_rule(i, model): 
     43def Demand_rule(model, i): 
    4444    expr = 0 
    4545    for j in model.ACT: 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/econmin.py

    r4119 r4155  
    3535model.Total_Cost = Objective(rule=Total_Cost_rule) 
    3636 
    37 def Demand_rule(i, model): 
     37def Demand_rule(model, i): 
    3838    expr = 0 
    3939    for j in model.ACT: 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/prod.py

    r3446 r4155  
    2828 
    2929# Limit Constraint 
    30 def Limit_rule(j, model): 
     30def Limit_rule(model, j): 
    3131    return (0, model.X[j], model.u[j]) 
    3232model.Limit = Constraint(model.P, rule=Limit_rule) 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/steel.py

    r4119 r4155  
    2020model.market = Param(model.PROD, within=NonNegativeReals) 
    2121 
    22 def Make_bounds(i,model): 
     22def Make_bounds(model,i): 
    2323    return (0,model.market[i]) 
    2424model.Make = Var(model.PROD, bounds=Make_bounds) 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/steel3.py

    r4119 r4155  
    2323model.market = Param(model.PROD, within=NonNegativeReals) 
    2424 
    25 def Make_bounds(i,model): 
     25def Make_bounds(model, i): 
    2626    return (model.commit[i],model.market[i]) 
    2727model.Make = Var(model.PROD, bounds=Make_bounds) 
  • coopr.pyomo/trunk/examples/pyomo/amplbook2/steel4.py

    r4119 r4155  
    2424model.market = Param(model.PROD, within=NonNegativeReals) 
    2525 
    26 def Make_bounds(i,model): 
     26def Make_bounds(model,i): 
    2727    return (model.commit[i],model.market[i]) 
    2828model.Make = Var(model.PROD, bounds=Make_bounds) 
     
    3232model.Total_Profit = Objective(rule=Objective_rule, sense=maximize) 
    3333 
    34 def Timelim_rule(s, model): 
     34def Timelim_rule(model, s): 
    3535    timeexpr = 0 
    3636    for p in model.PROD: 
  • coopr.pyomo/trunk/examples/pyomo/benders/master.py

    r4119 r4155  
    4545model.revenue = Param(model.PROD, model.WEEKS, model.SCEN, within=NonNegativeReals) 
    4646 
    47 def prob_validator(value, s, model): 
     47def prob_validator(model, value, s): 
    4848    return (value >= 0) and (value <= 1.0) 
    4949model.prob = Param(model.SCEN, validate=prob_validator) 
     
    6161model.Inv1.declare_attribute("urc") 
    6262 
    63 def sell_bounds(p, model): 
     63def sell_bounds(model, p): 
    6464    return (0, model.market[p, 1]) 
    6565model.Sell1 = Var(model.PROD, within=NonNegativeReals, bounds=sell_bounds) 
     
    7777model.CUTS = Set(within=PositiveIntegers, ordered=True) 
    7878 
    79 def price_validator(value, model): 
     79def price_validator(model, value): 
    8080    return value >= -0.000001 
    8181 
     
    9494model.Time1 = Constraint(rule=time1_rule) 
    9595 
    96 def balance1_rule(p, model): 
     96def balance1_rule(model, p): 
    9797    return  model.Make1[p] + model.inv0[p] - (model.Sell1[p] + model.Inv1[p]) == 0.0 
    9898model.Balance1 = Constraint(model.PROD, rule=balance1_rule) 
  • coopr.pyomo/trunk/examples/pyomo/benders/subproblem.py

    r4119 r4155  
    4545 
    4646# scenario probability 
    47 def unit_interval_validate(value, model): 
     47def unit_interval_validate(model, value): 
    4848    return (value >= 0.0) and (value <= 1.0) 
    4949model.prob = Param(validate=unit_interval_validate) 
     
    6565 
    6666# tons sold 
    67 def sell_bounds(p, t, model): 
     67def sell_bounds(model, p, t): 
    6868    return (0, model.market[p, t]) 
    6969model.Sell = Var(model.PROD, model.TWOPLUSWEEKS, within=NonNegativeReals, bounds=sell_bounds) 
     
    7272model.Sell.declare_attribute("urc") 
    7373 
    74 def time_rule(t, model): 
     74def time_rule(model, t): 
    7575    return sum([(1.0 / model.rate[p]) * model.Make[p, t] for p in model.PROD]) - model.avail[t] <= 0.0 
    7676model.Time = Constraint(model.TWOPLUSWEEKS, rule=time_rule) 
    7777 
    78 def balance2_rule(p, model): 
     78def balance2_rule(model, p): 
    7979    return (model.Make[p, 2] + model.inv1[p]) - (model.Sell[p, 2] + model.Inv[p, 2]) == 0.0 
    8080model.Balance2 = Constraint(model.PROD, rule=balance2_rule) 
    8181 
    82 def balance_rule(p, t, model): 
     82def balance_rule(model, p, t): 
    8383    return (model.Make[p, t] + model.Inv[p, t-1]) - (model.Sell[p, t] + model.Inv[p, t]) == 0.0 
    8484model.Balance = Constraint(model.PROD, model.THREEPLUSWEEKS, rule=balance_rule) 
  • coopr.pyomo/trunk/examples/pyomo/diet/diet1.py

    r4119 r4155  
    2424model.f_min = Param(model.FOOD, within=NonNegativeReals, default=0.0) 
    2525 
    26 def f_max_validate (value, j, model): 
     26def f_max_validate (model, value, j): 
    2727    return model.f_max[j] > model.f_min[j] 
    2828model.f_max = Param(model.FOOD, validate=f_max_validate, 
     
    3131model.n_min = Param(model.NUTR, within=NonNegativeReals, default=0.0) 
    3232 
    33 def n_max_validate (value, j, model): 
     33def n_max_validate (model, value, j): 
    3434    return value > model.n_min[j] 
    3535model.n_max = Param(model.NUTR, validate=n_max_validate, 
     
    4040# -------------------------------------------------------- 
    4141 
    42 def Buy_bounds(i,model): 
     42def Buy_bounds(model, i): 
    4343    return (model.f_min[i],model.f_max[i]) 
    4444model.Buy = Var(model.FOOD, bounds=Buy_bounds) 
     
    5353model.Total_Cost = Objective(rule=Total_Cost_rule) 
    5454 
    55 def Nutr_Amt_rule(i, model): 
     55def Nutr_Amt_rule(model, i): 
    5656    ans = 0 
    5757    for j in model.FOOD: 
     
    6262# -------------------------------------------------------- 
    6363 
    64 def Diet_rule(i, model): 
     64def Diet_rule(model, i): 
    6565    expr = 0 
    6666    for j in model.FOOD: 
  • coopr.pyomo/trunk/examples/pyomo/draft/api.py

    r4119 r4155  
    7272#  This option allows for more sophisticated construction rules 
    7373# 
    74 def f(i,model): 
     74def f(model, i): 
    7575    if i==10: 
    76         return None 
     76        return Set.End 
    7777    if i==0: 
    7878        return 1 
     
    8787#  Option 'validate' specifies a function that is used to validate set elements 
    8888# 
    89 def f(value,model): 
     89def f(model, value): 
    9090    return value in model.A 
    9191model.B = Set(validate=f) 
     
    214214#       parameter 
    215215# 
    216 def f(i,model): 
     216def f(model, i): 
    217217    return 3*i 
    218218model.Z = Param(model.A,initialize=f) 
     
    232232#  Option 'validate' specifies a function that is used to validate parameters 
    233233# 
    234 def f(value,model): 
     234def f(model, value): 
    235235    return value in model.A 
    236236model.Z = Param(validate=f) 
     
    308308#   variable values 
    309309# 
    310 def f(i,model): 
     310def f(model, i): 
    311311    return 3*i 
    312312model.x = Var(model.A,initialize=f) 
     
    321321# 
    322322model.x = Var(bounds=(0.0,1.0)) 
    323 def f(i,model): 
     323def f(model, i): 
    324324    return (model.x_low[i], model._x_high[i]) 
    325325model.x = Var(bounds=f) 
     
    423423model.Z = Param(model.A) 
    424424model.x = Var(model.A) 
    425 def f(i,model): 
     425def f(model, i): 
    426426    return model.Z[i] * model.A[i] 
    427427model.obj = Objective(model.A, rule=f) 
     
    499499model.Z = Param(model.A) 
    500500model.x = Var(model.A) 
    501 def f(i,model): 
     501def f(model, i): 
    502502    expr = model.Z[i] * model.A[i] 
    503503    return (0, expr, 1) 
     
    510510# An equality constraint can be specified as follows: 
    511511# 
    512 def f(i,model): 
     512def f(model, i): 
    513513    expr = model.Z[i] * model.A[i] 
    514514    return (expr, 0) 
     
    517517# bound information.  For example, the previous rule can be rewritten as 
    518518# 
    519 def f(i,model): 
     519def f(model, i): 
    520520    expr = model.Z[i] * model.A[i] 
    521521    expr = expr >= 0 
     
    533533 
    534534# 
    535 # If the constructor rule returns None, then the constraint index 
     535# If the constructor rule returns Constraint.Skip, then the constraint index 
    536536# is ignored.  Alternatively, a constructor rule can return a dictionary  
    537537# whose keys define the valid constraint indices.  Thus, the following two 
    538538# constraints are equivalent: 
    539539# 
    540 def f1(i,model): 
     540def f1(model, i): 
    541541    if i % 2 == 0: 
    542         return None 
     542        return Constraint.Skip 
    543543    expr = model.Z[i] * model.A[i] 
    544544    return (0, expr, 1) 
  • coopr.pyomo/trunk/examples/pyomo/draft/bpack.py

    r4119 r4155  
    5757# Constraint bletch 
    5858# 
    59 def bletch_rule(i,model): 
     59def bletch_rule(model, i): 
    6060    ans = 0 
    6161    j = i 
  • coopr.pyomo/trunk/examples/pyomo/draft/diet2.py

    r4119 r4155  
    1919model.f_min = Param(model.FOOD, within=NonNegativeReals) 
    2020 
    21 def f_max_valid (value, j, model): 
     21def f_max_valid (model, value, j): 
    2222    return model.f_max[j] > model.f_min[j] 
    2323model.f_max = Param(model.FOOD, validate=f_max_valid) 
     
    2525model.n_min = Param(model.NUTR, within=NonNegativeReals) 
    2626 
    27 def paramn_max (i, model): 
     27def paramn_max (model, i): 
    2828    model.n_max[i] > model.n_min[i] 
    2929    return model.n_max[i] 
     
    3434model.amt = Param(model.NUTR, model.FOOD, within=NonNegativeReals) 
    3535 
    36 def Buy_bounds(i,model): 
     36def Buy_bounds(model, i): 
    3737    return (model.f_min[i],model.f_max[i]) 
    3838model.Buy = Var(model.FOOD, bounds=Buy_bounds) 
     
    4747model.totalcost = Objective(rule=Objective_rule) 
    4848 
    49 def Diet_rule(i, model): 
     49def Diet_rule(model, i): 
    5050    expr = 0 
    5151    for j in model.FOOD: 
  • coopr.pyomo/trunk/examples/pyomo/p-median/pmedian.py

    r4119 r4155  
    2727model.obj = Objective(rule=rule) 
    2828 
    29 def rule(m, model): 
     29def rule(model, m): 
    3030    return (sum( (model.x[n,m] for n in model.Locations)), 1.0) 
    3131model.single_x = Constraint(model.Customers, rule=rule) 
    3232 
    33 def rule(n,m,model): 
     33def rule(model, n,m): 
    3434    return (None, model.x[n,m] - model.y[n], 0.0) 
    3535model.bound_y = Constraint(model.Locations, model.Customers, rule=rule) 
  • coopr.pyomo/trunk/examples/pyomo/piecewise/example6.py

    r4119 r4155  
    33from coopr.pyomo import * 
    44 
    5 def compute_breakpoints(i, m): 
     5def compute_breakpoints(m, i): 
    66    return [0] 
    77 
    8 def compute_slopes(i, m): 
     8def compute_slopes(m, i): 
    99    return [-1, 1] 
    1010 
  • coopr.pyomo/trunk/examples/pyomo/quadratic/example2.py

    r4119 r4155  
    1212model.x = Var(model.indices, within=Reals) 
    1313 
    14 def bound_x_rule(i, model): 
     14def bound_x_rule(model, i): 
    1515    return (-10, model.x[i], 10) 
    1616model.bound_x = Constraint(model.indices, rule=bound_x_rule) 
  • coopr.pyomo/trunk/examples/pyomo/quadratic/example3.py

    r4119 r4155  
    1313model.x = Var(model.indices, within=Reals) 
    1414 
    15 def bound_x_rule(i, model): 
     15def bound_x_rule(model, i): 
    1616    return (-10, model.x[i], 10) 
    1717model.bound_x = Constraint(model.indices, rule=bound_x_rule) 
  • coopr.pyomo/trunk/examples/pyomo/radertext/Ex2_1.py

    r4119 r4155  
    3030 
    3131# Constraints 
    32 def EnsureMachineLimit(m, M): 
     32def EnsureMachineLimit(M, m): 
    3333    return sum (M.NumDoors[d]*M.Labor[d,m] for d in M.DoorType) \ 
    3434           <= M.MachineLimit[m] 
  • coopr.pyomo/trunk/examples/pyomo/radertext/Ex2_2.py

    r4119 r4155  
    2626 
    2727# Constraints 
    28 def EnsureWorkforce(i, M): 
     28def EnsureWorkforce(M, i): 
    2929    if i != M.NumTimePeriods.value: 
    3030        return M.NumWorkers[i] + M.NumWorkers[i+1] >= M.RequiredWorkers[i+1] 
  • coopr.pyomo/trunk/examples/pyomo/radertext/Ex2_3.py

    r4119 r4155  
    4242# Constraints 
    4343 
    44 def BalanceCrude(i,M): 
     44def BalanceCrude(M,i): 
    4545    return sum (M.x[i,j] for j in M.GasType) == M.q[i] 
    4646model.BalanceCrudeProduction = Constraint(model.CrudeType, rule=BalanceCrude) 
    4747 
    48 def BalanceGas(j,M): 
     48def BalanceGas(M,j): 
    4949    return sum (M.x[i,j] for i in M.CrudeType) == M.z[j] 
    5050model.BalanceGasProduction = Constraint(model.GasType, rule=BalanceGas) 
    5151 
    52 def EnsureCrudeLimit(i,M): 
     52def EnsureCrudeLimit(M,i): 
    5353    return M.q[i] <= M.CrudeMax[i] 
    5454model.LimitCrude = Constraint(model.CrudeType, rule=EnsureCrudeLimit) 
    5555 
    56 def EnsureGasDemand(j,M): 
     56def EnsureGasDemand(M,j): 
    5757    return M.z[j] >= M.GasDemand[j] 
    5858model.DemandGas = Constraint(model.GasType, rule=EnsureGasDemand) 
    5959 
    60 def EnsureOctane(j,M): 
     60def EnsureOctane(M,j): 
    6161    return sum (M.x[i,j]*M.CrudeOctane[i] for i in M.CrudeType) \ 
    6262            >= M.MinGasOctane[j]*M.z[j] 
    6363model.OctaneLimit = Constraint(model.GasType, rule=EnsureOctane) 
    6464 
    65 def EnsureLowMixture(i,j,M): 
     65def EnsureLowMixture(M.i,j): 
    6666    return sum (M.x[k,j] for k in M.CrudeType)*M.MixtureLowBounds[i,j] \ 
    6767           <= M.x[i,j] 
     
    6969                             rule=EnsureLowMixture) 
    7070 
    71 def EnsureUpMixture(i,j,M): 
     71def EnsureUpMixture(M,i,j): 
    7272    return sum (M.x[k,j] for k in M.CrudeType)*M.MixtureUpBounds[i,j] \ 
    7373           >= M.x[i,j] 
  • coopr.pyomo/trunk/examples/pyomo/radertext/Ex2_5.py

    r4119 r4155  
    3737 
    3838# Constraints 
    39 def EnsureBalance(e,t,M): 
     39def EnsureBalance(M,e,t): 
    4040    if t != 1: 
    4141        return M.Inventory[e,t] == M.Inventory[e, t-1] + M.Produce[e,t] \ 
     
    4646model.InventoryBalance = Constraint(model.EngineType, model.Month, rule=EnsureBalance) 
    4747 
    48 def EnsureLaborLimit(t,M): 
     48def EnsureLaborLimit(M,t): 
    4949    return sum(M.Produce[e,t]*M.Labor[e] for e in M.EngineType) <= M.LaborBound 
    5050model.LimitLabor = Constraint(model.Month, rule=EnsureLaborLimit) 
    5151 
    52 def EnsureProdLimit(t,M): 
     52def EnsureProdLimit(M,t): 
    5353    return sum(M.Produce[e,t] for e in M.EngineType) <= M.ProdBound 
    5454model.ProdLimit = Constraint(model.Month, rule=EnsureProdLimit) 
    5555 
    56 def LeaveEnough(e,t,M): 
     56def LeaveEnough(M,e,t): 
    5757    if t == len(M.Month): 
    5858        return M.Inventory[e,t] >= M.FinInv[e] 
  • coopr.pyomo/trunk/examples/pyomo/radertext/Ex2_6b.py

    r4119 r4155  
    3131# Constraints 
    3232 
    33 def xEnsureUp(s,M): 
     33def xEnsureUp(M,s): 
    3434    return M.xCentralSensor - M.xPos[s] <= M.xMax[s] 
    3535model.xUpBound = Constraint(model.Sensor, rule=xEnsureUp) 
    3636 
    37 def xEnsureLow(s,M): 
     37def xEnsureLow(M,s): 
    3838    return M.xCentralSensor - M.xPos[s] >= -M.xMax[s] 
    3939model.xLowBound = Constraint(model.Sensor, rule=xEnsureLow) 
    4040 
    41 def yEnsureUp(s,M): 
     41def yEnsureUp(M,s): 
    4242    return M.yCentralSensor - M.yPos[s] <= M.yMax[s] 
    4343model.yUpBound = Constraint(model.Sensor, rule=yEnsureUp) 
    4444 
    45 def yEnsureLow(s,M): 
     45def yEnsureLow(M,s): 
    4646    return M.yCentralSensor - M.yPos[s] >= -M.yMax[s] 
    4747model.yLowBound = Constraint(model.Sensor, rule=yEnsureLow) 
    4848 
    49 def EnsureSensorBound(s,M): 
     49def EnsureSensorBound(M,s): 
    5050    return M.xMax[s] + M.yMax[s] <= M.Max 
    5151model.MaxDist = Constraint(model.Sensor, rule=EnsureSensorBound) 
  • coopr.pyomo/trunk/examples/pyomo/sos/basic_sos2_example.py

    r4119 r4155  
    1313# model is tied together down below. 
    1414 
    15 def c_param_init ( v, model ): 
     15def c_param_init ( model, v ): 
    1616    return (-1, -1, -3, -2, -2)[ v -1 ]  # -1 because Python is 0-based 
    1717 
    1818 
    19 def b_param_init ( c, model ): 
     19def b_param_init ( model, c): 
    2020    return (30, 30)[ c -1 ]  # -1 because Python is 0-based 
    2121 
    2222 
    23 def A_param_init ( c, v, model ): 
     23def A_param_init ( model, c, v): 
    2424    data = ( 
    2525      (-1, -1,  1,  1,  0), 
     
    3939 
    4040 
    41 def constraint_rule ( c, model ): 
     41def constraint_rule ( model, c): 
    4242    constraint_equation = ( 
    4343      model.b[ c ] >= sum( 
Note: See TracChangeset for help on using the changeset viewer.