Exemplo n.º 1
0
    def __calc__(self):
        p = Factorial
        iter = self.__state_iter
        proxy = SolverProxy()
        num = 0
        for tau, tau_state in iter(p.tau_lh()):
            for delta, delta_state in iter(p.delta_lh()):
                for cn, cn_state in iter(p.cn_lh()):
                    for cr, cr_state in iter(p.cr_lh(cn, delta)):
                        for s, s_state in iter(p.s_lh(cn)):
                            for a, a_state in iter(p.a_lh()):
                                # calculate model with restocking_fee
                                par_nb = Parameter(MODEL_NB, tau=tau, a=a, s=s, cn=cn)
                                sol_nb = proxy.read_or_calc_and_write(par_nb, resolution='middle')
                                num += 1; print(num)
                                #sol_nb = ModelNBSolver.solve(par_nb, 'low')
                                key_n = self.__key(MODEL_NB, tau_state, a_state, s_state, cr_state, cn_state, delta_state)
                                self._add(key_n, par_nb, sol_nb)

                                # calculate model two
                                par_o = Parameter(MODEL_2, tau=tau, a=a, s=s, cr=cr, cn=cn, delta=delta)
                                sol_o = proxy.calculate(par_o)
                                key_o = self.__key(MODEL_2, tau_state, a_state, s_state, cr_state, cn_state, delta_state)
                                self._add(key_o, par_o, sol_o)
        proxy.commit()
Exemplo n.º 2
0
    def _a_cn_generator(self):
        """
            Helper generator to generate parameters for plotting
            it varies cn from [cr, 1] and a from [0, upper_bound_a]
            Returns a tuple of (par_model_1, par_model_2)
        """
        def __cr(cn):
            #if self.cr == 'delta*cn/2':
            #    return self.delta * cn / 2
            if self.cr == '0.4*cn':
                return 0.4 * cn
            else:
                return self.cr

        def __s(cn):
            if self.s == 'cn/2':
                return cn / 2
            else:
                return self.s

        if self.s == '0.4*cn' and self.cr == '0.4*cn':
            for line, cn in enumerate(
                    drange(self.lower_bound_cn, self.upper_bound_cn,
                           self.step_size_cn)):
                for col, a in enumerate(
                        drange(self.lower_bound_a, self.upper_bound_a,
                               self.step_size_a)):
                    par_model_1 = Parameter(MODEL_NB,
                                            tau=self.tau,
                                            a=a,
                                            s=0.4 * cn,
                                            cn=cn)
                    par_model_2 = Parameter(MODEL_2_QUAD,
                                            tau=self.tau,
                                            a=a,
                                            s=0.4 * cn,
                                            cr=0.4 * cn,
                                            cn=cn,
                                            delta=self.delta)
                    yield (line, col, par_model_1, par_model_2)
        else:
            for line, cn in enumerate(
                    drange(self.lower_bound_cn, self.upper_bound_cn,
                           self.step_size_cn)):
                for col, a in enumerate(
                        drange(self.lower_bound_a, self.upper_bound_a,
                               self.step_size_a)):
                    par_model_1 = Parameter(MODEL_NB,
                                            tau=self.tau,
                                            a=a,
                                            s=__s(cn),
                                            cn=cn)
                    par_model_2 = Parameter(MODEL_2_QUAD,
                                            tau=self.tau,
                                            a=a,
                                            s=__s(cn),
                                            cr=__cr(cn),
                                            cn=cn,
                                            delta=self.delta)
                    yield (line, col, par_model_1, par_model_2)
Exemplo n.º 3
0
    def test_something(self):
        proxy = SolverProxy()
        proxy.read_or_calc_and_write()
        tau = 0.35
        delta = 0.85
        a = 0.001
        s = 0
        cr = 0.2125
        cn = 0.5
        par_nb = Parameter(MODEL_NB, tau=tau, a=a, s=s, cn=cn)
        sol_nb = ModelNBSolver.solve(par_nb, 'very high')
        print('wn = {}, b={}, rho={}, pn={}'.format(sol_nb.dec.wn,
                                                    sol_nb.dec.b,
                                                    sol_nb.dec.rho,
                                                    sol_nb.dec.pn))
        print(sol_nb.profit_man)
        print(sol_nb.profit_ret)

        par_o = Parameter(MODEL_2,
                          tau=tau,
                          a=a,
                          s=s,
                          cr=cr,
                          cn=cn,
                          delta=delta)
        sol_o = proxy.calculate(par_o)
        print(sol_o.profit_man)
Exemplo n.º 4
0
    def __calc__(self):
        p = Factorial
        iter = self.__state_iter
        proxy = SolverProxy()
        num = 0
        for tau, tau_state in iter([p.tau_lo(), p.tau_hi()]):
            for cn, cn_state in iter([p.cn_lo(), p.cn_hi()]):
                for s, s_state in iter([p.s_lo(), p.s_hi(cn)]):
                    for a, a_state in iter([p.a_lo(), p.a_hi()]):
                        # calculate model with restocking_fee
                        par_nb = Parameter(MODEL_NB, tau=tau, a=a, s=s, cn=cn)
                        sol_nb = proxy.read_or_calc_and_write(
                            par_nb, resolution='high')
                        num += 1
                        print(num)
                        #sol_nb = ModelNBSolver.solve(par_nb, 'low')
                        key_nb = self.__key(MODEL_NB, tau_state, a_state,
                                            s_state, Factorial.HIGH, cn_state,
                                            Factorial.HIGH)
                        self._add(key_nb, par_nb, sol_nb)

                        # calculate model without online store
                        par_no = Parameter(MODEL_1, tau=tau, a=a, s=s, cn=cn)
                        sol_no = proxy.calculate(par_no)
                        key_no = self.__key(MODEL_1, tau_state, a_state,
                                            s_state, Factorial.HIGH, cn_state,
                                            Factorial.HIGH)
                        self._add(key_no, par_no, sol_no)
        proxy.commit()
Exemplo n.º 5
0
 def __both_solutions(self, tau, a, s, cr, cn, delta):
     par_n = Parameter(MODEL_1, tau=tau, a=a, s=s, cn=cn)
     sol_n = self.__proxy.calculate(par_n)
     par_o = Parameter(MODEL_2,
                       tau=tau,
                       a=a,
                       s=s,
                       cr=cr,
                       cn=cn,
                       delta=delta)
     sol_o = self.__proxy.calculate(par_o)
     return (sol_n, sol_o)
Exemplo n.º 6
0
def test_db_and_plot():
    from matplotlib import pyplot as plt
    from solver import SolverProxy
    proxy = SolverProxy()
    a = [float(a) for a in np.linspace(0.0, 0.01, num=48 + 2)]
    x = np.zeros((len(a), 1))
    y = np.zeros((len(a), 1))
    for i, act_a in enumerate(a):
        if act_a == 0:
            x[0] = None
            y[0] = None
            continue
        par = Parameter(MODEL_2_QUAD, tau=0.09, a=act_a, s=0.04000000000000001, cn=0.1, cr=0.04000000000000001, delta=0.7956)
        #solP = ModelTwoQuadSolver.solve(par)
        solP = proxy.calculate(par, resolution='very-high')
        x[i] = solP.dec.wn
        y[i] = solP.profit_ret
        # plot actual a on x[i]
        #plt.text(act_a, x[i], str(act_a))
    proxy.commit()
    plt.plot(a, x, label='x', marker='o')
    plt.plot(a, y, label='y')
    plt.legend()
    #plt.savefig('figure_quad.pdf')
    plt.show()
Exemplo n.º 7
0
def plot_model_n_retailer(wn):
    par = Parameter(MODEL_1, tau=0.15, cn=0.1, s=0.5 * 0.1, a=0.005)
    NUM_PN = 10
    NUM_RHO = 100
    values = np.zeros([int(NUM_PN * NUM_RHO), 3])
    i = 0
    for pn in np.linspace(0, 1, NUM_PN):
        for rho in np.linspace(1, 20, NUM_RHO):
            profit = ModelOneNumericalSolver.get_retailer_profit(
                par, wn, pn, rho)
            values[i, :] = [pn, rho, profit]
            i += 1
    # plot
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_trisurf(values[:, 0], values[:, 1], values[:, 2])
    ax.set_xlabel('pn')
    ax.set_ylabel('rho')
    ax.set_title('Retailer\'s Profit,  $w_n$={}'.format(wn), loc='left')
    # plot optimum
    best_pn, best_rho = ModelOneNumericalSolver.get_retailer_decision(par, wn)
    best_profit = ModelOneNumericalSolver.get_retailer_profit(
        par, wn, best_pn, best_rho)
    latexlegend = r'${p_n}^*=$' + '{}'.format(
        best_pn) + r', ${\rho}^*=$' + '{:.2f}'.format(best_rho)
    ax.scatter(best_pn,
               best_rho,
               best_profit,
               values[:, 2],
               color='red',
               label=latexlegend)
    plt.legend()
    plt.show()
Exemplo n.º 8
0
 def test_optimize_instance_c(self):
     solver = ModelTwoNumericalSolver()
     par = Parameter(MODEL_2, tau=.0, a=.01, s=.0, cr=.0, cn=.1, delta=.8)
     sol = solver.optimize(par)
     self.assertIsNotNone(sol)
     self.assertAlmostEqual(sol.profit_man, 0.1687500000000)
     self.assertAlmostEqual(sol.profit_ret, 0.0281250000000)
Exemplo n.º 9
0
def plottiplotti():
    proxy = SolverProxy()
    a = [float(a) for a in np.linspace(0.0, 0.01, num=48 + 2)]
    x = np.zeros((len(a), 1))
    y = np.zeros((len(a), 1))
    dist = 0
    for i, act_a in enumerate(a):
        if act_a == 0:
            x[0] = None
            y[0] = None
            continue
        par = Parameter(MODEL_2,
                        tau=0.09,
                        a=act_a,
                        s=0.04000000000000001,
                        cn=0.1,
                        cr=0.04000000000000001,
                        delta=0.7956)
        solP = ModelTwoSolver.solve(par)
        solA = proxy.calculate(par)
        x[i] = solP.dec.wn
        y[i] = solA.dec.wn
        dist += abs(solP.dec.wn - solA.dec.wn)
    from matplotlib import pyplot as plt
    plt.plot(a, x, label='search')
    plt.plot(a, y, label='analy')
    plt.legend()
    plt.show()
    print(dist)
Exemplo n.º 10
0
 def test_optimize_instance_d(self):
     solver = ModelTwoNumericalSolver()
     # i dont know whether this parms lead to two a, but i will check the output anyway
     par = Parameter(MODEL_2, tau=.4, a=.01, s=.0, cr=.0, cn=.0, delta=.9)
     sol = solver.optimize(par)
     self.assertIsNotNone(sol)
     self.assertAlmostEqual(sol.profit_man, 0.1669565217391)
Exemplo n.º 11
0
 def test_write_and_read(self):
     proxy = SolverProxy()
     #par = Parameter(MODEL_2_QUAD, tau=.09, a=0.00146, s=.04, cr=.04, cn=.1, delta=.7956)
     par = Parameter(MODEL_1, tau=0.09, a=0.00146, s=.04, cn=.1)
     sol = proxy.read_or_calc_and_write(par, comment='unittest')
     proxy.commit()
     print(sol)
Exemplo n.º 12
0
def plot_model_n_manufacturer_both_cases():
    par = Parameter(MODEL_1, tau=0.15, cn=0.1, s=0.5 * 0.1, a=0.005)
    # print whole profit function of the manufacturer
    NUM_POINTS = 100
    all_wn = np.linspace(0, 1, NUM_POINTS)
    all_profit_one = np.zeros(NUM_POINTS)
    all_profit_two = np.zeros(NUM_POINTS)
    all_profit = np.zeros(NUM_POINTS)
    knee_point = -1
    for i, wn in enumerate(all_wn):
        all_profit_one[i] = ModelOneNumericalSolver.get_manufacturer_profit(
            par, wn, case=_CASE_ONE)
        all_profit_two[i] = ModelOneNumericalSolver.get_manufacturer_profit(
            par, wn, case=_CASE_TWO)
        all_profit[i] = ModelOneNumericalSolver.get_manufacturer_profit(
            par, wn)
        if all_profit_two[i] == all_profit[i] and knee_point < 0:
            knee_point = i
    plt.plot(all_wn, all_profit)
    plt.text(0.35, 0.085, r'$\rho > 1$', size=8, color='#1f77b4', rotation=38)
    plt.text(0.85, 0.048, r'$\rho = 1$', size=8, color='#1f77b4', rotation=-54)
    plt.plot(all_wn[10:knee_point],
             all_profit_two[10:knee_point],
             linestyle='--',
             color='grey')
    plt.text(0.30, 0.059, r'$\rho = 1$', size=8, color='grey', rotation=38)

    plt.title(
        r"Manufacturer's Profit $\tau=0.15, c_n =0.1, s=\frac{c_n}{2}, a=0.005$"
    )
    plt.xlim(0, 1)
    plt.ylim(np.nanmin(all_profit) * 1.1, np.nanmax(all_profit) * 1.1)
    plt.xlabel('$w_n$')
    plt.show()
Exemplo n.º 13
0
 def test_zwei(self):
     from solver import ModelTwoSolver, ModelNBSolver
     import numpy as np
     np.seterr(all='ignore')
     for a in np.linspace(0.024329896907216497, 0.024742268041237116, 10):
         par_nb = Parameter(MODEL_NB, tau=0.15, cn=0.1, s=0.5 * 0.1, a=a)
         sol = ModelNBSolver.solve(par_nb, resolution='case study')
         print(sol.profit_man, sol.profit_ret, sol.dec.wn, sol.dec.b,
               sol.case, sol.dec.rho)
Exemplo n.º 14
0
 def test_something(self):
     from solver import ModelTwoSolver
     par = Parameter(MODEL_2_QUAD,
                     tau=0.09,
                     a=0.0008163265306122449,
                     s=0.04000000000000001,
                     cn=0.1,
                     cr=0.04000000000000001,
                     delta=0.7956)
     ModelTwoSolver.solve(par)
Exemplo n.º 15
0
def test_ob_gleich():
    par_21 = Parameter(MODEL_2_QUAD, tau=0.09, a=0.004081632653061225, s=0.04000000000000001, cn=0.1, cr=0.04000000000000001, delta=0.7956)
    wn_pr_21 = (0.5429821819318537, 0.48640200062519534)
    wn_pr_20 = (0.5461081587996248, 0.4885901844326352)
    # now test with wn,pr combination of par
    sol_with_21 = ModelTwoQuadSolver.getSolution(par_21, wn_pr_21[0], wn_pr_21[1])
    sol_with_20 = ModelTwoQuadSolver.getSolution(par_21, wn_pr_20[0], wn_pr_20[0])
    assert sol_with_21.profit_man == 0.15756762319737758
    print(sol_with_21.profit_man)
    print(sol_with_20.profit_man)
Exemplo n.º 16
0
def case_quad_fixed_a():
    cn = np.linspace(0.0, 0.9, num=100)
    y = []
    for act_cn in np.linspace(0.0, 0.9, num=100):
        act_cn = float(act_cn)
        par = Parameter(MODEL_2_QUAD, tau=0.09, a=.4, s=.1, cr=.2, cn=act_cn, delta=.4)
        sol = ModelTwoQuadSolver.solve(par, 'low')
        y.append(sol.dec.qr)
    plt.plot(cn, y)
    print(y)
    plt.show()
Exemplo n.º 17
0
def main():
    par = Parameter(MODEL_2_QUAD, tau=0.09, a=0.0038775510204081634, s=0.04000000000000001, cn=0.1, cr=0.04000000000000001, delta=0.7956)
    # find any start vector of case 1
    startP = ModelTwoQuadGridSearch.search(par, iter=1, case=1)
    start_vec = [startP.dec.wn, startP.dec.pr]
    # improve wn, pr
    result = minimize(ModelTwoQuadSolver._minimize_func, args={'par': par, 'case': 1}, x0=start_vec, method='Nelder-Mead', options={'xatol': 0.000000001, 'fatol': 0.000000001})
    # build a new solution using wn, pr
    solP = ModelTwoQuadSolver.getSolution(par, float(result.x[0]), float(result.x[1]))
    print(startP.profit_man, startP.dec.wn, startP.dec.pr, startP.dec.rho)
    print(solP.profit_man, solP.dec.wn, solP.dec.pr, solP.dec.rho)
Exemplo n.º 18
0
 def __model_one_par_generator(self):
     """ Helper generator for yielding all combinations of input data for model 2"""
     for tau in drange(0, 1, 0.1):
         for a in drange(0.01, 0.1, 0.01):
             for s in drange(0, 1, .1):
                 for cn in drange(s, 1, .1):
                     yield Parameter(MODEL_1,
                                     tau=round(tau, 1),
                                     a=round(a, 2),
                                     s=round(s, 1),
                                     cn=round(cn, 1))
Exemplo n.º 19
0
 def test_case_1_or_2(self):
     solver = ModelOneNumericalSolver()
     par = Parameter(MODEL_1, tau=0.3, a=0.01, s=0, cn=0.3)
     # self checking if input vars not in case 1 and not in case 2:
     self.assertTrue(
         self.__input_is_in_case_1(par) and self.__input_is_in_case_2(par))
     sol = solver.optimize(par)
     self.assertAlmostEqual(
         sol.profit_ret, 0.01428571
     )  # would be case 2 solution, because is higher than case 1 solution
     self.assertAlmostEqual(sol.profit_man, 0.02857143)
Exemplo n.º 20
0
 def test_always_case_one(self):
     from solver import ModelTwoQuadSolver
     par = Parameter(MODEL_2_QUAD,
                     tau=0.3,
                     a=0.0005,
                     s=0.07,
                     cn=0.67,
                     cr=0.1,
                     delta=0.3)
     sol = ModelTwoQuadSolver.solve(par, resolution='super high')
     print(sol.profit_man, sol.profit_ret)
     print(sol)
Exemplo n.º 21
0
 def test_right_case(self):
     solver = ModelTwoNumericalSolver()
     #a = np.
     par = Parameter(MODEL_2,
                     tau=.09,
                     a=0.00146,
                     s=.04,
                     cr=.04,
                     cn=.1,
                     delta=.7956)
     sol = solver.optimize(par)
     self.assertAlmostEqual(sol.profit_man, 0.1582925507399)
Exemplo n.º 22
0
def case_model_nb_fixed_cn():
    a = np.linspace(0.001, 0.02, num=10)
    y = []
    for act_a in a:
        act_a = float(act_a)
        par = Parameter(MODEL_2_QUAD, tau=0.09, a=act_a, s=0.4*0.1, cr=0.4*0.1, cn=0.1, delta=.7956)
        sol = ModelNBSolver.solve(par, 'low')
        print(act_a)
        y.append(sol.dec.b)
        y.append()
    plt.plot(a, y)
    plt.show()
Exemplo n.º 23
0
 def test_something(self):
     search = ModelTwoGridSearch()
     par = Parameter(MODEL_2_QUAD,
                     tau=0.09,
                     a=0.0008163265306122449,
                     s=0.04000000000000001,
                     cn=0.1,
                     cr=0.04000000000000001,
                     delta=0.7956)
     sol = search.search(par)
     print(sol.profit_man, sol.dec.wn, sol.dec.pr)
     self.assertIsNotNone(sol)
Exemplo n.º 24
0
    def test_case_1a(self):
        solver = ModelOneNumericalSolver()
        # this parameter should lead to case one (rho is gte 1)
        par = Parameter(MODEL_1, tau=0.1, a=0.005, s=0.0005, cn=0.01)
        # self checking the my test input variables..
        self.assertTrue(self.__input_is_in_case_1(par))

        analyitcal_profit_manufacturer = (
            (1 - par.cn)**2 / 8) - (1 / 2) * (1 + par.cn - 2 * par.s) * (
                par.tau * par.a)**(1 / 2) + (1 / 2) * par.a * par.tau
        sol = solver.optimize(par)
        self.assertAlmostEqual(analyitcal_profit_manufacturer, sol.profit_man)
Exemplo n.º 25
0
 def test_something(self):
     from solver import ModelTwoSolver
     par = Parameter(MODEL_2_QUAD,
                     tau=0.09,
                     a=0.0020408163265306124,
                     s=0.04000000000000001,
                     cn=0.1,
                     cr=0.04000000000000001,
                     delta=0.7956)
     sol = ModelTwoSolver.solve(par)
     print(sol)
     print(ModelTwoSolver.solve_analytical(par))
Exemplo n.º 26
0
    def __calc__(self):
        p = Factorial
        iter = self.__state_iter
        proxy = SolverProxy()

        for tau, tau_state in iter(p.tau_lh()):
            for delta, delta_state in iter(p.delta_lh()):
                for cn, cn_state in iter(p.cn_lh()):
                    for cr, cr_state in iter(p.cr_lh(cn, delta)):
                        for s, s_state in iter(p.s_lh(cn)):
                            for a, a_state in iter(p.a_lh()):
                                # calculate model one
                                par_n = Parameter(MODEL_1, tau=tau, a=a, s=s, cn=cn)
                                sol_n = proxy.calculate(par_n)
                                key_n = self.__key(MODEL_1, tau_state, a_state, s_state, cr_state, cn_state, delta_state)
                                self._add(key_n, par_n, sol_n)

                                # calculate model two
                                par_o = Parameter(MODEL_2, tau=tau, a=a, s=s, cr=cr, cn=cn, delta=delta)
                                sol_o = proxy.calculate(par_o)
                                key_o = self.__key(MODEL_2, tau_state, a_state, s_state, cr_state, cn_state, delta_state)
                                self._add(key_o, par_o, sol_o)
Exemplo n.º 27
0
    def test_case_2_dec_vars(self):
        solver = ModelOneNumericalSolver()
        # this args should lead to case two (rho is equal to 1)
        par = Parameter(MODEL_1, tau=0.1, a=0.006, s=0.005, cn=0.3)
        # self checking the test input variables
        self.assertTrue(self.__input_is_in_case_2(par))

        sol = solver.optimize(par)
        self.assertAlmostEqual(sol.dec.pn, 0.83319444, msg='pn not the same')
        self.assertAlmostEqual(sol.dec.wn, (1 / (1 - par.tau)) *
                               ((1 + par.cn) / 2 - (par.tau *
                                                    (1 + par.s)) / 2),
                               msg='wn not the same')
        self.assertAlmostEqual(sol.dec.rho, 1.0, msg='rho not the same')
Exemplo n.º 28
0
    def test_blub(self):
        from solver import ModelTwoSolver, ModelNBSolver
        import numpy as np
        np.seterr(all='ignore')
        #par_nb = Parameter(MODEL_NB, tau=0.15, a=0.001, s=0, cn=0.1)
        par_nb = Parameter(MODEL_NB,
                           tau=0.15,
                           cn=0.1,
                           s=0.5 * 0.1,
                           a=0.024329896907216497)

        sol = ModelNBSolver.solve(par_nb, resolution='high')
        print(sol.profit_man, sol.profit_ret, sol.dec.wn, sol.dec.b, sol.case,
              sol.dec.rho)
Exemplo n.º 29
0
    def test_case_2a(self):
        solver = ModelOneNumericalSolver()
        # this args should lead to case two (rho is equal to 1)
        par = Parameter(MODEL_1, tau=0.1, a=0.006, s=0.005, cn=0.3)
        # self checking the test input variables..
        # if the following condition is true, it must lead to a case b optimization
        self.assertTrue(
            self.__input_is_in_case_2(par)
            and not self.__input_is_in_case_1(par))

        analyitcal_profit_manufacturer = (
            (1 - par.cn - par.tau + par.s * par.tau)**2) / (8 * (1 - par.tau))
        sol = solver.optimize(par)
        self.assertAlmostEqual(analyitcal_profit_manufacturer, sol.profit_man)
Exemplo n.º 30
0
 def test_instance_a(self):
     solver = ModelTwoNumericalSolver()
     par = Parameter(MODEL_2,
                     tau=0.15,
                     a=0.0013471502590673575,
                     s=0.05,
                     cr=0.01,
                     cn=0.1,
                     delta=0.85)
     dec = solver._optimize_case_one_c(par)
     profit_man, profit_ret = solver.calc_profits(par, dec)
     sol = Solution(dec, profit_man, profit_ret, '1c')
     print(dec.qr - (par.tau / dec.rho) * dec.qn)
     print(solver._is_valid(par, sol))
     print(profit_man)