Skip to main content

initialization

Ongoing

Comments

3 comments

  • Hi Param,

    To model a scenario where a variable can take only one value from a specified set at a particular time-step, and later use this variable for plotting or other purposes, you can utilize the capabilities of the Gurobi Optimizer in a more targeted manner. Here's a step-by-step explanation and a Python code snippet using the Gurobi Python API to achieve this:

    ### Defining the Problem
    You have a set \( \{8, 16, 24, 32\} \), and you want to assign exactly one of these values to a variable \( r[i, j] \) at time-step \( j \). This seems like a classic use-case for binary variables to select the value from the set.

    ### Step-by-Step Implementation
    1. **Variable Definitions**:
       - Define a binary variable for each value in the set for each timestep. For instance, \( b_8, b_{16}, b_{24}, b_{32} \) could be binary variables where \( b_k = 1 \) if \( r[i,j] = k \) and \( 0 \) otherwise.
       - Define \( r[i, j] \) as an integer or continuous variable depending on your specific requirements (e.g., sum of the products of set elements and corresponding binary variables).

    2. **Constraints**:
       - Ensure only one binary variable can take the value 1 at any time-step. This enforces that \( r[i, j] \) can only take one value from the set.
       - The value of \( r[i, j] \) should be equal to the sum of the products of each element in the set and its corresponding binary variable.

    3. **Objective (if any)**:
       - Depending on the broader problem, define an objective function.

    4. **Retrieving and Plotting Data**:
       - After solving the model, retrieve the value of \( r[i, j] \) for each timestep and use it for plotting or further analysis.

    This approach should solve your issue by setting up a decision variable linked with a set of binary variables to enforce the exclusive choice of values from your set.

     

    - Bot

    0
  • Param Patel
    First Comment
    First Question

    Thank you for your reply. I tried that approach but it did not work 

    def model_variables_and_constraints(model, tk, v1_t, v2_t, e1, e2, v, T_full_charge, T_top_up, delta_t):
        r1t = {}
        e1t = {}
        # current_set = [8, 16, 24, 32, 48, 64]

        b1 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_8a')
        b2 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_16a')
        b3 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_24a')
        b4 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_32a')
        b5 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_48a')
        b6 = model.addVar(vtype=gb.GRB.BINARY, name='binary_var_current_64a')

        model.addConstr(gb.quicksum([b1,b2,b3,b4,b5,b6])==1)
        # Add the charging variables r and e for full charge and top-up EV
        for i in range(len(v1_t)):
            for j in range(len(tk)):
                r1t[i, j] = model.addVar(lb=0, ub=64, vtype=gb.GRB.INTEGER, name=f'EV_{i}_current_{j}')
                model.addConstr(r1t[i,j] == gb.quicksum([8*b1,16*b2,24*b3,32*b4,48*b5,64*b6]))
                e1t[i, j] = model.addVar(lb=0, ub=e1[i], vtype=gb.GRB.INTEGER, name=f'ChargingVariable_e1_{i}_{j}')
                if tk[i] <= tk[j] <= T_full_charge[i]:
                    model.addConstr(r1t[i, j] == 0)

        # Define constraints
        for i in range(len(v1_t)):
            for j in range(len(tk)):
                res = model.addVar(lb=0, ub=50000, vtype=gb.GRB.INTEGER, name=f'volts*current_{i}_{j}')
                final_res = model.addVar(lb=0, ub=50, vtype=gb.GRB.INTEGER, name=f'round_result_{i}_{j}')
                model.addConstr(res == r1t[i, j] * v * delta_t)
                model.addConstr(final_res == res / 1000)
                # Constraint to update pending energy based on cumulative charging completed till that step
                if tk[i]<=tk[j]<=T_full_charge[i]:
                    if j > 0:
                        model.addConstr(e1t[i, j] == e1[j-1] - final_res)
                    else:
                        model.addConstr(e1t[i, j] == e1[i] - final_res)
                else:
                    model.addConstr(e1t[i,j]==0)
     

        return r1t, e1t
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Param,

    How can I assign a constant value to a variable at a particular timestep?

    What is meant here by timestep?

    - Riley

    0

Please sign in to leave a comment.