def runtrial(n, asks, bids, y, V, p, iscontrol, gensize=8):
    """
    Main execution, running a single experiment.

    @param n {int} The number of generations to run.
    @param asks {pandas.DataFrame} The historical ask prices.
    @param bids {pandas.DataFrame} The historical bid prices.
    @param y {dict or pandas.Series} The current portfolio composition.
    @param V {number} The portfolio value.
    @param p {number} The percentage of V that should be kept as cash (upper
        bound), where no more than 2p is kept in cash.
    @param iscontrol {boolean} True if the transaction costs constraint should
        be ingnored.
    @param gensize {int, default=8} The maximum generation size as well as the
        size of the starting generation.
    """
    returns, rbar = get_returns(bids)

    variables = list(asks.axes[1])
    definition_inner = {
        'lb': 0,
        'ub': 10000,
        'category': 'discreet'
    }
    definition = {key: definition_inner for key in variables}
    objective = [
        lambda x: (expected_returns(x, variables, y, V, rbar, bids.ix[-1]) -
                   expected_risk(x, variables, y, V, returns, rbar,
                                 bids.ix[-1])
                   ) * -1
    ]
    if not iscontrol:
        objective.append(lambda x: expected_transaction(x, variables,
                                                        asks.ix[-1],
                                                        bids.ix[-1]))
    ineq = [
        lambda x: constraint(x, variables, y, V, asks.ix[-1], bids.ix[-1], p),
        lambda x: -constraint(x, variables, y, V, asks.ix[-1], bids.ix[-1],
                              2*p),

    ]
    j = 0
    for symb in variables:
        ineq.append(lambda x: -(x[j] + y.get(symb, 0)))
        j += 1

    starting_gen = []
    for i in range(gensize - 2):
        curr_chromosome = []
        for j in range(len(variables)):
            curr_chromosome.append(random.randint(0, 2000))
        starting_gen.append(curr_chromosome)
    chr0 = []
    chr100 = []
    for i in range(len(variables)):
        chr0.append(0)
        chr100.append(100)
    starting_gen.append(chr0)
    starting_gen.append(chr100)

    problem = ga(name='Test', variables=variables, definition=definition,
                 objective=objective, starting_gen=starting_gen,
                 max_gen_size=gensize, total_generations=n, ineq=ineq,
                 beta=2, mutation_prob=0.15, crossover_prob=0.75)
    # print problem
    problem.solve()
    # print problem.current_generation_str()
    # print problem.solution_str()

    x = problem.solution['chromosome']
    # print x

    Er = expected_returns(x, variables, y, V, rbar, bids.ix[-1])
    # print 'Expected Returns = %.5f (should be near 0.00007)' % Er
    Ersk = expected_risk(x, variables, y, V, returns, rbar, bids.ix[-1])
    # print 'Expected Risk = %.5f (should be near 0.00126)' % Ersk
    g = constraint(x, variables, y, V, asks.ix[-1], bids.ix[-1], p)
    return pd.Series(map_x_to_definition(x, definition)), problem, Er, Ersk, g
            'lb': -100,
            'ub': 100,
            'category': 'discreet'
        },
        'x2': {
            'lb': -100,
            'ub': 100,
            'category': 'discreet'
        }
    }
    objective = [
        lambda x: 10*x[0] - x[1],
        lambda x: (1 + x[1]) / x[0],
    ]
    starting_gen = [[1, 1],
                    [1, 8],
                    [7, 55],
                    [1, 0],
                    [3, 17],
                    [2, 11]]
    problem = ga(name='Example 5-7', variables=variables,
                 definition=definition, objective=objective,
                 starting_gen=starting_gen, trim_first=False,
                 total_generations=10000)
    print problem
    print problem.current_generation_str(True)

    problem.solve()
    print problem.current_generation_str()
    print problem.solution_str()
Exemplo n.º 3
0
    ]
    eq = []
    starting_gen = [
        [0.2833, 0.1408],
        [0.0248, 0.0316],
        [0.1384, 0.4092],
        [0.3229, 0.1386],
        [0.0481, 0.1625],
        [0.4921, 0.2845],
    ]
    problem = ga(
        name="Two-bar Truss",
        variables=variables,
        definition=definition,
        objective=objective,
        ineq=ineq,
        starting_gen=starting_gen,
        trim_first=False,
        total_generations=10,
        max_gen_size=6,
    )
    print problem
    print problem.current_generation_str(True)

    selection_rnd = [
        0.5292,
        0.0436,
        0.2949,
        0.0411,
        0.9116,
        0.7869,