Beispiel #1
0
def test_max_ratio_2():

    # m0 = larch.Model.Example(1)
    m1 = larch.Model.Example(1)

    # m0.utility_ca = P.tottime * (X.tottime * 2) + P.tottime * X.totcost
    # m0.remove_unused_parameters()

    # m0.set_value(P.tottime, maximum=0)
    m1.set_value(P.totcost, maximum=0)

    c1 = RatioBound(P.tottime,
                    P.totcost,
                    min_ratio=0.75,
                    max_ratio=2.0,
                    scale=1)

    # m0.load_data()
    m1.load_data()

    m1.constraints = [
        c1,
    ]

    # r0 = m0.maximize_loglike(
    # 	method='slsqp',
    # )
    r1 = m1.maximize_loglike(method='slsqp', )

    assert m1['tottime'].value / m1['totcost'].value == approx(2.0, rel=1e-3)

    assert r1['loglike'] == approx(-3730.8942681224476)

    assert 'Optimization terminated successfully' in r1.message
    assert c1.fun(m1.pf.value) == approx(0, abs=1e-5)

    # m0.calculate_parameter_covariance()
    m1.calculate_parameter_covariance()

    # assert m0.pf.loc['tottime', 'std_err'] == approx(0.000248, rel=5e-3)
    assert m1.pf.loc['tottime', 'std_err'] == approx(0.000248 * 2, rel=5e-3)
    assert m1.pf.loc['totcost', 'std_err'] == approx(0.000248, rel=5e-3)
Beispiel #2
0
def test_constrained_optimization():
    from larch.numba import example
    m = example(1)
    from larch.model.constraints import RatioBound, OrderingBound
    m.set_value("totcost", -0.001, maximum=0)
    m.set_value("tottime", maximum=0)
    m.constraints = [
        RatioBound(P("totcost"),
                   P("tottime"),
                   min_ratio=0.1,
                   max_ratio=1.0,
                   scale=1),
        OrderingBound(P("ASC_WALK"), P("ASC_BIKE")),
    ]
    m.load_data()
    r = m.maximize_loglike(method='slsqp')
    assert r.loglike == approx(-3647.76149525901)
    x = {
        'ASC_BIKE': -0.8087472748965431,
        'ASC_SR2': -2.193449976582375,
        'ASC_SR3P': -3.744188833076006,
        'ASC_TRAN': -0.7603092451373663,
        'ASC_WALK': -0.8087472751682576,
        'hhinc#2': -0.0021699330391421407,
        'hhinc#3': 0.0003696763687090173,
        'hhinc#4': -0.00509836274463602,
        'hhinc#5': -0.0431749907425252,
        'hhinc#6': -0.002373556571769923,
        'totcost': -0.004910169034222911,
        'tottime': -0.04790588175791953,
    }
    assert dict(r.x) == approx(x)
    assert r.iteration_number == 48

    m.set_values("null")
    m.set_value("totcost", -0.001, maximum=0)
    r2 = m.maximize_loglike(method='slsqp', bhhh_start=3)
    assert r2.iteration_number == 24
    assert r2.loglike == approx(-3647.76149525901)
    assert dict(r2.x) == approx(x, rel=1e-2)
Beispiel #3
0
def test_contraint_interpretation():

	assert interpret_contraint("aaaa > bbbb") == OrderingBound('bbbb', 'aaaa')
	assert interpret_contraint("aaaa < bbbb") == OrderingBound('aaaa', 'bbbb')
	assert interpret_contraint("aaaa/bbbb > 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa/bbbb < 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("aaaa / bbbb > 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa / bbbb < 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("aaaa / bbbb >= 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa / bbbb <= 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("3.1 < aaaa / bbbb <= 3.2") == RatioBound('aaaa', 'bbbb', min_ratio=3.1, max_ratio=3.2)
	assert interpret_contraint("aaaa > 3") == FixedBound('aaaa', minimum=3)
	assert interpret_contraint("aaaa < 3") == FixedBound('aaaa', maximum=3)
	assert interpret_contraint("3.1 < aaaa < 3.5") == FixedBound('aaaa', minimum=3.1, maximum=3.5)

	assert interpret_contraint("aaaa/bbbb > 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa/bbbb < 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb > 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb < 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb >= 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb <= 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("1/4 < aaaa / bbbb <= 3/4") == RatioBound('aaaa', 'bbbb', min_ratio=1/4, max_ratio=3/4)
	assert interpret_contraint("aaaa > 3/4") == FixedBound('aaaa', minimum=3/4)
	assert interpret_contraint("aaaa < 3/4") == FixedBound('aaaa', maximum=3/4)
	assert interpret_contraint("1/4 < aaaa < 3/4") == FixedBound('aaaa', minimum=1/4, maximum=3/4)