예제 #1
0
def solve13():
    # Optimization of Multiple Linked Phases
    # Initialize gekko model
    m = GEKKO()
    # Number of collocation nodes
    nodes = 3

    # Number of phases
    n = 5

    # Time horizon (for all phases)
    m.time = np.linspace(0, 1, 100)

    # Input (constant in IMODE 4)
    u = [m.Var(1, lb=-2, ub=2, fixed_initial=False) for i in range(n)]

    # Example of same parameter for each phase
    tau = 5

    # Example of different parameters for each phase
    K = [2, 3, 5, 1, 4]

    # Scale time of each phase
    tf = [1, 2, 4, 8, 16]

    # Variables (one version of x for each phase)
    x = [m.Var(0) for i in range(5)]

    # Equations (different for each phase)
    for i in range(n):
        m.Equation(tau * x[i].dt() / tf[i] == -x[i] + K[i] * u[i])

    # Connect phases together at endpoints
    for i in range(n - 1):
        m.Connection(x[i + 1], x[i], 1, len(m.time) - 1, 1, nodes)
        m.Connection(x[i + 1], 'CALCULATED', pos1=1, node1=1)

    # Objective
    # Maximize final x while keeping third phase = -1
    m.Obj(-x[n - 1] + (x[2] + 1)**2 * 100)

    # Solver options
    m.options.IMODE = 6
    m.options.NODES = nodes

    # Solve
    m.solve()

    # Calculate the start time of each phase
    ts = [0]
    for i in range(n - 1):
        ts.append(ts[i] + tf[i])

    # Plot
    plt.figure()
    tm = np.empty(len(m.time))
    for i in range(n):
        tm = m.time * tf[i] + ts[i]
        plt.plot(tm, x[i])
    plt.show()
예제 #2
0
c = 17.5
r = 0.71
k = 80.5
U_max = 20

u = m.MV(lb=0, ub=1, value=1)
u.STATUS = 1

x = m.Var(value=70)

m.Equation(x.dt() == r * x * (1 - x / k) - u * U_max)

J = m.Var(value=0)
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf, J, pos2='end')
m.Equation(J.dt() == (E - c / x) * u * U_max)
m.Obj(-Jf)

m.options.IMODE = 6
m.options.NODES = 3
m.options.SOLVER = 3

m.solve(debug=True)

#m.GUI()
print(Jf.value[0])

plt.figure()
plt.subplot(2, 1, 1)
plt.plot(m.time, J.value, label='$')
예제 #3
0
class MPCnode(Node):
    def __init__(self, id, x, y, nrj, ctrlHrz, ctrlRes):
        super().__init__(id, x, y, nrj)  
        
        self.verbose = True
        
        self.ctrlHrz = ctrlHrz                  # Control Horizon
        
        # time points
        self.ctrlRes = ctrlRes                  # Control Resolution. Number of control steps within the control horizon
        # constants
        self.Egen = 1*10**-5
        self.const = 0.6
        
        self.packet = 1
        self.E = 1
        
        self.m = GEKKO(remote=False)
        self.m.time = np.linspace( 0, self.ctrlHrz, self.ctrlRes)
        
        
        
        #self.deltaDist = -1.5
        self.deltaDist = self.m.FV(value = -1.5)
        #self.deltaDist.STATUS = 1

                # packet rate
        self.pr = self.m.MV(value=self.PA, integer = True,lb=1,ub=20)
        self.pr.STATUS = 1
        self.pr.DCOST = 0
        
        # Energy Stored
        self.nrj = self.m.Var(value=0.05, lb=0)                  # Energy Amount
        self.d = self.m.Var(value=70, lb = 0)                     # Distance to receiver
        self.d2 = self.m.Intermediate(self.d**2)
        
        
        
        # energy/pr balance
        #self.m.Equation(self.d.dt() == -1.5)
        self.m.Equation(self.d.dt() == self.deltaDist)
        self.m.Equation(self.nrj >= self.Egen - ((Eelec+EDA)*self.packet + self.pr*self.pSize*(Eelec + Eamp * self.d2)))
        self.m.Equation(self.nrj.dt() == self.Egen - ((Eelec+EDA)*self.packet + self.pr*self.pSize*(Eelec + Eamp * self.d2)))
        
        
        # objective (profit)
        self.J = self.m.Var(value=0)
        # final objective
        self.Jf = self.m.FV()
        self.Jf.STATUS = 1
        self.m.Connection(self.Jf,self.J,pos2='end')
        self.m.Equation(self.J.dt() == self.pr*self.pSize)
        # maximize profit
        self.m.Obj(-self.Jf)
        
        # options
        self.m.options.IMODE = 6  # optimal control
        self.m.options.NODES = 3  # collocation nodes
        self.m.options.SOLVER = 1 # solver (IPOPT)





    def resetGEKKO(self):
        self.m = GEKKO(remote=False)
        self.m.time = np.linspace( 0, self.ctrlHrz, self.ctrlRes)
        
        self.pr = self.m.MV(value=self.PA, integer = True,lb=1,ub=20)
        self.pr.STATUS = 1
        self.pr.DCOST = 0
        

    def getDeltaDist(self, sinkX, sinkY, sdeltaX, sdeltaY, deltaDist):        
        distBefore = np.sqrt((sinkX**2)+(sinkY**2))
        distAfter = np.sqrt(((sinkX+sdeltaX)**2)+((sinkY+sdeltaY)**2))
        self.deltaDist = distAfter - distBefore
        return self.deltaDist
    
    def controlPR(self, deltaD):        

        
        # solve optimization problem
        self.deltaDist.value = 1.5

        self.m.solve(disp=False)
        
        self.setPR(self.pr.value[1])
        #self.pr.value = self.pr.value[1]
        
        #self.m.GUI()
        
        # print profit
        print('Optimal Profit: ' + str(self.Jf.value[0]))
        
        if self.verbose:
            # plot results
            plt.figure(1)
            plt.subplot(2,1,1)
            plt.plot(self.m.time,self.J.value,'r--',label='packets')
            plt.plot(self.m.time[-1],self.Jf.value[0],'ro',markersize=10,\
                     label='final packets = '+str(self.Jf.value[0]))
            plt.plot(self.m.time,self.nrj.value,'b-',label='energy')
            plt.ylabel('Value')
            plt.legend()
            plt.subplot(2,1,2)
            plt.plot(self.m.time,self.pr.value,'k.-',label='packet rate')
            plt.ylabel('Rate')
            plt.xlabel('Time (yr)')
            plt.legend()
            plt.show()
예제 #4
0
u = m.MV(value=1, lb=0, ub=1)
u.STATUS = 1
u.DCOST = 0

# population
x = m.Var(70)

# population balance
m.Equation(x.dt() == r * x * (1 - x/k) - u*u_max)

# profit
j = m.Var(value=0) # objective
jF = m.FV() # final objective
jF.STATUS = 1

m.Connection(jF, j, pos2 = 'end')
m.Equation(j.dt() == (e - c / x) * u * u_max)

m.Obj(-j)

m.options.IMODE = 6 # optimal control mode
m.options.NODES = 3
m.options.SOLVER = 3

m.solve(disp=False)

print(jF.value[0])

plt.figure(1) # plot results
plt.subplot(2,1,1)
plt.plot(m.time,j.value,'r--',label='profit')
예제 #5
0
def fishing():
    m = GEKKO()

    n = 101
    m.time = np.linspace(0, 10, n)

    E = 1
    c = 17.5
    r = 0.71
    k = 80.5
    U_max = 20

    u = m.MV(lb=0, ub=1, value=1)
    u.STATUS = 1

    x = m.Var(value=70)

    m.Equation(x.dt() == r * x * (1 - x / k) - u * U_max)

    J = m.Var(value=0)
    Jf = m.FV()
    Jf.STATUS = 1
    m.Connection(Jf, J, pos2='end')
    m.Equation(J.dt() == (E - c / x) * u * U_max)
    m.Obj(-Jf)

    m.options.IMODE = 6
    m.options.NODES = 3
    m.options.SOLVER = 3

    m.solve(disp=False)

    assert J.value == [
        0.0, 1.495191, 2.980856, 4.45711, 5.924057, 7.381791, 8.830394,
        10.26994, 11.70049, 13.1221, 14.5348, 15.93865, 17.33365, 18.71983,
        20.0972, 21.46575, 22.82547, 24.17635, 25.51835, 26.85144, 28.17557,
        29.49069, 30.79672, 32.09258, 33.19262, 34.14704, 35.03382, 35.90079,
        36.7674, 37.63793, 38.51146, 39.38639, 40.26171, 41.13703, 42.01228,
        42.88747, 43.76263, 44.63778, 45.51293, 46.38808, 47.26324, 48.13839,
        49.01354, 49.88869, 50.76384, 51.63899, 52.51415, 53.3893, 54.26445,
        55.1396, 56.01475, 56.88991, 57.76506, 58.64021, 59.51536, 60.39051,
        61.26566, 62.14082, 63.01597, 63.89114, 64.76636, 65.64164, 66.51694,
        67.39212, 68.2665, 69.13882, 70.00774, 70.87441, 71.74908, 72.66269,
        73.67438, 74.84509, 76.11564, 77.37664, 78.62796, 79.86948, 81.10102,
        82.32241, 83.53349, 84.73404, 85.92385, 87.10269, 88.2703, 89.4264,
        90.57071, 91.70289, 92.82261, 93.92949, 95.02312, 96.10307, 97.16886,
        98.21996, 99.25584, 100.2759, 101.2794, 102.2657, 103.2339, 104.1833,
        105.1128, 106.0215, 106.9081
    ]
    assert Jf.value == [
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081, 106.9081,
        106.9081, 106.9081, 106.9081
    ]
    assert x.value == [
        70.0, 68.68286, 67.43015, 66.23608, 65.0955, 64.00383, 62.95696,
        61.95118, 60.98318, 60.04994, 59.14873, 58.27707, 57.43269, 56.61351,
        55.81765, 55.04334, 54.28897, 53.55306, 52.83422, 52.13115, 51.44267,
        50.76765, 50.10503, 49.45505, 49.10604, 48.98275, 48.96466, 48.97739,
        48.99067, 48.99787, 49.00039, 49.00074, 49.00047, 49.00021, 49.00006,
        49.0, 48.99999, 48.99999, 48.99999, 48.99999, 49.0, 49.0, 49.0, 49.0,
        49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.00001,
        49.00001, 49.00001, 49.00001, 49.0, 48.99997, 48.99987, 48.99967,
        48.99944, 48.9994, 49.0006, 49.005, 49.0147, 49.02789, 49.02863,
        48.96884, 48.75625, 48.29394, 47.6701, 47.05438, 46.44599, 45.84423,
        45.24837, 44.65774, 44.07168, 43.48957, 42.91077, 42.33468, 41.7607,
        41.18826, 40.61676, 40.04563, 39.47429, 38.90217, 38.32869, 37.75326,
        37.17529, 36.59418, 36.00931, 35.42005, 34.82575, 34.22573, 33.6193,
        33.00574, 32.38429, 31.75415, 31.1145
    ]
    assert u.value == [
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.853423, 0.7421807,
        0.6899773, 0.6745367, 0.674141, 0.6771105, 0.6794158, 0.6804968,
        0.6808019, 0.6808022, 0.6807487, 0.6807017, 0.6806795, 0.6806744,
        0.6806733, 0.680673, 0.680673, 0.6806731, 0.6806734, 0.6806735,
        0.6806737, 0.6806738, 0.6806739, 0.6806739, 0.6806738, 0.6806737,
        0.6806736, 0.6806735, 0.6806733, 0.6806732, 0.6806732, 0.6806732,
        0.6806735, 0.6806743, 0.6806769, 0.6806896, 0.6807248, 0.6807767,
        0.6807916, 0.6806986, 0.6800795, 0.6784639, 0.6757571, 0.6738888,
        0.6799703, 0.710282, 0.7875389, 0.9150895, 1.0, 1.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
    ]