Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Try to use generic WorstPlan...
Browse files Browse the repository at this point in the history
  • Loading branch information
kt86 committed Jan 22, 2024
1 parent 10ea88c commit 4c5ba8d
Showing 1 changed file with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,59 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.matsim.api.core.v01.population.HasPlansAndId;
import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector;
import org.matsim.core.replanning.selectors.PlanSelector;
import org.matsim.freight.logistics.LSP;
import org.matsim.freight.logistics.LSPPlan;

class LSPWorstPlanForRemovalSelector implements PlanSelector<LSPPlan, LSP> {
class LSPWorstPlanForRemovalSelector extends GenericWorstPlanForRemovalSelector<LSPPlan, LSP> {//implements PlanSelector<LSPPlan, LSP> {

private static final String UNDEFINED_TYPE = "undefined";

@Override
public LSPPlan selectPlan(HasPlansAndId<LSPPlan, LSP> lsp) {

Map<String, Integer> typeCounts = new ConcurrentHashMap<String, Integer>();

// count how many plans per type an agent has:
for (LSPPlan plan : lsp.getPlans()) {
String type = plan.getType();
if (type == null) {
type = UNDEFINED_TYPE;
}
typeCounts.merge(type, 1, (a, b) -> a + b);
}

LSPPlan worst = null;
double worstScore = Double.POSITIVE_INFINITY;
for (LSPPlan plan : lsp.getPlans()) {
String type = plan.getType();
if (type == null) {
type = UNDEFINED_TYPE;
}
if (typeCounts.get(type) > 1) {
if (plan.getScore() == null || plan.getScore().isNaN()) {
worst = plan;
worstScore = Double.NEGATIVE_INFINITY;
} else if (plan.getScore() < worstScore) {
worst = plan;
worstScore = plan.getScore();
}
}
}
if (worst == null) {
// there is exactly one plan, or we have of each plan-type exactly one.
// select the one with worst score globally, or the first one with score=null
for (LSPPlan plan : lsp.getPlans()) {
if (plan.getScore() == null || plan.getScore().isNaN()) {
return plan;
}
if (plan.getScore() < worstScore) {
worst = plan;
worstScore = plan.getScore();
}
}
}
return worst;
}
// private static final String UNDEFINED_TYPE = "undefined";
//
// @Override
// public LSPPlan selectPlan(HasPlansAndId<LSPPlan, LSP> lsp) {
//
// Map<String, Integer> typeCounts = new ConcurrentHashMap<String, Integer>();
//
// // count how many plans per type an agent has:
// for (LSPPlan plan : lsp.getPlans()) {
// String type = plan.getType();
// if (type == null) {
// type = UNDEFINED_TYPE;
// }
// typeCounts.merge(type, 1, (a, b) -> a + b);
// }
//
// LSPPlan worst = null;
// double worstScore = Double.POSITIVE_INFINITY;
// for (LSPPlan plan : lsp.getPlans()) {
// String type = plan.getType();
// if (type == null) {
// type = UNDEFINED_TYPE;
// }
// if (typeCounts.get(type) > 1) {
// if (plan.getScore() == null || plan.getScore().isNaN()) {
// worst = plan;
// worstScore = Double.NEGATIVE_INFINITY;
// } else if (plan.getScore() < worstScore) {
// worst = plan;
// worstScore = plan.getScore();
// }
// }
// }
// if (worst == null) {
// // there is exactly one plan, or we have of each plan-type exactly one.
// // select the one with worst score globally, or the first one with score=null
// for (LSPPlan plan : lsp.getPlans()) {
// if (plan.getScore() == null || plan.getScore().isNaN()) {
// return plan;
// }
// if (plan.getScore() < worstScore) {
// worst = plan;
// worstScore = plan.getScore();
// }
// }
// }
// return worst;
// }
}

0 comments on commit 4c5ba8d

Please sign in to comment.