Ejemplo n.º 1
0
def eval_lfunction_FValuePlusOrMinus(r, context):
    from mcdp_dp import UncertainGateSym, MinusValueDP, PlusValueDP

    from .eval_lfunction_imp import eval_lfunction
    from .eval_constant_imp import eval_constant
    from .helpers import create_operation_lf
    
    check_isinstance(r, CDP.FValuePlusOrMinus)
    median = eval_lfunction(r.median, context)
    extent = eval_constant(r.extent, context)
    
    F = context.get_ftype(median)

    # provides f = between 10 g and 20 g
    
    # MinusValueDP :   r + c ≥ f
    # PlusValueDP :   r ≥ f + c 
    
    # f <= f0 - c # pessimistic
    # f <= f0 + c # optimistic 
    dpl =  MinusValueDP(F, extent.value, extent.unit)
    dpu =  PlusValueDP(F, extent.value, extent.unit)
    
    fl = create_operation_lf(context, dpl, functions=[median])
    fu = create_operation_lf(context, dpu, functions=[median])
     
    dp = UncertainGateSym(F)
    return create_operation_lf(context, dp=dp, functions=[fl, fu])
Ejemplo n.º 2
0
def eval_rvalue_approx_u(r, context):
    assert isinstance(r, CDP.ApproxURes)

    #
    #  r1-approx-r2----------------- Uncertainty gate
    #          |____(+ step)--[r3]|

    r1 = eval_rvalue(r.rvalue, context)
    step = eval_constant(r.step, context)

    R = context.get_rtype(r1)
    tu = get_types_universe()
    try:
        tu.check_leq(step.unit, R)
    except NotLeq as e:
        msg = ('The step is specified in a unit (%s), which is not compatible '
               'with the resource (%s).' % (step.unit, R))
        raise_wrapped(DPSemanticError, e, msg, compact=True)

    stepu = step.cast_value(R)
    if stepu == 0.0:
        return r1

    dp = makeLinearFloor0DP(R, stepu)

    r2 = create_operation(context,
                          dp=dp,
                          resources=[r1],
                          name_prefix='_approx',
                          op_prefix='_toapprox',
                          res_prefix='_result')

    dpsum = PlusValueDP(R, c_value=step.value, c_space=step.unit)
    r3 = create_operation(context,
                          dp=dpsum,
                          resources=[r2],
                          name_prefix='_sum')

    dpu = UncertainGate(R)

    return create_operation(context,
                            dp=dpu,
                            resources=[r2, r3],
                            name_prefix='_uncertain')
Ejemplo n.º 3
0
def eval_rvalue_RValuePlusOrMinus(r, context):
    from mcdp_dp import UncertainGate, MinusValueDP, PlusValueDP
    from .eval_resources_imp import eval_rvalue
    from .eval_constant_imp import eval_constant

    check_isinstance(r, CDP.RValuePlusOrMinus)
    median = eval_rvalue(r.median, context)
    extent = eval_constant(r.extent, context)
    
    Rl = context.get_rtype(median)

    dpl =  MinusValueDP(Rl, extent.value, extent.unit)
    rl = create_operation(context, dpl, resources=[median])
    dpu =  PlusValueDP(Rl, extent.value, extent.unit)
    ru = create_operation(context, dpu, resources=[median])
    
    dp = UncertainGate(Rl)

    return create_operation(context, dp=dp, resources=[rl, ru])
Ejemplo n.º 4
0
def get_plus_op(context, r, c):
    """ r + constant """
    rtype = context.get_rtype(r)

    T1 = rtype
    T2 = c.unit

    def Rcomp_from_Nat(value):
        if is_top(Nat(), value):
            val = Rcomp().get_top()
        else:
            val = float(value)
        return val

    if isinstance(T1, Rcomp) and isinstance(T2, Rcomp):
        dp = PlusValueRcompDP(c.value)
    elif isinstance(T1, Rcomp) and isinstance(T2, Nat):
        # cast Nat to Rcomp
        val = Rcomp_from_Nat(c.value)
        dp = PlusValueRcompDP(val)
    elif isinstance(T1, Rcomp) and isinstance(T2, Rcomp):
        dp = PlusValueRcompDP(c.value)
    elif isinstance(T1, Nat) and isinstance(T2, Rcomp):
        # This is the case:
        #
        #   provided f + Rcomp:2.3 <= required r
        dp = PlusValueRcompDP(c.value)
    elif isinstance(T1, RcompUnits) and isinstance(T2, RcompUnits):
        dp = PlusValueDP(F=T1, c_value=c.value, c_space=T2)
    elif isinstance(T1, Nat) and isinstance(T2, Nat):
        dp = PlusValueNatDP(c.value)
    elif isinstance(T1, RcompUnits) and isinstance(T2, Nat):
        val = Rcomp_from_Nat(c.value)
        dp = PlusValueRcompDP(val)
    else:
        msg = ('Cannot create addition operation between resource of type %s'
               ' and constant of type %s.' % (T1, T2))
        raise_desc(DPInternalError, msg)  # , rtype=T1, c=c)

    r2 = create_operation(context, dp, resources=[r], name_prefix='_plus')
    return r2
Ejemplo n.º 5
0
def eval_lfunction_FValueMinusN(lf, context):
    ops = get_odd_ops(unwrap_list(lf.ops))

    # first one should be a functionality
    fvalue = eval_lfunction(ops[0], context)

    # ops after the first should be constant
    must_be_constants = ops[1:]
    from .eval_constant_imp import eval_constant

    constants = []
    for mc in must_be_constants:
        try:
            c = eval_constant(mc, context)
            constants.append(c)
        except:
            raise

    constant = plus_constantsN(constants)

    # f - constant <= (x)
    # f <= (x) + Constant

    F = context.get_ftype(fvalue)
    if isinstance(F, Nat) and isinstance(constant.unit, Nat):
        dp = PlusValueNatDP(constant.value)
    elif isinstance(F, Rcomp):
        dp = PlusValueRcompDP(constant.value)
    elif isinstance(F, RcompUnits):
        dp = PlusValueDP(F=F, c_value=constant.value, c_space=constant.unit)
    else:
        msg = 'Could not create this operation with %s. ' % F
        raise_desc(DPSemanticError, msg, F=F)

    return create_operation_lf(context,
                               dp=dp,
                               functions=[fvalue],
                               name_prefix='_minusvalue',
                               op_prefix='_op',
                               res_prefix='_result')
Ejemplo n.º 6
0
def PlusValueDP1():
    F = parse_poset('J')
    U = parse_poset('mJ')
    v = 1000.0
    return PlusValueDP(F, c_value=v, c_space=U)