-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added different plan builders, revert some changes in plan builder
- Loading branch information
Showing
4 changed files
with
113 additions
and
56 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/org/matsim/prepare/choices/BestKPlanGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.matsim.prepare.choices; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import org.matsim.modechoice.CandidateGenerator; | ||
import org.matsim.modechoice.PlanCandidate; | ||
import org.matsim.modechoice.PlanModel; | ||
import org.matsim.modechoice.search.TopKChoicesGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Keeps selected plan as the first candidate and adds the rest with best options. | ||
*/ | ||
public class BestKPlanGenerator implements CandidateGenerator { | ||
private final int topK; | ||
private final TopKChoicesGenerator generator; | ||
|
||
public <T> BestKPlanGenerator(int topK, TopKChoicesGenerator generator) { | ||
this.topK = topK; | ||
this.generator = generator; | ||
} | ||
|
||
@Override | ||
public List<PlanCandidate> generate(PlanModel planModel, @Nullable Set<String> consideredModes, @Nullable boolean[] mask) { | ||
|
||
List<String[]> chosen = new ArrayList<>(); | ||
chosen.add(planModel.getCurrentModes()); | ||
|
||
// Chosen candidate from data | ||
PlanCandidate existing = generator.generatePredefined(planModel, chosen).get(0); | ||
|
||
List<PlanCandidate> result = new ArrayList<>(); | ||
result.add(existing); | ||
result.addAll(generator.generate(planModel, consideredModes, mask)); | ||
|
||
return result.stream().distinct().limit(topK).toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/matsim/prepare/choices/RandomPlanGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.matsim.prepare.choices; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import org.matsim.modechoice.CandidateGenerator; | ||
import org.matsim.modechoice.ModeEstimate; | ||
import org.matsim.modechoice.PlanCandidate; | ||
import org.matsim.modechoice.PlanModel; | ||
import org.matsim.modechoice.search.TopKChoicesGenerator; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Generates random candidates. | ||
*/ | ||
public class RandomPlanGenerator implements CandidateGenerator { | ||
|
||
private final int topK; | ||
private final TopKChoicesGenerator gen; | ||
private final SplittableRandom rnd = new SplittableRandom(0); | ||
|
||
public RandomPlanGenerator(int topK, TopKChoicesGenerator generator) { | ||
this.topK = topK; | ||
this.gen = generator; | ||
} | ||
|
||
@Override | ||
public List<PlanCandidate> generate(PlanModel planModel, @Nullable Set<String> consideredModes, @Nullable boolean[] mask) { | ||
|
||
List<String[]> chosen = new ArrayList<>(); | ||
chosen.add(planModel.getCurrentModes()); | ||
|
||
// Chosen candidate from data | ||
PlanCandidate existing = gen.generatePredefined(planModel, chosen).get(0); | ||
|
||
// This changes the internal state to randomize the estimates | ||
for (Map.Entry<String, List<ModeEstimate>> entry : planModel.getEstimates().entrySet()) { | ||
for (ModeEstimate est : entry.getValue()) { | ||
double[] utils = est.getEstimates(); | ||
if (utils != null) | ||
for (int i = 0; i < utils.length; i++) { | ||
utils[i] = -rnd.nextDouble(); | ||
} | ||
} | ||
} | ||
|
||
List<PlanCandidate> result = new ArrayList<>(); | ||
result.add(existing); | ||
result.addAll(gen.generate(planModel, consideredModes, mask)); | ||
|
||
return result.stream().distinct().limit(topK).toList(); | ||
} | ||
|
||
} |