Welcome to OptFlow 0.1.3 Alpha#

Unified Optimization with Dataflow Graphs

OptFlow is an unified optimization framework integrating multiple optimization methodologies, aiming to solve an unprecedented range of optimization problems in an efficient way. It lets you express your problem in a generic, Turing-complete dataflow graph language, rather than in the restrictive standard form required by different optimization methodologies. You can provide (almost) any computable objective that you want to maximize or minimize, including but not limited to

Optimize the actions of a player to maximize the score of a game (let’s say, CartPole)

Optimize the selection of items to maximize the total value of a knapsack without exceeding its capacity

Optimize the number in the blank box to deliver a feasible solution of 9x9 Soduku

Optimizes the transition function of a Turing machine to maximize the sum of the value on its tape

OptFlow supports auto-modeling that analyzes the characteristics of your dataflow-graph represented problem, and translates it to the following backend solving methodologies:

OptFlow also supports auto-solving that automatically accelerates the solving of your problem by matching it with suitable acceleration techniques, including:

In this way, OptFlow let you unlock the power of various optimization methodologies as a whole, and enjoy performance boost seamlessly.

OptFlow Pipeline

OptFlow pipeline compared with the traditional one.#

Examples#

The following code shows how OptFlow solves the CartPole problem simulated by Gym

import optflow as flow
from optflow.contrib.gym_op import Gym, GymStep, GymReset

gym_init = Gym('CartPole-v1')
gym_reset = GymReset()
gym_step = GymStep()

# Construct the problem with dataflow graph
env = gym_init()
env, state = gym_reset(env)
total_reward = flow.Constant(0.)
x = flow.Variable(cat=flow.categorical, size=2, shape=(flow.infinite, ))    # actions to be optimized
with flow.ForLoop(flow.infinite) as t:              # create a loop operation to run the game
    env, state, reward, done = gym_step(env, x[t])  # play one step of the game
    total_reward += reward                          # accumulate the reward
    with flow.IfCond(done):     # create a condition operation to check if the game is terminated
        flow.break_loop()                           # break the game loop

prob = flow.Problem(objective=total_reward, sense=flow.maximize)

# Train a model as reinforcement learning method is selected
prob.train(trainer=flow.rl, params={'num_episodes': 100})
# The optimal objective value is returned by `prob.solve()`.
opt_obj = prob.solve(solver=flow.rl)
# The optimal value for x is stored in `x.optimized_value`.
print(x.optimized_value)

[Run Online Demo] (password: )

and the following code shows how OptFlow solves the knapsack problem with multiple optimization methodologies

import optflow as flow
import numpy as np

# Problem data
N, max_weight = 5, 10
weight = flow.Constant(value=np.array([2, 2, 6, 5, 4]))
value = flow.Constant(value=np.array([6, 3, 5, 4, 6]))

# Construct the problem with dataflow graph
x = flow.Variable(cat=flow.binary, size=N, shape=(N, ))
obj = flow.sum([value[i] * x[i] for i in range(N)])
constraints = [flow.sum([weight[i] * x[i] for i in range(N)]) <= max_weight]

prob = flow.Problem(objective=obj, constraints=constraints, sense=flow.maximize)

# Solve the problem with multiple optimization methodologies
opt_obj_prog = prob.solve(solver=flow.programming)  # solve via mathematical programming
opt_obj_mh = prob.solve(solver=flow.metaheuristics) # solve via metaheuristics
opt_obj_search = prob.solve(solver=flow.search)     # solve via search
print(opt_obj_prog, opt_obj_search, opt_obj_mh, x.optimized_value)

[Run Online Demo] (password: )

While both the two short scripts construct the problem with dataflow graph, they involve four optimization methodologies: reinforcement learning, mathematical programming, metaheuristics and search.

More examples can be found in the Examples page.

Table of Contents#

Indices and tables#