def total_cost(self, p=None, p_cost=None, pcost_model=None):
        p = self.p if p is None else p
        p_cost = self.p_cost if p_cost is None else p_cost
        pcost_model = self.pcost_model if pcost_model is None else pcost_model
        p = 0.0 if not self.online else p
        if pcost_model == PW_LINEAR:
            n_segments = len(p_cost) - 1
            for i in range(n_segments):
                x1, y1 = p_cost[i]
                x2, y2 = p_cost[i + 1]
                m = (y2 - y1) / (x2 - x1)
                c = y1 - m * x1
                if x1 <= p <= x2:
                    result = m*p + c
                    break
            else:
                logger.error("Value [%f] outside pwl cost curve [%s]." %
                             (p, p_cost[-1][0]))
                result = m*p + c
        elif pcost_model == POLYNOMIAL:
            result = polyval(p_cost, p)
        else:
            raise ValueError
        if self.is_load:
            return -result
        else:
            return result