Archived
0

implement search stations

This commit is contained in:
iMoHax
2015-01-21 13:20:43 +03:00
parent d43af2939e
commit 7c1e828eae
16 changed files with 507 additions and 37 deletions

View File

@@ -25,7 +25,10 @@ public interface Market {
Collection<Place> get();
default Collection<Vendor> getVendors(){
return new PlacesWrapper(get());
return getVendors(false);
}
default Collection<Vendor> getVendors(boolean includeTransit){
return new PlacesWrapper(get(), includeTransit);
}
Collection<Group> getGroups();
Collection<Item> getItems();

View File

@@ -64,6 +64,42 @@ public class MarketAnalyzer {
this.limit = count;
}
public Collection<Offer> getOffers(Item item, MarketFilter filter){
Collection<Offer> offers = market.getSell(item);
Collection<Offer> res = new ArrayList<>(offers.size());
callback.setMax(offers.size());
for (Offer offer : offers) {
if (callback.isCancel()) break;
if (isFiltered(offer.getVendor()) || filter.isFiltered(offer.getVendor())){
LOG.trace("Is filtered, skip");
callback.inc();
continue;
}
res.add(offer);
callback.inc();
}
callback.onEnd();
return res;
}
public Collection<Vendor> getVendors(MarketFilter filter){
Collection<Vendor> vendors = getVendors();
Collection<Vendor> res = new ArrayList<>(vendors.size());
callback.setMax(vendors.size());
for (Vendor vendor : vendors) {
if (callback.isCancel()) break;
if (filter.isFiltered(vendor)){
LOG.trace("Is filtered, skip");
callback.inc();
continue;
}
res.add(vendor);
callback.inc();
}
callback.onEnd();
return res;
}
public Collection<Order> getTop(double balance){
LOG.debug("Get top {}", limit);
Collection<Place> places = getPlaces();
@@ -230,14 +266,14 @@ public class MarketAnalyzer {
}
public PathRoute getPath(Vendor from, Vendor to){
RouteGraph graph = new RouteGraph(from, getVendors(), tank, maxDistance, true, jumps);
RouteGraph graph = new RouteGraph(from, getVendors(true), tank, maxDistance, true, jumps);
return (PathRoute)graph.getFastPathTo(to);
}
public Collection<PathRoute> getPaths(Vendor from, double balance){
callback.setMax(1);
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize, callback.onStartSearch());
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
Collection<PathRoute> res = searcher.getPaths(from, vendors, jumps, balance, cargo, limit);
callback.inc();
callback.onEndSearch();
@@ -246,7 +282,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getPaths(Place from, double balance){
List<PathRoute> top = new ArrayList<>(limit);
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
callback.setMax(vendors.size());
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize, callback.onStartSearch());
for (Vendor vendor : from.get()) {
@@ -267,7 +303,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
callback.setMax(1);
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize, callback.onStartSearch());
Collection<PathRoute> res = searcher.getPaths(from, to, getVendors(), jumps, balance, cargo, limit);
Collection<PathRoute> res = searcher.getPaths(from, to, getVendors(true), jumps, balance, cargo, limit);
callback.inc();
callback.onEndSearch();
return res;
@@ -275,7 +311,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getPaths(Place from, Place to, double balance){
List<PathRoute> top = new ArrayList<>(limit);
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
Collection<Vendor> fVendors = from.get();
Collection<Vendor> toVendors = to.get();
int count = (int) Math.ceil(limit / fVendors.size());
@@ -306,7 +342,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getPaths(Vendor from, Place to, double balance){
List<PathRoute> top = new ArrayList<>(limit);
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
Collection<Vendor> toVendors = to.get();
int count = (int) Math.ceil(limit / toVendors.size());
callback.setMax(toVendors.size());
@@ -328,7 +364,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getPaths(Place from, Vendor to, double balance){
List<PathRoute> top = new ArrayList<>(limit);
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
Collection<Vendor> fVendors = from.get();
int count = (int) Math.ceil(limit / fVendors.size());
callback.setMax(fVendors.size());
@@ -350,7 +386,7 @@ public class MarketAnalyzer {
public Collection<PathRoute> getTopPaths(double balance){
List<PathRoute> top = new ArrayList<>(limit);
Collection<Vendor> vendors = getVendors();
Collection<Vendor> vendors = getVendors(true);
callback.setMax(vendors.size());
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize, callback.onStartSearch());
for (Vendor vendor : vendors) {
@@ -389,11 +425,15 @@ public class MarketAnalyzer {
}
private Collection<Vendor> getVendors(){
return getVendors(false);
}
private Collection<Vendor> getVendors(boolean includeTransit){
if (filter != null){
Collection<Vendor> vendors = new PlacesWrapper(getPlaces());
Collection<Vendor> vendors = new PlacesWrapper(getPlaces(), includeTransit);
return filter.filteredVendors(vendors);
} else {
return market.getVendors();
return market.getVendors(includeTransit);
}
}

View File

@@ -8,21 +8,23 @@ import java.util.Iterator;
public class PlacesWrapper extends AbstractCollection<Vendor> {
private final Collection<Place> places;
private final boolean includeTransit;
private int size;
public PlacesWrapper(Collection<Place> places) {
public PlacesWrapper(Collection<Place> places, boolean includeTransit) {
this.places = places;
this.includeTransit = includeTransit;
size = 0;
for (Place place : places) {
int count = place.count();
size += count > 0 ? count : 1;
size += count > 0 ? count : includeTransit ? 1 : 0;
}
}
@NotNull
@Override
public Iterator<Vendor> iterator() {
return new VendorsIterator(places);
return new VendorsIterator(places, includeTransit);
}
@Override

View File

@@ -9,11 +9,13 @@ import java.util.Iterator;
public class VendorsIterator implements Iterator<Vendor> {
private final Iterator<Place> places;
private final boolean includeTransit;
private Iterator<Vendor> vendors;
private Vendor next;
public VendorsIterator(Collection<Place> places) {
public VendorsIterator(Collection<Place> places, boolean includeTransit) {
this.places = places.iterator();
this.includeTransit = includeTransit;
nextPlace();
}
@@ -25,7 +27,10 @@ public class VendorsIterator implements Iterator<Vendor> {
vendors = v.iterator();
nextVendor();
} else {
next = new TransitVendor(place);
if (includeTransit)
next = new TransitVendor(place);
else
nextPlace();
}
} else {
next = null;

View File

@@ -65,7 +65,7 @@ public class RouteGraphTest extends Assert {
@Test
public void testRoutes() throws Exception {
RouteGraph graph = new RouteGraph(v1, market.getVendors(), 1, 1, true, 4);
RouteGraph graph = new RouteGraph(v1, market.getVendors(true), 1, 1, true, 4);
graph.setBalance(500);
graph.setCargo(5);
//Profit: 150 180 200 230 670 620 950 890 620 950 1015 1180 890 950 930
@@ -97,7 +97,7 @@ public class RouteGraphTest extends Assert {
@Test
public void testRoutes2() throws Exception {
RouteGraph graph = new RouteGraph(v5, market.getVendors(), 1, 15, true, 4);
RouteGraph graph = new RouteGraph(v5, market.getVendors(true), 1, 15, true, 4);
graph.setBalance(500);
graph.setCargo(5);
ArrayList<Path<Vendor>> routes = (ArrayList<Path<Vendor>>) graph.getPathsTo(v1, 5);

View File

@@ -28,7 +28,7 @@ public class RouteSearcherTest extends Assert {
Vendor ithaca = market.get().stream().filter((v)->v.getName().equals("Ithaca")).findFirst().get().get().iterator().next();
RouteSearcher searcher = new RouteSearcher(13.4, 40);
RouteGraph graph = new RouteGraph(ithaca, market.getVendors(), 40, 13.4, true, 6);
RouteGraph graph = new RouteGraph(ithaca, market.getVendors(true), 40, 13.4, true, 6);
graph.setCargo(440);
graph.setBalance(6000000);
@@ -36,7 +36,7 @@ public class RouteSearcherTest extends Assert {
List<Path<Vendor>> epaths = graph.getPathsTo(ithaca, 10);
PathRoute expect = epaths.stream().map(p -> (PathRoute) p).findFirst().get();
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(), 6, 6000000, 440, 10);
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(true), 6, 6000000, 440, 10);
PathRoute actual = apaths.stream().findFirst().get();
assertTrue("Routes is different",expect.isRoute(actual));
@@ -51,23 +51,23 @@ public class RouteSearcherTest extends Assert {
Vendor lhs3262 = market.get().stream().filter((v)->v.getName().equals("LHS 3262")).findFirst().get().get().iterator().next();
RouteSearcher searcher = new RouteSearcher(13.6, 40);
RouteGraph graph = new RouteGraph(ithaca, market.getVendors(), 40, 13.6, true, 6);
RouteGraph graph = new RouteGraph(ithaca, market.getVendors(true), 40, 13.6, true, 6);
graph.setCargo(440);
graph.setBalance(6000000);
List<Path<Vendor>> epaths = graph.getPathsTo(ithaca, 10);
PathRoute expect = epaths.stream().map(p -> (PathRoute) p).findFirst().get();
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(), 6, 6000000, 440, 10);
List<PathRoute> apaths = searcher.getPaths(ithaca, ithaca, market.getVendors(true), 6, 6000000, 440, 10);
PathRoute actual = apaths.stream().findFirst().get();
assertTrue("Routes is different",expect.isRoute(actual));
graph = new RouteGraph(lhs3262, market.getVendors(), 40, 13.6, true, 6);
graph = new RouteGraph(lhs3262, market.getVendors(true), 40, 13.6, true, 6);
graph.setCargo(440);
graph.setBalance(6000000);
expect = graph.getPathsTo(lhs3262, 10).stream().map(p -> (PathRoute)p).findFirst().get();
apaths = searcher.getPaths(lhs3262, lhs3262, market.getVendors(), 6, 6000000, 440, 10);
apaths = searcher.getPaths(lhs3262, lhs3262, market.getVendors(true), 6, 6000000, 440, 10);
actual = apaths.stream().findFirst().get();
assertEquals("Routes is different",expect.getAvgProfit(), actual.getAvgProfit(), 0.00001);

View File

@@ -166,7 +166,6 @@ public class MarketTest extends Assert {
int c = 3;
for (Vendor vendor : market.getVendors()) {
if ("Transit".equals(vendor.toString())) continue;
if ("Vendor 1".equals(vendor.getName())){
assertEquals(vendor1, vendor);
assertEquals(place1, vendor.getPlace());
@@ -208,12 +207,12 @@ public class MarketTest extends Assert {
place1.remove(vendor2);
place2.remove(vendor3);
assertEquals(2, market.getVendors().size());
assertEquals(0, market.getVendors().size());
market.remove(place1);
market.remove(place2);
assertEquals(0, market.getVendors().size());
assertEquals(0, market.getVendors(true).size());
}
@Test
@@ -340,7 +339,7 @@ public class MarketTest extends Assert {
assertEquals(0, market.getSell(item1).size());
assertEquals(0, market.getBuy(item1).size());
assertEquals(0, market.getVendors().size());
assertEquals(0, market.getVendors(true).size());
assertEquals(0, market.get().size());
assertEquals(0, sellStat.getOffers().size());
assertEquals(0, buyStat.getOffers().size());