Scripts/utils.py
def mip_optimization(cal_df, y, constrain=3, daily_weights=None):
    """Mixed integer linear programming optimization with constraints.
    Args:
        y (numpy.ndarray): sum of daily features (dim=#ofdays)
        constrain (int): minimum days in office
        daily_weights (array): weighting of days, e.g. if you prefer to come on mondays
    Return:
         
    """
    # daily weighting
    u = np.ones(len(y)) if daily_weights==None else daily_weights
    I = range(len(y))   # idx for days for summation

    m = Model("knapsack")   # MIP model
    w = [m.add_var(var_type=BINARY) for i in I] # weights to optimize
    m.objective  = maximize(xsum(y[i]* w[i] for i in I)) # optimization function
    m += xsum(w[i] * u[i] for i in I) <= constrain # constraint 
    m.optimize()

    #selected = [i for i in I if w[i].x >= 0.99]
    selected = [w[i].x for i in I]
    
    df = pd.DataFrame(columns=["home_office"], index=cal_df.index, data={'home_office': selected})
    
    return df
Mixed integer linear programming optimization with constraints.
    Args:
        y (numpy.ndarray): sum of daily features (dim=#ofdays)
        constrain (int): minimum days in office
        daily_weights (array): weighting of days, e.g. if you prefer to come on mondays
    Return:
         
    
Mixed integer linear programming optimization with constraints.
Mixed integer linear programming optimization with constraints .
def mip_optimization(cal_df, y, constrain=3, daily_weights=None):
    u = np.ones(len(y)) if daily_weights==None else daily_weights
    I = range(len(y))   
    m = Model("knapsack")   
    w = [m.add_var(var_type=BINARY) for i in I] 
    m.objective  = maximize(xsum(y[i]* w[i] for i in I)) 
    m += xsum(w[i] * u[i] for i in I) <= constrain 
    m.optimize()
    selected = [w[i].x for i in I]
    df = pd.DataFrame(columns=["home_office"], index=cal_df.index, data={'home_office': selected})
    return df
def mip_optimization ( cal_df , y , constrain = 3 , daily_weights = None ) : u = np . ones ( len ( y ) ) if daily_weights == None else daily_weights I = range ( len ( y ) ) m = Model ( "knapsack" ) w = [ m . add_var ( var_type = BINARY ) for i in I ] m . objective = maximize ( xsum ( y [ i ] * w [ i ] for i in I ) ) m += xsum ( w [ i ] * u [ i ] for i in I ) <= constrain m . optimize ( ) selected = [ w [ i ] . x for i in I ] df = pd . DataFrame ( columns = [ "home_office" ] , index = cal_df . index , data = { 'home_office' : selected } ) return df
Mixed integer linear programming optimization with constraints.
Mixed integer linear programming optimization with constraints .
"""Mixed integer linear programming optimization with constraints.
    Args:
        y (numpy.ndarray): sum of daily features (dim=#ofdays)
        constrain (int): minimum days in office
        daily_weights (array): weighting of days, e.g. if you prefer to come on mondays
    Return:
         
    """ # daily weighting # idx for days for summation # MIP model # weights to optimize # optimization function # constraint  #selected = [i for i in I if w[i].x >= 0.99]
cal_df
y
sum of daily features (dim=#ofdays)
sum of daily features ( dim = #ofdays )
constrain
minimum days in office
minimum days in office
daily_weights
weighting of days, e.g. if you prefer to come on mondays
weighting of days e . g . if you prefer to come on mondays