예제 #1
0
 def generate_objective(self, tokens, result_value_name):
     obj_expr, _ = generate_objective_and_constraint_expr(
         columns=self.data_frame.columns,
         objective=tokens,
         constraints=None,
         variables=self.variables,
         result_value_name=result_value_name,
         variable_str="model.x",
         data_str="DATA_FRAME")
     return obj_expr
예제 #2
0
 def generate_constraints(self, constraint, result_value_name):
     _, c_expr = generate_objective_and_constraint_expr(
         columns=self.data_frame.columns,
         objective=None,
         constraints=[constraint],
         variables=self.variables,
         result_value_name=result_value_name,
         variable_str="model.x",
         data_str="DATA_FRAME")
     assert len(c_expr) == 1, "invalid constraint expression"
     return c_expr[0]
예제 #3
0
파일: local.py 프로젝트: zlb1028/sqlflow
def generate_model_with_data_frame(data_frame, variables, variable_type,
                                   result_value_name, objective, direction,
                                   constraints):
    """
    Generate a Pyomo ConcreteModel.

    Args:
        data_frame (pandas.DataFrame): the input table data.
        variables (list[str]): the variable names to be optimized.
        variable_type (str): the variable type.
        result_value_name (str): the result value name to be optimized.
        objective (list[str]): the objective string token list.
        direction (str): "maximize" or "minimize".
        constraints (dict): the constraint expression containing the token list
            and GROUP BY column name.

    Returns:
        A Pyomo ConcreteModel.
    """
    direction = direction.lower()
    if direction == 'maximize':
        direction = pyomo_env.maximize
    elif direction == 'minimize':
        direction = pyomo_env.minimize
    else:
        raise ValueError("direction must be one of 'maximize' or 'minimize'")

    if not hasattr(pyomo_env, variable_type):
        raise ValueError("cannot find variable type %s" % variable_type)

    variable_type = getattr(pyomo_env, variable_type)

    model = pyomo_env.ConcreteModel()
    var_num = len(data_frame)
    model.x = pyomo_env.Var(list(range(var_num)), within=variable_type)

    columns = data_frame.columns

    variable_str = "model.x"
    data_str = "DATA_FRAME"

    obj_expr, c_exprs = generate_objective_and_constraint_expr(
        columns=columns,
        objective=objective,
        constraints=constraints,
        variables=variables,
        result_value_name=result_value_name,
        variable_str=variable_str,
        data_str=data_str)

    DATA_FRAME_LOCK.acquire()
    try:
        global DATA_FRAME
        DATA_FRAME = data_frame
        obj_func = eval("lambda model: %s" % obj_expr)
        model.objective = pyomo_env.Objective(rule=obj_func, sense=direction)

        for i, (expr, for_range, iter_vars) in enumerate(c_exprs):
            attr_name = "constraint_%d" % i

            if for_range:
                assert iter_vars, "for_range and iter_vars must be " \
                                  "both non-empty"
                setattr(model, attr_name, pyomo_env.ConstraintList())
                constraint_list = getattr(model, attr_name)
                template = "lambda model, constraint_list: [constraint_list.add(%s) for %s in %s]"  # noqa: E501
                add_constraint_str = template % (expr, ",".join(iter_vars),
                                                 for_range)
                eval(add_constraint_str)(model, constraint_list)
            else:
                assert not iter_vars, \
                    "for_range and iter_vars must be both empty"
                func = eval('lambda model: %s' % expr)
                constraint = pyomo_env.Constraint(rule=func)
                setattr(model, attr_name, constraint)
    finally:
        DATA_FRAME = None
        DATA_FRAME_LOCK.release()
    return model
예제 #4
0
def run_optimize_on_optflow(train_table, columns, variables, variable_type,
                            result_value_name, objective, direction,
                            constraints, solver, result_table, user_id):
    """
    Run the optimize case in the local mode.

    Args:
        train_table (str): the source table name.
        columns (list[str]): the column names of the source table.
        variables (list[str]): the variable names to be optimized.
        variable_type (str): the variable type.
        result_value_name (str): the result value name to be optimized.
        objective (list[str]): the objective string token list.
        direction (str): "maximize" or "minimize".
        constraints (dict): the constraint expression containing the token list
            and GROUP BY column name.
        solver (str): the solver used to solve the model.
        result_table (str): the table name to save the solved results.
        user_id (str): the user id.

    Returns:
        None
    """

    if direction.lower() == "maximize":
        direction = "max"
    elif direction.lower() == "minimize":
        direction = "min"
    else:
        raise ValueError("direction must be maximize or minimize")

    if len(variables) == 2:
        obj_expr = generate_optflow_fsl_expr_when_two_vars(
            columns=columns,
            tokens=objective,
            variables=variables,
            result_value_name=result_value_name)
        constraint_expressions = []
        for c in constraints:
            tokens = c.get("tokens")
            group_by = c.get("group_by")
            c_expr = generate_optflow_fsl_expr_when_two_vars(
                columns=columns,
                tokens=tokens,
                variables=variables,
                result_value_name=result_value_name,
                group_by=group_by)
            constraint_expressions.append(c_expr)
    else:
        obj_expr, c_exprs = generate_objective_and_constraint_expr(
            columns=columns,
            objective=objective,
            constraints=constraints,
            variables=variables,
            result_value_name=result_value_name,
            variable_str="@X",
            data_str="@input")

        constraint_expressions = []
        for expr, for_range, iter_vars in c_exprs:
            if for_range:
                c_expr_str = "for %s in %s: %s" % (",".join(iter_vars),
                                                   for_range, expr)
            else:
                c_expr_str = expr

            constraint_expressions.append(c_expr_str)

    fsl_file_content = '''
variables: {}

var_type: {}

objective: {}
{}

constraints:
{}
'''.format(",".join(variables), variable_type, direction, obj_expr,
           "\n".join(constraint_expressions))

    submit_optflow_job(train_table=train_table,
                       result_table=result_table,
                       fsl_file_content=fsl_file_content,
                       solver=solver,
                       user_id=user_id)