Пример #1
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()
Пример #2
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)
Пример #3
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)
Пример #4
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')
Пример #5
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)
Пример #6
0
    def test_model_1_compare_analytical(self):
        mof = MemoryOutputFile()
        generator = Generator(MODEL_1, mof)
        solver = ModelOneNumericalSolver()
        ana_solver = AnalyticalSolver()

        generator.generate()
        for solution in mof.getSolutions():
            par, sol = solution['par'], solution['sol']
            assert par != None
            dec_vars, prof_man, prof_ret = ana_solver.calcModelOne(par)
            if dec_vars == None:
                self.assertIsNone(prof_man)
                self.assertIsNone(prof_ret)
                self.assertIsNone(sol)
            elif sol == None:  # solver says no solution
                if dec_vars != None:
                    print(dec_vars)
                self.assertIsNone(dec_vars)
                self.assertIsNone(prof_man)
                self.assertIsNone(prof_ret)
            else:
                self.assertAlmostEqual(sol.dec.pn, dec_vars.pn)
                self.assertAlmostEqual(sol.dec.wn, dec_vars.wn)
                self.assertAlmostEqual(sol.dec.rho, dec_vars.rho)
                self.assertAlmostEqual(sol.dec.qn, dec_vars.qn)
                self.assertAlmostEqual(sol.profit_man, prof_man)
                self.assertAlmostEqual(sol.profit_ret, prof_ret)
Пример #7
0
 def calc(self):
     self.nr_cols = int(
         (self.upper_bound_a - self.lower_bound_a) / self.step_size_a) + 1
     self.nr_lines = int((self.upper_bound_cn - self.lower_bound_cn) /
                         self.step_size_cn) + 1
     self.matrix = np.zeros([self.nr_lines, self.nr_cols])
     solver_m1, solver_m2 = ModelOneNumericalSolver(
     ), ModelTwoNumericalSolver()
     i = 0
     numall = self.nr_cols * self.nr_lines
     print(numall)
     #self.proxy.beginWrite()
     for line, col, par_model_1, par_model_2 in self._a_cn_generator():
         # calc solutions
         if par_model_1.a == .0:
             sol_model_1, sol_model_2 = None, None
         else:
             #sol_model_1 = solver_m1.optimize(par_model_1)
             #sol_model_2 = solver_m2.optimize(par_model_2)
             #sol_model_1 = self.proxy.read_or_calc_and_write(par_model_1, resolution='low')
             sol_model_1 = self.proxy.calculate(par_model_1,
                                                resolution='low')
             #sol_model_2 = self.proxy.read_or_calc_and_write(par_model_2, resolution='low')
             #sol_model_2 = self.proxy.calculate(par_model_2, resolution='low')
         self.matrix[line, col] = self._calc_func(sol_model_1, None,
                                                  par_model_2)
         if i % 100 == 0:
             self.proxy.commit()
             print('{} ({:.2f} %)    at {}'.format(
                 i, (i / numall) * 100, str(datetime.datetime.now())))
         i += 1
     self.proxy.commit()
Пример #8
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()
Пример #9
0
    def test_case_1a_dec_vars(self):
        solver = ModelOneNumericalSolver()
        # this args 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 test input variables..
        # if the following condition is true, it must lead to a case a optimization
        self.assertTrue(
            self.__input_is_in_case_1(par)
            and not self.__input_is_in_case_2(par))

        sol = solver.optimize(par)
        self.assertAlmostEqual(sol.dec.pn, (3 + par.cn) / 4 - (1 / 2) *
                               (par.a * par.tau)**(1 / 2),
                               msg='pn not the same')
        self.assertAlmostEqual(sol.dec.wn,
                               (1 + par.cn) / 2 - (par.a * par.tau)**(1 / 2),
                               msg='wn not the same')
        self.assertAlmostEqual(sol.dec.rho,
                               par.tau / 2 + (1 - par.cn) / 4 *
                               (par.tau / par.a)**(1 / 2),
                               msg='rho not the same')
Пример #10
0
def plot_model_n_manufacturer(showLine=True, points=[], pointText=True):
    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 = np.zeros(NUM_POINTS)
    for i, wn in enumerate(all_wn):
        profit = ModelOneNumericalSolver.get_manufacturer_profit(par, wn)
        all_profit[i] = profit
    if showLine:
        plt.plot(all_wn, all_profit)

    # plot points
    if len(points) > 0:
        for wn in points:
            profit = ModelOneNumericalSolver.get_manufacturer_profit(par, wn)
            plt.plot(wn, profit, marker='o', color='red')
            pn, rho = ModelOneNumericalSolver.get_retailer_decision(par, wn)
            latextext = r'${p_n}^*=$' + '{}'.format(
                pn) + r', ${\rho}^*=$' + '{:.2f}'.format(rho)
            if pointText: plt.text(wn, profit - 0.01, latextext, size=8)
    # knee?
    #plt.annotate('knee?', xy=(.63, .081), xytext=(.43, .05),
    #        arrowprops=dict(facecolor='black', shrink=0.05),
    #        )

    #plt.text(0.25, 0.05, '$w_n$=0.2')
    #plt.text(0.25, 0.04, r'${p_n}^*(w_n=0.2)= ?$')
    #plt.text(0.25, 0.03, r'${\rho}^*(w_n=0.2)= ?$')
    #plt.vlines(0.2, -1, 1, linestyle='--', color='grey', alpha=.5)
    #plt.yticks([])
    #plt.ylabel(r'profit($w_n, {p_n}^*, {\rho}^*$)')

    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()
Пример #11
0
def plot_model_n_rhos():
    # calculate 50 instances:
    NUM_POINTS = 50
    a_values = np.linspace(0.0075, 0.0085, NUM_POINTS)
    rho_values = np.zeros(NUM_POINTS)
    for i, a in enumerate(a_values):
        par = Parameter(MODEL_1, tau=0.15, cn=0.1, s=0.5 * 0.1, a=a)
        solution = ModelOneNumericalSolver.solve(par)
        rho_values[i] = solution.dec.rho
    # plot a curve, where rho is a function of a:
    plt.plot(a_values, rho_values, marker='o', markersize=4, label=r'$\rho$')
    plt.legend()
    plt.xlabel('a (costs of effort)')
    plt.ylabel(r'$\rho$ (effort)')
    plt.show()
Пример #12
0
 def test_qn(self):
     solver = ModelOneNumericalSolver()
     # this args 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)
     sol = solver.optimize(par)
     self.assertAlmostEqual(sol.dec.qn, 1 - sol.dec.pn)
Пример #13
0
 def test_sol_not_possible(self):
     solver = ModelOneNumericalSolver()
     par = Parameter(MODEL_1, tau=1, a=0.01, s=0, cn=0.8)
     self.assertIsNone(solver.optimize(par))