Exemplo n.º 1
0
 def ml_cost(self, pos_v, neg_v):
     """
     Variational approximation to the maximum likelihood positive phase.
     :param v: T.matrix of shape (batch_size, n_v), training examples
     :return: tuple (cost, gradient)
     """
     pos_cost = T.mean(self.free_energy(pos_v))
     neg_cost = T.mean(self.free_energy(neg_v))
     cost = pos_cost - neg_cost
     return costmod.Cost(cost, self.params(), [pos_v, neg_v])
Exemplo n.º 2
0
 def __init__(self, task, name=None, cost_obj=None):
     assert isinstance(task, Task)
     assert isinstance(cost_obj, cost.Cost)
     self._this_task = task  # this route belongs to task
     self._name = name
     self._cost = cost_obj if task.line_expense is None else \
         cost.Cost(cost_obj.distance, task.line_expense, cost_obj.duration)
     self._expected_end_time = \
         self._this_task.expected_start_time + self._this_task.no_run_time + self._cost.duration
     self._next_steps = list()  # next reachable steps for sorting in future
     self._profit = None
     self._max_profit = None
Exemplo n.º 3
0
 def __init__(self, name, avl_loc=None, avl_time=0, candidate_num_limit=10):
     self._name = name
     self._avl_loc = avl_loc
     self._avl_time = avl_time  # hours
     self._candidate_plans_sorted = list()
     self._candidate_num_limit = candidate_num_limit
     # record all possible plans with a virtual route, a self-loop route
     self._start_route = Route(task=Task(loc_start=self._avl_loc,
                                         loc_end=self._avl_loc,
                                         start_time=self._avl_time,
                                         occur_prob=1.0,
                                         is_virtual=1,
                                         name=self._name),
                               name=None,
                               cost_obj=cost.Cost())
Exemplo n.º 4
0
    def get_reg_cost(self, l2=None, l1=None):
        """
        Builds the symbolic expression corresponding to first-order gradient descent
        of the cost function ``cost'', with some amount of regularization defined by the other
        parameters.
        :param l2: dict whose values represent amount of L2 regularization to apply to
        parameter specified by key.
        :param l1: idem for l1.
        """
        cost = T.zeros((), dtype=floatX)
        params = []

        for p in self.params():

            if l1.get(p.name, 0):
                cost += l1[p.name] * T.sum(abs(p))
                params += [p]

            if l2.get(p.name, 0):
                cost += l2[p.name] * T.sum(p**2)
                params += [p]

        return costmod.Cost(cost, params)
Exemplo n.º 5
0
def init(s_cost):

    if (s_cost == 'parabola1d'):
        bounds = np.array([[0.0, 1.0]])
        maxmin = 1
        tol = 1e-6
        fCST = fnc_cost.parabola1d
    elif (s_cost == 'parabola2d'):
        bounds = np.array([[-1.0, 1.0], [-1.0, 1.0]])
        maxmin = 1
        tol = 1e-8
        fCST = fnc_cost.parabola2d
    elif (s_cost == 'sphere'):
        bounds = np.array([[-1.0, 1.0], [-1.0, 1.0], [-1.0, 1.0]])
        maxmin = 1
        tol = 1e-6
        fCST = fnc_cost.sphere
    elif (s_cost == 'charbonneau1'):
        bounds = np.array([[0.0, 1.0], [0.0, 1.0]])
        maxmin = -1
        tol = 1e-6
        fCST = fnc_cost.charbonneau1
    elif (s_cost == 'charbonneau2'):
        bounds = np.array([[0.0, 1.0], [0.0, 1.0]])
        maxmin = -1
        tol = 1e-8
        fCST = fnc_cost.charbonneau2
    elif (s_cost == 'charbonneau4'):
        bounds = np.array([[0.0, 2.0], [-1.0, 1.0], [0.01, 1.0], [0.0, 2.0],
                           [-1.0, 1.0], [0.001, 1.0]])
        maxmin = 1
        tol = 1e-8
        fCST = fnc_cost.charbonneau4
    else:
        raise Exception('[init]: invalid s_prob %s')

    return cost.Cost(fCST, bounds, tol, maxmin)