added filtering in the cost function. It can be (de)activted with the filtered_cost argument which defaults to True