(self, instances_by_market)
return added_instances, removed_instances
Modify the capacity of the cluster to match a specified state

        :param instances_by_market: a dict from InstanceMarket -> num, representing the desired number of
            instances in each specified market; unspecified markets are set to 0
        :returns: a tuple (added_instances, removed_instances)
Modify the capacity of the cluster to match a specified state
Modify the capacity of the cluster to match a specified state
def modify_size(self, instances_by_market):
        """ Modify the capacity of the cluster to match a specified state

        :param instances_by_market: a dict from InstanceMarket -> num, representing the desired number of
            instances in each specified market; unspecified markets are set to 0
        :returns: a tuple (added_instances, removed_instances)
        """
        added_instances, removed_instances = [], []
        instances_by_market.update(
            {market_to_empty: 0 for market_to_empty in set(self.instance_ids_by_market) - set(instances_by_market)}
        )
        for market, num in instances_by_market.items():
            delta = int(num - self.market_size(market))

            if delta > 0:
                instances = [Instance(market, self.simulator.current_time) for i in range(delta)]
                self.instance_ids_by_market[market].extend([instance.id for instance in instances])
                added_instances.extend(instances)

            if delta < 0:
                to_del = abs(delta)
                for id in self.instance_ids_by_market[market][:to_del]:
                    self.instances[id].end_time = self.simulator.current_time
                    removed_instances.append(self.instances[id])
                    del self.instances[id]
                del self.instance_ids_by_market[market][:to_del]
                if not self.instance_ids_by_market[market]:
                    del self.instance_ids_by_market[market]

        self.instances.update({instance.id: instance for instance in added_instances})
        return added_instances, removed_instances
def modify_size ( self , instances_by_market ) : added_instances , removed_instances = [ ] , [ ] instances_by_market . update ( { market_to_empty : 0 for market_to_empty in set ( self . instance_ids_by_market ) - set ( instances_by_market ) } ) for market , num in instances_by_market . items ( ) : delta = int ( num - self . market_size ( market ) ) if delta > 0 : instances = [ Instance ( market , self . simulator . current_time ) for i in range ( delta ) ] self . instance_ids_by_market [ market ] . extend ( [ instance . id for instance in instances ] ) added_instances . extend ( instances ) if delta < 0 : to_del = abs ( delta ) for id in self . instance_ids_by_market [ market ] [ : to_del ] : self . instances [ id ] . end_time = self . simulator . current_time removed_instances . append ( self . instances [ id ] ) del self . instances [ id ] del self . instance_ids_by_market [ market ] [ : to_del ] if not self . instance_ids_by_market [ market ] : del self . instance_ids_by_market [ market ] self . instances . update ( { instance . id : instance for instance in added_instances } ) return added_instances , removed_instances