Beispiel #1
0
def computeLambda(state, maximumTime=1.0):
    activeAgents = state.getActiveAgents()
    agentNames = getUniqueNames(activeAgents, prefix="Agent ")
    classNames = getUniqueNames(state.getChoiceClasses(), prefix="Class ")
    choiceNames = getUniqueNames(state.getChoices(), prefix="")

    choiceVariables = LpVariable.dicts("p", choiceNames.values(), lowBound=0)
    lambdaVariable = LpVariable("l", lowBound=0.0, upBound=maximumTime)

    def createConstraints(problem, variable):
        problem += lpSum(choiceVariables) <= 1, "Distribution"
        for choiceClass, height in state.getCurrentClassHeights().items():
            problem += createLpSum(choiceClass, choiceNames, choiceVariables) >= \
                height, classNames[choiceClass] + " height"
        for agent in state.getActiveAgents():
            problem += createLpSum(state.getCurrentAgentChoiceClass(agent),
                                   choiceNames, choiceVariables) >= \
                state.getAgentHeight(agent) + variable * state.getAgentSpeed(agent), \
                agentNames[agent] + " push"

    problem = LpProblem("Lambda", LpMaximize)
    createConstraints(problem, lambdaVariable)
    problem.setObjective(lambdaVariable)
    checkPulpStatus(problem.solve(state.getSettings().getSolver()))
    lambdaOpt = lambdaVariable.value()

    bouncingAgents = []
    stringAgents = []  #TODO just a workaround; find better solution
    for currentAgent in activeAgents:
        print "Current agent: " + agentNames[
            currentAgent]  # name is off by one
        problem = LpProblem(agentNames[currentAgent], LpMaximize)
        createConstraints(problem, lambdaOpt)
        (choiceClass, height, speed) = state.getAgentData(currentAgent)
        # print "Choice Class: " + repr(choiceClass) + ",
        print "height: " + repr(height) + " , speed: " + repr(speed)
        problem.setObjective(
            createLpSum(choiceClass, choiceNames, choiceVariables) -
            lambdaOpt * speed - height)
        checkPulpStatus(problem.solve(state.getSettings().getSolver()))
        value = problem.objective.value()
        # print "value: " + repr(value) + "\n"
        if not state.getSettings().isNonnegative(value):
            raise ValueError(
                str(value) + " negative while determining bounce of " +
                repr(currentAgent) + " from " + repr(choiceClass) + "@" +
                str(height) + "/" + str(speed))
        if state.getSettings().isClose(value, 0):
            bouncingAgents.append(currentAgent)
            stringAgents.append(int(currentAgent.getName()))
        print "climbing time: " + repr(lambdaOpt) + ", bouncingAgents: " + str(
            stringAgents) + "\n"
        # print map(Agent.getName(), bouncingAgents)
    return (lambdaOpt, bouncingAgents)
Beispiel #2
0
def computeLambda(state, maximumTime=1.0):
    activeAgents = state.getActiveAgents()
    agentNames = getUniqueNames(activeAgents, prefix="Agent ")
    classNames = getUniqueNames(state.getChoiceClasses(), prefix="Class ")
    choiceNames = getUniqueNames(state.getChoices(), prefix="")

    choiceVariables = LpVariable.dicts("p", choiceNames.values(), lowBound=0)
    lambdaVariable = LpVariable("l", lowBound=0.0, upBound=maximumTime)

    def createConstraints(problem, variable):
        problem += lpSum(choiceVariables) <= 1, "Distribution"
        for choiceClass, height in state.getCurrentClassHeights().items():
            problem += createLpSum(choiceClass, choiceNames, choiceVariables) >= \
                height, classNames[choiceClass] + " height"
        for agent in state.getActiveAgents():
            problem += createLpSum(state.getCurrentAgentChoiceClass(agent),
                                   choiceNames, choiceVariables) >= \
                state.getAgentHeight(agent) + variable * state.getAgentSpeed(agent), \
                agentNames[agent] + " push"

    problem = LpProblem("Lambda", LpMaximize)
    createConstraints(problem, lambdaVariable)
    problem.setObjective(lambdaVariable)
    checkPulpStatus(problem.solve(state.getSettings().getSolver()))
    lambdaOpt = lambdaVariable.value()

    bouncingAgents = []
    stringAgents = [] #TODO just a workaround; find better solution
    for currentAgent in activeAgents:
	print "Current agent: " + agentNames[currentAgent] # name is off by one
        problem = LpProblem(agentNames[currentAgent], LpMaximize)
        createConstraints(problem, lambdaOpt)
        (choiceClass, height, speed) = state.getAgentData(currentAgent)
	# print "Choice Class: " + repr(choiceClass) + ", 
	print "height: " + repr(height) + " , speed: " + repr(speed)
        problem.setObjective(createLpSum(choiceClass, choiceNames, choiceVariables)
                             - lambdaOpt * speed - height)
        checkPulpStatus(problem.solve(state.getSettings().getSolver()))
        value = problem.objective.value()
	# print "value: " + repr(value) + "\n"
        if not state.getSettings().isNonnegative(value):
            raise ValueError(str(value) + " negative while determining bounce of " +
                             repr(currentAgent) + " from " + repr(choiceClass) +
                             "@" + str(height) + "/" + str(speed))
        if state.getSettings().isClose(value, 0):
            bouncingAgents.append(currentAgent)
	    stringAgents.append(int(currentAgent.getName()))
	print "climbing time: " + repr(lambdaOpt) + ", bouncingAgents: " + str(stringAgents) + "\n"	
	# print map(Agent.getName(), bouncingAgents)
    return (lambdaOpt, bouncingAgents)
def computeLambda(state, maximumTime=1.0):
    '''
    @type state: SSRState
    @type maximumTime: float
    '''
    towerNames = getUniqueNames(state.getTowers(), prefix="T")
    choiceNames = getUniqueNames(state.getChoices(), prefix="")
    choiceVariables = LpVariable.dicts("p", choiceNames.values(), lowBound=0.0)
    lambdaVariable = LpVariable("l", lowBound=0.0, upBound=maximumTime)

    def createConstraints(problem, variable):
        problem += lpSum(choiceVariables) <= 1, "Distribution"
        for tower, towerName in towerNames.items():
            problem += createLpSum(tower.getChoiceClass(), choiceNames, choiceVariables) >= \
                tower.getHeight() + variable * \
                tower.getSpeed(), towerName

    problem = LpProblem("Lambda", LpMaximize)
    createConstraints(problem, lambdaVariable)
    problem.setObjective(lambdaVariable)
    checkPulpStatus(problem.solve(state.getSettings().getSolver()))
    lambdaOpt = lambdaVariable.value()

    freezingTowers = []
    for currentTower, towerName in towerNames.items():
        if currentTower.isFrozen():
            continue
        problem = LpProblem(towerName, LpMaximize)
        createConstraints(problem, lambdaOpt)
        problem.setObjective(
            createLpSum(currentTower.getChoiceClass(), choiceNames,
                        choiceVariables) -
            lambdaOpt * currentTower.getSpeed() - currentTower.getHeight())
        checkPulpStatus(problem.solve(state.getSettings().getSolver()))
        value = problem.objective.value()
        if not state.getSettings().isNonnegative(value):
            raise ValueError(
                str(value) + " negative while determining frozen state of " +
                repr(currentTower))
        if state.getSettings().isClose(problem.objective.value(), 0):
            freezingTowers.append(currentTower)

    return (lambdaOpt, frozenset(freezingTowers))
Beispiel #4
0
def computeLambda(state, maximumTime=1.0):
    '''
    @type state: SSRState
    @type maximumTime: float
    '''
    towerNames = getUniqueNames(state.getTowers(), prefix="T")
    choiceNames = getUniqueNames(state.getChoices(), prefix="")
    choiceVariables = LpVariable.dicts("p", choiceNames.values(), lowBound=0.0)
    lambdaVariable = LpVariable("l", lowBound=0.0, upBound=maximumTime)

    def createConstraints(problem, variable):
        problem += lpSum(choiceVariables) <= 1, "Distribution"
        for tower, towerName in towerNames.items():
            problem += createLpSum(tower.getChoiceClass(), choiceNames, choiceVariables) >= \
                tower.getHeight() + variable * \
                tower.getSpeed(), towerName

    problem = LpProblem("Lambda", LpMaximize)
    createConstraints(problem, lambdaVariable)
    problem.setObjective(lambdaVariable)
    checkPulpStatus(problem.solve(state.getSettings().getSolver()))
    lambdaOpt = lambdaVariable.value()

    freezingTowers = []
    for currentTower, towerName in towerNames.items():
        if currentTower.isFrozen():
            continue
        problem = LpProblem(towerName, LpMaximize)
        createConstraints(problem, lambdaOpt)
        problem.setObjective(createLpSum(currentTower.getChoiceClass(), choiceNames, choiceVariables)
                             - lambdaOpt * currentTower.getSpeed() - currentTower.getHeight())
        checkPulpStatus(problem.solve(state.getSettings().getSolver()))
        value = problem.objective.value()
        if not state.getSettings().isNonnegative(value):
            raise ValueError(str(value) + " negative while determining frozen state of " +
                             repr(currentTower))
        if state.getSettings().isClose(problem.objective.value(), 0):
            freezingTowers.append(currentTower)

    return (lambdaOpt, frozenset(freezingTowers))
Beispiel #5
0
# 使用する変数
x = LpVariable('X')
y = LpVariable('Y')

# 目的関数
problem += 30 * x + 25 * y

# 制約条件
problem += 0.5 * x + 0.25 * y <= 10  # ひき肉の総量は 3800g
problem += 0.5 * x + 0.75 * y <= 15  # 玉ねぎの総量は 2100g

# 解く
problem.solve()

# 結果表示
print('x: {x}'.format(x=x.value()))
print('y: {y}'.format(y=y.value()))
print(x.value() * y.value())

# class Node:
#     def __init__(self, x, y):
#         self.x = x
#         self.y = y
#         self.state = None

# def main():
#     x, y = map(int, input().split())
#     start = []
#     goal =[]
#     maze_node = [[Node(i, j) for j in range(y)] for i in range(x)]
#     for i in range(x):