def create_actions(self): for c in self.counters: # counter actions # inc(c,1) precondition = [(self.counter_values[c][0].index, True), (self.counter_values[c][1].index, False)] effect = [(self.counter_values[c][1].index, True)] a = Action('inc-{0}-{1}'.format(c, 1), precondition, [], effect) self.actions.append(a) #inc(c,k) for k in xrange(2, self.max_value): precondition = [(self.counter_values[c][k - 1].index, True), (self.counter_values[c][k].index, False)] effect = [(self.counter_values[c][k].index, True)] a = Action('inc-{0}-{1}'.format(c, k), precondition, [], effect) self.actions.append(a) #dec(c,max) precondition = [(self.counter_values[c][self.max_value - 1].index, True)] effect = [(self.counter_values[c][self.max_value - 1].index, False) ] a = Action('dec-{0}-{1}'.format(c, self.max_value), precondition, [], effect) self.actions.append(a) #dec(c,k) for k in xrange(self.max_value - 2, 0, -1): precondition = [(self.counter_values[c][k + 1].index, False), (self.counter_values[c][k].index, True)] effect = [(self.counter_values[c][k].index, False)] a = Action('dec-{0}-{1}'.format(c, k), precondition, [], effect) self.actions.append(a)
def __make_pickup_action_4(self, block, cylinder, lp=None): name = 'pickup_block_%d_from_cylinder_%d' % (block, cylinder) prec = [(self.block_clear[block].index, True), (self.block_position[block].index, self.block_position_values['on_piston']), (self.block_container[block].index, self.block_container_values[in_cylinder(cylinder)]), (self.gripper_empty.index, True)] eff = [(self.block_clear[block].index, False), (self.gripper_empty.index, False), (self.cylinder_empty[cylinder].index, True), (self.block_position[block].index, self.block_position_values['hand']), (self.block_container[block].index, self.block_container_values['none'])] if self.sdac and lp != None: o_f = LinExpr(lp.column_heights[cylinder]) #cylinder height of_varname = lp.column_heights[cylinder].getAttr("VarName") else: o_f = None of_varname = None return Action(name, prec, [], eff, objective_function=o_f, of_vname=of_varname)
def create_actions(self): self.actions = [] for branch in self.network.branches: self.create_open_action(branch) self.create_close_action(branch) # TODO: See ../README - Settle on what is right. # if branch.start_closed and not branch.final_closed: # self.create_open_action(branch) # elif not branch.start_closed and branch.final_closed: # self.create_close_action(branch) #Eend action here. end_preconditions = [(self.plan_end_var.index, False)] end_effects = [(self.plan_end_var.index, True)] action_constant = 1 end_conditional_costs = [] constant = 2**len(self.network.branches) for bus in self.network.buses: if self.sdac_type == "Objective": action_constant = constant elif sdac == "Valuation": end_conditional_costs.append( ConditionalCost(secondary_condition=[ (bus.secondary_variable_i, False) ], ccost=constant * bus.p)) self.actions.append( Action("End_plan", end_preconditions, [], end_effects, conditional_costs=end_conditional_costs, of_vname=self.objective_function, ep_constant=action_constant))
def __make_drive_action(self, vname, pkc, lfrom, lto): name = 'drive_{0}_{1}_to_{2}'.format(vname, lfrom, lto) prec = [(self.at[vname].index, lfrom)] if LinehaulTask.SYMMETRY_BREAKING: prev = None for (vn, _, _, _) in self.vehicles: if vn == vname: break else: prev = vn depot = self.locations[0] home = depot + "*" if prev != None and lfrom == depot: sb_prec = [(self.at[prev].index, home)] prec += sb_prec if lfrom == depot and lto == home: vtype = self.vehicle_type[vname] sb_prec = [(self.vehicle_type_used[vtype].index, False)] prec += sb_prec eff = [(self.at[vname].index, lto)] if lto not in self.locations: lto = self.locations[0] if lto != self.locations[0]: eff += [(self.visited[(vname, lto)].index, True)] if LinehaulTask.SYMMETRY_BREAKING: if lfrom == self.locations[0] and lto != lfrom: vtype = self.vehicle_type[vname] sb_eff = [(self.vehicle_type_used[vtype].index, True)] eff += sb_eff dist = self.distance_matrix[(lfrom, lto)] cost = math.trunc((dist / 100.0) * pkc) #print( 'action: {0} cost: {1}'.format( name, cost ) ) return Action(name, prec, [], eff, cost)
def __make_unstack_stack_action(self, top_block, bot_block, dest_block, cylinder, dest_cylinder): name = "move_block_" + format(top_block) + "_from_block_" + format( bot_block) + "_in_cylinder_" + format( cylinder) + "_to_block_{0}_in_cylinder_{1}".format( dest_block, dest_cylinder) pre = [(self.block_clear[top_block].index, True), (self.block_clear[dest_block].index, True), (self.block_in_cylinder[(top_block, cylinder)].index, True), (self.block_in_cylinder[(bot_block, cylinder)].index, True), (self.block_in_cylinder[(dest_block, dest_cylinder)].index, True), (self.block_on_block[(top_block, bot_block)].index, True)] eff = [(self.block_in_cylinder[(top_block, cylinder)].index, False), (self.block_in_cylinder[(top_block, dest_cylinder)].index, True), (self.block_on_block[(top_block, bot_block)].index, False), (self.block_on_block[(top_block, dest_block)].index, True), (self.block_clear[bot_block].index, True), (self.block_clear[dest_block].index, False)] return Action(name, pre, [], eff)
def __make_unstack_putdown_action(self, top_block, bot_block, from_cylinder, cylinder): name = "move_block_" + format(top_block) + "_from_block_" + format( bot_block) + "_in_cylinder_" + format( from_cylinder) + "_to_piston_in_cylinder_{0}".format(cylinder) pre = [ (self.cylinder_empty[cylinder].index, True), (self.block_clear[top_block].index, True), (self.block_in_cylinder[(top_block, from_cylinder)].index, True), (self.block_in_cylinder[(bot_block, from_cylinder)].index, True), (self.block_on_block[(top_block, bot_block)].index, True) ] # now effects eff = [(self.block_on_piston[top_block].index, True), (self.block_in_cylinder[(top_block, cylinder)].index, True), (self.cylinder_empty[cylinder].index, False), (self.block_clear[bot_block].index, True), (self.block_in_cylinder[(top_block, from_cylinder)].index, False), (self.block_on_block[(top_block, bot_block)].index, False)] return Action(name, pre, [], eff)
def __make_stack_action_4(self, top_block, bot_block, cylinder, lp=None): name = 'stack_block_%d_onto_block_%d_in_cylinder_%d' % ( top_block, bot_block, cylinder) prec = [(self.block_position[top_block].index, self.block_position_values['hand']), (self.block_container[bot_block].index, self.block_container_values[in_cylinder(cylinder)]), (self.block_clear[bot_block].index, True)] eff = [(self.block_clear[top_block].index, True), (self.gripper_empty.index, True), (self.block_position[top_block].index, self.block_position_values[on_block(bot_block)]), (self.block_container[top_block].index, self.block_container_values[in_cylinder(cylinder)]), (self.block_clear[bot_block].index, False)] if self.sdac and lp != None: o_f = LinExpr(lp.column_heights[cylinder]) #cylinder height of_varname = lp.column_heights[cylinder].getAttr("VarName") else: o_f = None of_varname = None return Action(name, prec, [], eff, objective_function=o_f, of_vname=of_varname)
def create_actions( self ) : self.inc_actions_by_counter_and_value = {} self.dec_actions_by_counter_and_value = {} for x in self.counter_values : for v in range( 0, self.max_value ) : precondition = [ ( x.index, v ) ] effects = [ (x.index, v+1) ] a = Action( 'inc-{0}-{1}'.format( x.name, v ), precondition, [], effects ) self.actions.append( a ) self.inc_actions_by_counter_and_value[ ( x.name, v ) ] = a for v in range( 1, self.max_value + 1 ) : precondition = [ ( x.index, v ) ] effects = [ ( x.index, v-1) ] a = Action( 'dec-{0}-{1}'.format( x.name, v ), precondition, [], effects ) self.actions.append( a ) self.dec_actions_by_counter_and_value[ ( x.name, v ) ] = a
def create_close_action(self, branch): name = 'Close_{0}'.format(branch.name) preconditions = [(branch.closed.index, False)] effects = [(branch.closed.index, True)] #print name, " conditional costs: ", self.conditional_costs self.actions.append( Action(name, preconditions, [], effects, cost=0.1, conditional_costs=self.conditional_costs, of_vname=self.objective_function))
def __make_putdown_action_4(self, block, cylinder): name = 'putdown_block_%d_onto_cylinder_%d' % (block, cylinder) prec = [(self.cylinder_empty[cylinder].index, True), (self.block_in_gripper[block].index, True)] eff = [(self.block_clear[block].index, True), (self.block_on_piston[block].index, True), (self.block_in_cylinder[(block, cylinder)].index, True), (self.gripper_empty.index, True), (self.cylinder_empty[cylinder].index, False), (self.block_in_gripper[block].index, False)] return Action(name, prec, [], eff)
def project_primary_over_vars(task, G, variable_set): variable_map = {} inv_variable_map = {} action_map = {} inv_action_map = {} # create projected primary task projected_primary = Task(task.domain_name, task.instance_name) # re-map primary task variables for new_index, old_index in enumerate(variable_set): variable_map[old_index] = new_index inv_variable_map[new_index] = old_index x = task.state_vars[old_index] projected_primary.create_state_var(x.name, x.domain, x.default_value) # re-map planning task actions for old_index, action in enumerate(task.actions): new_index = len(projected_primary.actions) new_primary_precs = [] for x, v in action.prim_precs: if x not in variable_map: continue new_primary_precs.append((variable_map[x], v)) new_effect = [] for x, v in action.effect: if x not in variable_map: continue new_effect.append((variable_map[x], v)) if len(new_effect) == 0: continue projected_primary.actions.append( Action(action.name, new_primary_precs, action.sec_precs, new_effect, action.cost)) action_map[old_index] = new_index inv_action_map[new_index] = old_index #print( 'Projected task created' ) #projected_primary.print_statistics( sys.stdout ) projected_G = [] for x, v in G: if x not in variable_map: continue projected_G.append((variable_map[x], v)) return projected_primary, projected_G, (variable_map, inv_variable_map), (action_map, inv_action_map)
def __make_putdown_action_4(self, block, cylinder): name = 'putdown_block_%d_onto_cylinder_%d' % (block, cylinder) prec = [(self.cylinder_empty[cylinder].index, True), (self.block_position[block].index, self.block_position_values['hand'])] eff = [(self.block_clear[block].index, True), (self.gripper_empty.index, True), (self.cylinder_empty[cylinder].index, False), (self.block_position[block].index, self.block_position_values['on_piston']), (self.block_container[block].index, self.block_container_values[in_cylinder(cylinder)])] return Action(name, prec, [], eff)
def __make_pickup_putdown_action(self, block, from_cylinder, cylinder): name = "move_block_" + format( block) + "_from_piston" + "_in_cylinder_" + format( from_cylinder) + "_to_piston_in_cylinder_{0}".format(cylinder) pre = [(self.cylinder_empty[cylinder].index, True), (self.block_clear[block].index, True), (self.block_in_cylinder[(block, from_cylinder)].index, True), (self.block_on_piston[block].index, True)] eff = [(self.block_in_cylinder[(block, cylinder)].index, True), (self.block_in_cylinder[(block, from_cylinder)].index, False), (self.cylinder_empty[cylinder].index, False), (self.cylinder_empty[from_cylinder].index, True)] return Action(name, pre, [], eff)
def __make_stack_action_4(self, top_block, bot_block, cylinder): name = 'stack_block_%d_onto_block_%d_in_cylinder_%d' % ( top_block, bot_block, cylinder) prec = [(self.block_in_gripper[top_block].index, True), (self.block_clear[bot_block].index, True), (self.block_in_cylinder[(bot_block, cylinder)].index, True)] eff = [(self.block_clear[top_block].index, True), (self.block_in_cylinder[(top_block, cylinder)].index, True), (self.gripper_empty.index, True), (self.block_on_block[(top_block, bot_block)].index, True), (self.block_in_gripper[top_block].index, False), (self.block_clear[bot_block].index, False)] return Action(name, prec, [], eff)
def __make_stack_action_4(self, top_block, bot_block, cylinder): name = 'stack_block_%d_onto_block_%d_in_cylinder_%d' % ( top_block, bot_block, cylinder) prec = [(self.block_position[top_block].index, self.block_position_values['hand']), (self.block_container[bot_block].index, self.block_container_values[in_cylinder(cylinder)]), (self.block_clear[bot_block].index, True)] eff = [(self.block_clear[top_block].index, True), (self.gripper_empty.index, True), (self.block_position[top_block].index, self.block_position_values[on_block(bot_block)]), (self.block_container[top_block].index, self.block_container_values[in_cylinder(cylinder)]), (self.block_clear[bot_block].index, False)] return Action(name, prec, [], eff)
def create_close_action(self, branch): name = 'Close_{0}'.format(branch.name) preconditions = [(branch.closed.index, False)] effects = [(branch.closed.index, True)] self.actions.append(Action(name, preconditions, [], effects))
def create_close_action(self, device): name = 'Close_{0}'.format(device.name) precondition = [(device.closed.index, False)] effects = [(device.closed.index, True)] self.actions.append(Action(name, precondition, [], effects))
def create_open_action(self, device): # Open action name = 'Open_{0}'.format(device.name) preconditions = [(device.closed.index, True)] effects = [(device.closed.index, False)] self.actions.append(Action(name, preconditions, [], effects))
def project_primary_over_vars(task, G, variable_set): variable_map = {} inv_variable_map = {} action_map = {} inv_action_map = {} # create projected primary task projected_primary = Task(task.domain_name, task.instance_name) #print("Projecting primary") # re-map primary task variables for new_index, old_index in enumerate(variable_set): variable_map[old_index] = new_index inv_variable_map[new_index] = old_index x = task.state_vars[old_index] projected_primary.create_state_var(x.name, x.domain, x.default_value) #print("Name: ", x.name, " Old index: ",old_index , " New index", new_index) #print("Done with primary. Actions now.") # re-map planning task actions for old_index, action in enumerate(task.actions): new_index = len(projected_primary.actions) new_primary_precs = [] for x, v in action.prim_precs: if x not in variable_map: continue new_primary_precs.append((variable_map[x], v)) new_effect = [] for x, v in action.effect: if x not in variable_map: continue new_effect.append((variable_map[x], v)) if len(new_effect) == 0: continue new_conditional_costs = [] for cc in action.conditional_costs: new_primary_condition = [] for x, v in cc.primary_conditions: if x not in variable_map: print("x not in variable map:", x) continue new_primary_condition.append((variable_map[x], v)) new_secondary_condition = [] if len(new_primary_condition) == 0 and len( new_secondary_condition) == 0: print("No primary or secondary conditions") continue new_conditional_costs.append( ConditionalCost(primary_condition=new_primary_condition, secondary_condition=new_secondary_condition, ccost=cc.ccost)) #print("variable_map is ", variable_map) #print("conditional_costs are ") #for cc in action.conditional_costs : #print("conditions :") #for x,v in cc.primary_conditions : #print(x,v) #print("cost ", cc.ccost) #print("new conditional costs are : ") #for ncc in new_conditional_costs : #print("conditions :") #for x,v in ncc.primary_conditions : # print(x,v) #print("cost ", cc.ccost) projected_primary.actions.append( Action(action.name, new_primary_precs, action.sec_precs, new_effect, action.cost, conditional_costs=new_conditional_costs, objective_function=action.objective_function, of_vname=action.of_varname)) #print("Addng action: ", action.name, " New primary precs: ", new_primary_precs, " Effects: ", new_effect) action_map[old_index] = new_index inv_action_map[new_index] = old_index projected_G = [] for x, v in G: if x not in variable_map: continue projected_G.append((variable_map[x], v)) return projected_primary, projected_G, (variable_map, inv_variable_map), (action_map, inv_action_map)