コード例 #1
0
def solve_lad(X, Y):
    Y_cvx = matrix(Y)
    X_cvx = matrix(X)
    w_hat = variable(X.shape[1])
    solvers.options['show_progress'] = False
    op(sum(abs(Y_cvx - X_cvx * w_hat))).solve()
    return w_hat.value
コード例 #2
0
    def balance(self, energy_demand):
        active_engines = filter(lambda engine: engine.is_enabled, self.engines)
        
        if len(active_engines) <= 0:
            return []
            
        rpms = variable(len(active_engines), 'rpms')    

        fixed_engine_costs = matrix([float(engine.engine_type.fixed_engine_cost) for engine in active_engines])
        linear_engine_costs = matrix([float(engine.engine_type.linear_engine_cost) for engine in active_engines])

        fixed_energy_outputs = matrix([float(engine.engine_type.fixed_energy_output) for engine in active_engines])
        linear_energy_outputs = matrix([float(engine.engine_type.linear_energy_output) for engine in active_engines])

        minimum_rpms = matrix([float(engine.engine_type.minimum_rpm) for engine in active_engines])
        maximum_rpms = matrix([float(engine.engine_type.maximum_rpm) for engine in active_engines])

        energy_demand_constraint = ((float(energy_demand) - dot(rpms, linear_energy_outputs) - sum(fixed_energy_outputs)) <= 0)

        maximum_rpm_constraint = ((rpms - maximum_rpms) <= 0)
        minimum_rpm_constraint = ((rpms - minimum_rpms) >= 0)

        constraints = [energy_demand_constraint, maximum_rpm_constraint, minimum_rpm_constraint]                
        objective_function = op((dot(linear_engine_costs, rpms) - sum(fixed_engine_costs)), constraints)        
                      
        objective_function.solve()       
        
        for i in range(len(active_engines)):
            engine = active_engines[i]
            engine.rpm = rpms.value[i]
            engine.energy_output = float(engine.engine_type.fixed_energy_output) * engine.rpm + float(engine.engine_type.fixed_energy_output)
            
        return active_engines              
コード例 #3
0
def lin_regression():

    raw_data = pd.read_csv("../Datasets/winequality-red.csv",
                           sep=";",
                           header=0)
    raw_training = raw_data[:1500]
    x = cm.matrix(raw_training.iloc[:, :-1].to_numpy(dtype=float))
    y = cm.matrix(raw_training.iloc[:, -1:].to_numpy(dtype=float))

    raw_test = raw_data[1500:].reset_index(drop=True)
    x_test = cm.matrix(raw_test.iloc[:, :-1].to_numpy(dtype=float))
    y_test = cm.matrix(raw_test.iloc[:, -1:].to_numpy(dtype=float))

    a = cm.variable(x.size[1])
    b = cm.variable()
    z = cm.variable(x.size[0])

    constraint_1 = (z >= (y - x * a - b))
    constraint_2 = (z >= (x * a + b - y))

    z_min = cm.op(cm.min(cm.sum(z) / x.size[0]), [constraint_1, constraint_2])
    z_min.solve()

    calc_a = a.value
    calc_b = b.value

    z_train = y - x * calc_a - calc_b
    z_test = y_test - x_test * calc_a - calc_b

    train_results = x * calc_a + calc_b
    average_training_error = mean_square_error(y, train_results)

    test_results = x_test * calc_a + calc_b
    average_test_error = mean_square_error(y_test, test_results)

    print(f"average training error = {average_training_error}")
    print(f"average testing error = {average_test_error}")
コード例 #4
0
ファイル: views.py プロジェクト: justinsvegliato/omnitech
def balance(request, engines, demand):
    engines = json.loads(engines)
    enabled_engines = filter(lambda engine: engine["_isEnabled"], engines)
    print enabled_engines
    if not enabled_engines:
        return HttpResponse(json.dumps(enabled_engines))
        
    vector_size = len(enabled_engines)
    rpms = variable(vector_size, "rpms")   

    fixed_engine_costs = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).fixed_engine_cost) for engine in enabled_engines])
    linear_engine_costs = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).linear_engine_cost) for engine in enabled_engines])

    fixed_energy_outputs = matrix([float(EngineType.objects.get(pk=int(engine["_engineTypeId"])).fixed_energy_output) for engine in enabled_engines])
    linear_energy_outputs = matrix([float(EngineType.objects.get(pk=int(engine["_engineTypeId"])).linear_energy_output) for engine in enabled_engines])

    minimum_rpms = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).minimum_rpm) for engine in enabled_engines])
    maximum_rpms = matrix([float(EngineType.objects.get(pk=engine["_engineTypeId"]).maximum_rpm) for engine in enabled_engines])

    demand_constraint = ((float(demand) - dot(rpms, linear_energy_outputs) - sum(fixed_energy_outputs)) <= 0)

    maximum_rpm_constraint = ((rpms - maximum_rpms) <= 0)
    minimum_rpm_constraint = ((rpms - minimum_rpms) >= 0)

    constraints = [demand_constraint, maximum_rpm_constraint, minimum_rpm_constraint]                
    objective_function = op((dot(linear_engine_costs, rpms) + sum(fixed_engine_costs)), constraints)        
                      
    objective_function.solve()

    print rpms.value

    rpmsIndex = 0
    for engine in engines:
        engine["_rpm"] = 0
        engine["_energyOutput"] = 0
        if engine["_isEnabled"]:
            print rpms
            if rpms.value:
                engine["_rpm"] = rpms.value[rpmsIndex]
                rpmsIndex += 1
            else:
                engine["_rpm"] = float(EngineType.objects.get(pk=engine["_engineTypeId"]).maximum_rpm)

            energy_output_per_rpm = float(EngineType.objects.get(pk=engine["_engineTypeId"]).linear_energy_output)
            base_energy_output = float(EngineType.objects.get(pk=engine["_engineTypeId"]).fixed_energy_output)
            engine["_energyOutput"] = energy_output_per_rpm * float(engine["_rpm"]) + base_energy_output      

    return HttpResponse(json.dumps(engines));
コード例 #5
0
def cvxopt(storage_size_list,house_pv,sp_pv,pandr_pv):
    cost_list=[]
    for storage_max in storage_size_list:
        storage_max=float(storage_max)
        storage_power=storage_max/8
        [buy_price,sell_price]=price()
        
        pvl=house_pv*solar_data()+sp_pv*solar_data_flat()+pandr_pv*solar_data_10()  #PV assumed to be accurately predictied by weather forecast
        fix_load=load_heat_data()+hot_water()    #load which assmued to be accurately predicted, which is heat load
        float_load=residential()+load_sciencepark()+car_charging()     #Load which cannot be predicited accurately, so predicted by data of a week ago
        use_load=fix_load+np.roll(float_load,7*48)   #Estimated total load to be used for cvxopt
        '''
        #use_load=load_heat_data()+hot_water()+residential()+load_sciencepark()
        
        eff=math.sqrt(0.85)
        start=200
        end=207
        duration=(end-start)*48
        day_i=list(range(start*48,end*48))
        
        
        cml=([-1.]+[0.]*(duration-1)+[1.])*(duration-1)+[-1.]
        #cml=([-math.sqrt(0.85)]+[0.]*(duration-1)+[math.sqrt(0.85)])*(duration-1)+[-math.sqrt(0.85)]
        #cml=([-0.85]+[0.]*(duration-1)+[0.85])*(duration-1)+[-0.85]
        cm=matrix(cml,(duration,duration))
        
        pv=matrix(pvl[day_i],(duration,1))
        load=matrix(use_load[day_i],(duration,1))
        bp=matrix(buy_price[day_i],(duration,1))
        sp=matrix(sell_price[day_i],(duration,1))
        
        storage_max=100000.
        zero=matrix([0.]*duration,(duration,1))
        emax=matrix([storage_max]*duration,(duration,1))  #maximum storage
        pmax=matrix([50.e3]*duration,(duration,1))  #maximum power to grid
        charge_pmax=matrix([25000.]*duration,(duration,1))
        effm=matrix([eff]*duration,(duration,1))
        ieffm=matrix([1/eff]*duration,(duration,1))
        #discharge_pmax=matrix([-25000.]*duration,(duration,1))
        
        e=variable(duration)  #Stored energy
        c=variable(duration)  #Charging energy 
        d=variable(duration)  #Discharging energy 
        b=variable(duration)  #Buy
        s=variable(duration)  #Sell 
        
        i1=(b>=zero)
        i2=(s>=zero)
        i3=(e>=zero)
        i4=(e<=emax)
        i5=(b<=pmax)
        i6=(s<=pmax)
        i7=(c<=charge_pmax)
        i8=(d<=charge_pmax)
        i9=(c>=zero)
        i10=(d>=zero)
        
        e1=(c/eff-d*eff+load-pv==b-s)
        e2=(e[0]==matrix([0.]))
        e3=(c-d==cm*e)
        
        ob=dot(b,bp)-dot(s,sp)
        
        lp=op(ob,[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,e1,e2,e3])
        solvers.options['show_progress'] = True
        lp.solve()
        '''
        
        eff=math.sqrt(0.85)         #Round trip efficiency = 0.85, so each process of charge or discharge has efficiency of sqrt(0.85)
        start=0
        end=365
        duration=(end-start)*48
        day_i=list(range(start*48,end*48))
        cvxopt_balancing=[]
        cvxopt_buy=np.array([])     #Record power traded with grid, positive = import
        cvxopt_storage=np.array([])     #Record amount of stored energy
        day_cost=0
        
        for day in range(start,end):
            time_index=list(range(day*48,day*48+48))
            cml=([-1.]+[0.]*(48-1)+[1.])*(48-1)+[-1.]       #List to create matrix to multiply with amount of energy stored in each time period to calculate the change in energy stored
            #cml=([-math.sqrt(0.85)]+[0.]*(duration-1)+[math.sqrt(0.85)])*(duration-1)+[-math.sqrt(0.85)]
            #cml=([-0.85]+[0.]*(duration-1)+[0.85])*(duration-1)+[-0.85]
            cm=matrix(cml,(48,48))
            
            pv=matrix(pvl[time_index],(48,1))
            load=matrix(use_load[time_index],(48,1))
            bp=matrix(buy_price[time_index],(48,1))
            sp=matrix(sell_price[time_index],(48,1))
            
            zero=matrix([0.]*48,(48,1))
            emax=matrix([storage_max]*48,(48,1))  #maximum storage
            pmax=matrix([50.e3]*48,(48,1))  #maximum power to grid
            charge_pmax=matrix([storage_power]*48,(48,1))
            #discharge_pmax=matrix([-25000.]*duration,(duration,1))
            
            e=variable(48)  #Stored energy
            c=variable(48)  #Charging energy 
            d=variable(48)  #Discharging energy 
            b=variable(48)  #Buy
            s=variable(48)  #Sell 
            
            i1=(b>=zero)
            i2=(s>=zero)
            i3=(e>=zero)
            i4=(e<=emax)
            i5=(b<=pmax)
            i6=(s<=pmax)
            i7=(c<=charge_pmax) #Maximum power of storage
            i8=(d<=charge_pmax)
            i9=(c>=zero)
            i10=(d>=zero)
            i11=(c[47]-d[47]+e[47]>=matrix([0.]))       #Energy in storage always > 0
            i12=(c[47]-d[47]+e[47]<=matrix([storage_max]))  #Energy in storage always < maximum capacity
            
            e1=(c/eff-d*eff+load-pv==b-s)
            if day == start:
                e2=(e[0]==matrix([0.]))
            else:
                next_storage=cvxopt_storage[-1]-cvxopt_balancing[-1]
                e2=(e[0]==matrix(next_storage))
            e3=(c-d==cm*e)
            
            ob=dot(b,bp)-dot(s,sp)
            
            lp=op(ob,[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,e1,e2,e3])
            solvers.options['show_progress'] = False
            lp.solve()
            day_cost+=lp.objective.value()[0]
        
            for t in range(0,48):
                cvxopt_balancing.append(d.value[t]-c.value[t])
                cvxopt_buy=np.append(cvxopt_buy,b.value[t]-s.value[t])
                cvxopt_storage=np.append(cvxopt_storage,e.value[t])
        #print(b.value[47]-s.value[47])
        #print(c.value[47]*eff-d.value[47]/eff+e.value[47])
        
#        print(day_cost)
        #Calculate actual energy flow based on prediction of CVXOPT
        buy=[]
        charge_power_l=[]
        balancing_l=[]
        storage=np.array([e.value[0]])     #Initial charge in storage decided by cvxopt
        excess=(pvl-fix_load-float_load)[day_i]
        for t in range(duration):
            if cvxopt_balancing[t]>0:      #Storage discharge
                if cvxopt_balancing[t]>storage[t]:      #CVXOPT wants more discharge than currently stored
        #            balancing=-storage[t]*eff            #Storage discharge all stored
                    charge_power=-storage[t]
        #            print('a')
                else:
        #            balancing=-cvxopt_balancing[t]*eff   #Do as CVXOPT wants
                    charge_power=-cvxopt_balancing[t]
        #            print('b')
            else:                           #Storage charge
                if cvxopt_balancing[t]>storage_max-storage[t]:      #CVXOPT wants to charge more than remaining capacity
        #            balancing=(storage_max-storage[t])/eff             #Charge by remaining capacity
                    charge_power=storage_max-storage[t]
        #            print('c')
                else:
        #            balancing=-cvxopt_balancing[t]/eff   #Do as CVXOPT wants
                    charge_power=-cvxopt_balancing[t]
        #            print('d')
            if charge_power>0:
                balancing=charge_power/eff
            else:
                balancing=charge_power*eff
            buy.append(-excess[t]+balancing)
            balancing_l.append(balancing)
            charge_power_l.append(-charge_power)
            storage=np.append(storage,storage[t]+charge_power)
        #    print(t,excess[t],charge_power,cvxopt_balancing[t])
        #    print(buy[t],cvxopt_buy[t])
        #    print(storage[t],cvxopt_storage[t])
        running_cost=market(buy,sell_price[day_i],buy_price[day_i])
#        print(running_cost)
        cost_list.append(running_cost)
#        print(max(max(buy-cvxopt_buy),-min(buy-cvxopt_buy)))
        #print(sell_price[day_i])
        #print(buy_price[day_i])
        balancing_l=np.array(balancing_l)
#        fig,ax=plt.subplots()
        #ax.plot(list(range(0,duration)),(fix_load+float_load)[day_i],'-r',label='actual')
        #ax.plot(list(range(0,duration)),use_load[day_i],'-b',label='estimate')
        #ax.step(list(range(0,duration)),buy,'-r',label='actual')
        #ax.step(list(range(0,duration)),cvxopt_buy,'-b',label='estimate')
        #ax.step(list(range(0,duration)),charge_power_l,'-r',label='actual')
        #ax.step(list(range(0,duration)),cvxopt_balancing,'-b',label='estimate')
        #ax.step(list(range(0,duration+1)),storage,'-b',label='estimate')
#        t_list=np.array(range(49))/2
#        p1=ax.step(t_list,pvl[206*48-1:207*48]/500,'-r',label='PV generation')
#        p2=ax.step(t_list,(fix_load+float_load)[206*48-1:207*48]/500,'-k',label='demand')
#        p3=ax.fill_between(t_list,0,(pvl-balancing_l)[206*48-1:207*48]/500,step='pre',color='y',label='power output')
#        ax.set_xlim([0,24])
#        plt.xticks([0,6,12,18,24])
#        plt.ylabel('power (MW)')
#        plt.xlabel('time')
#        plt.ylim([-10,45])
#        plt.title('Power Flow on a Summer Day using CVXOPT')
#        fig.legend(loc='upper center', bbox_to_anchor=(0.77, 0.89), ncol=1)
#        plt.savefig('summer cvxopt power.png',dpi=600)
    return cost_list
コード例 #6
0
def cvxopt(storage_size_list):
    cost_list=[]
    for storage_max in storage_size_list:
        storage_max=float(storage_max)
        storage_power=storage_max/4
        [buy_price,sell_price]=price()
        
        pvl=236000*0.9*0.2*0.9*solar_data()+88000*0.9*0.2*0.9*solar_data_flat()
        fix_load=load_heat_data()+hot_water()    #load which assmued to be accurately predicted, which is heat load
        float_load=residential()+load_sciencepark()     #Load which cannot be predicited accurately, so predicted by data of a week ago
        use_load=fix_load+np.roll(float_load,7*48)   #Estimated total load to be used for cvxopt
        '''
        #use_load=load_heat_data()+hot_water()+residential()+load_sciencepark()
        
        eff=math.sqrt(0.85)
        start=200
        end=207
        duration=(end-start)*48
        day_i=list(range(start*48,end*48))
        
        
        cml=([-1.]+[0.]*(duration-1)+[1.])*(duration-1)+[-1.]
        #cml=([-math.sqrt(0.85)]+[0.]*(duration-1)+[math.sqrt(0.85)])*(duration-1)+[-math.sqrt(0.85)]
        #cml=([-0.85]+[0.]*(duration-1)+[0.85])*(duration-1)+[-0.85]
        cm=matrix(cml,(duration,duration))
        
        pv=matrix(pvl[day_i],(duration,1))
        load=matrix(use_load[day_i],(duration,1))
        bp=matrix(buy_price[day_i],(duration,1))
        sp=matrix(sell_price[day_i],(duration,1))
        
        storage_max=100000.
        zero=matrix([0.]*duration,(duration,1))
        emax=matrix([storage_max]*duration,(duration,1))  #maximum storage
        pmax=matrix([50.e3]*duration,(duration,1))  #maximum power to grid
        charge_pmax=matrix([25000.]*duration,(duration,1))
        effm=matrix([eff]*duration,(duration,1))
        ieffm=matrix([1/eff]*duration,(duration,1))
        #discharge_pmax=matrix([-25000.]*duration,(duration,1))
        
        e=variable(duration)  #Stored energy
        c=variable(duration)  #Charging energy 
        d=variable(duration)  #Discharging energy 
        b=variable(duration)  #Buy
        s=variable(duration)  #Sell 
        
        i1=(b>=zero)
        i2=(s>=zero)
        i3=(e>=zero)
        i4=(e<=emax)
        i5=(b<=pmax)
        i6=(s<=pmax)
        i7=(c<=charge_pmax)
        i8=(d<=charge_pmax)
        i9=(c>=zero)
        i10=(d>=zero)
        
        e1=(c/eff-d*eff+load-pv==b-s)
        e2=(e[0]==matrix([0.]))
        e3=(c-d==cm*e)
        
        ob=dot(b,bp)-dot(s,sp)
        
        lp=op(ob,[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,e1,e2,e3])
        solvers.options['show_progress'] = True
        lp.solve()
        '''
        
        eff=math.sqrt(0.85)
        start=0
        end=365
        duration=(end-start)*48
        day_i=list(range(start*48,end*48))
        cvxopt_balancing=[]
        cvxopt_buy=np.array([])     #Record power traded with grid, positive = import
        cvxopt_storage=np.array([])     #Record amount of stored energy
        day_cost=0
        
        for day in range(start,end):
            time_index=list(range(day*48,day*48+48))
            cml=([-1.]+[0.]*(48-1)+[1.])*(48-1)+[-1.]
            #cml=([-math.sqrt(0.85)]+[0.]*(duration-1)+[math.sqrt(0.85)])*(duration-1)+[-math.sqrt(0.85)]
            #cml=([-0.85]+[0.]*(duration-1)+[0.85])*(duration-1)+[-0.85]
            cm=matrix(cml,(48,48))
            
            pv=matrix(pvl[time_index],(48,1))
            load=matrix(use_load[time_index],(48,1))
            bp=matrix(buy_price[time_index],(48,1))
            sp=matrix(sell_price[time_index],(48,1))
            
            zero=matrix([0.]*48,(48,1))
            emax=matrix([storage_max]*48,(48,1))  #maximum storage
            pmax=matrix([50.e3]*48,(48,1))  #maximum power to grid
            charge_pmax=matrix([storage_power]*48,(48,1))
            #discharge_pmax=matrix([-25000.]*duration,(duration,1))
            
            e=variable(48)  #Stored energy
            c=variable(48)  #Charging energy 
            d=variable(48)  #Discharging energy 
            b=variable(48)  #Buy
            s=variable(48)  #Sell 
            
            i1=(b>=zero)
            i2=(s>=zero)
            i3=(e>=zero)
            i4=(e<=emax)
            i5=(b<=pmax)
            i6=(s<=pmax)
            i7=(c<=charge_pmax)
            i8=(d<=charge_pmax)
            i9=(c>=zero)
            i10=(d>=zero)
            i11=(c[47]-d[47]+e[47]>=matrix([0.]))
            i12=(c[47]-d[47]+e[47]<=matrix([storage_max]))
            
            e1=(c/eff-d*eff+load-pv==b-s)
            if day == start:
                e2=(e[0]==matrix([0.]))
            else:
                next_storage=cvxopt_storage[-1]-cvxopt_balancing[-1]
                e2=(e[0]==matrix(next_storage))
            e3=(c-d==cm*e)
            
            ob=dot(b,bp)-dot(s,sp)
            
            lp=op(ob,[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,e1,e2,e3])
            solvers.options['show_progress'] = False
            lp.solve()
            day_cost+=lp.objective.value()[0]
        
            for t in range(0,48):
                cvxopt_balancing.append(d.value[t]-c.value[t])
                cvxopt_buy=np.append(cvxopt_buy,b.value[t]-s.value[t])
                cvxopt_storage=np.append(cvxopt_storage,e.value[t])
        #print(b.value[47]-s.value[47])
        #print(c.value[47]*eff-d.value[47]/eff+e.value[47])
        
#        print(day_cost)
        
        buy=[]
        charge_power_l=[]
        balancing_l=[]
        storage=np.array([e.value[0]])     #Initial charge in storage decided by cvxopt
        excess=(pvl-fix_load-float_load)[day_i]
        for t in range(duration):
            if cvxopt_balancing[t]>0:      #Storage discharge
                if cvxopt_balancing[t]>storage[t]:      #CVXOPT wants more discharge than currently stored
        #            balancing=-storage[t]*eff            #Storage discharge all stored
                    charge_power=-storage[t]
        #            print('a')
                else:
        #            balancing=-cvxopt_balancing[t]*eff   #Do as CVXOPT wants
                    charge_power=-cvxopt_balancing[t]
        #            print('b')
            else:                           #Storage charge
                if cvxopt_balancing[t]>storage_max-storage[t]:      #CVXOPT wants to charge more than remaining capacity
        #            balancing=(storage_max-storage[t])/eff             #Charge by remaining capacity
                    charge_power=storage_max-storage[t]
        #            print('c')
                else:
        #            balancing=-cvxopt_balancing[t]/eff   #Do as CVXOPT wants
                    charge_power=-cvxopt_balancing[t]
        #            print('d')
            if charge_power>0:
                balancing=charge_power/eff
            else:
                balancing=charge_power*eff
            buy.append(-excess[t]+balancing)
            balancing_l.append(balancing)
            charge_power_l.append(-charge_power)
            storage=np.append(storage,storage[t]+charge_power)
        #    print(t,excess[t],charge_power,cvxopt_balancing[t])
        #    print(buy[t],cvxopt_buy[t])
        #    print(storage[t],cvxopt_storage[t])
        running_cost=market(buy,sell_price[day_i],buy_price[day_i])
#        print(running_cost)
        cost_list.append(running_cost)
        print(max(max(buy-cvxopt_buy),-min(buy-cvxopt_buy)))
        #print(sell_price[day_i])
        #print(buy_price[day_i])
        
        #fig,ax=plt.subplots()
        #ax.plot(list(range(0,duration)),(fix_load+float_load)[day_i],'-r',label='actual')
        #ax.plot(list(range(0,duration)),use_load[day_i],'-b',label='estimate')
        #ax.step(list(range(0,duration)),buy,'-r',label='actual')
        #ax.step(list(range(0,duration)),cvxopt_buy,'-b',label='estimate')
        #ax.step(list(range(0,duration)),charge_power_l,'-r',label='actual')
        #ax.step(list(range(0,duration)),cvxopt_balancing,'-b',label='estimate')
        #ax.step(list(range(0,duration+1)),storage,'-b',label='estimate')
        #fig.legend()
    return cost_list