Archived
0

improve compute min lands

This commit is contained in:
Mo
2016-04-25 15:48:07 +03:00
parent 118786afba
commit f18e007c63
8 changed files with 57 additions and 29 deletions

View File

@@ -3,8 +3,8 @@ package ru.trader.analysis;
public interface MutableRouteSpecification<T> extends RouteSpecification<T> {
@Override
public default boolean updateMutated(){return true;}
default boolean updateMutated(){return true;}
@Override
public default boolean mutable(){return true;}
default boolean mutable(){return true;}
}

View File

@@ -21,7 +21,7 @@ public class NullRouteSpecification<T> implements RouteSpecification<T> {
}
@Override
public int matchCount() {
public int maxMatches() {
return 1;
}
}

View File

@@ -5,17 +5,18 @@ import ru.trader.analysis.graph.Traversal;
public interface RouteSpecification<T> {
public boolean specified(Edge<T> edge, Traversal<T> entry);
boolean specified(Edge<T> edge, Traversal<T> entry);
default boolean content(Edge<T> edge, Traversal<T> entry){return specified(edge, entry);}
public default int lastFound(Edge<T> edge, Traversal<T> entry){
return specified(edge, entry) ? 0 : matchCount();
default int lastFound(Edge<T> edge, Traversal<T> entry){
return specified(edge, entry) ? 0 : minMatches();
}
public default int matchCount(){return 1;}
public default boolean updateMutated(){return false;}
public default boolean mutable(){return false;}
public default void update(Traversal<T> entry){}
public default long getStart(){return 0;}
public default long getEnd(){return Long.MAX_VALUE;}
default int maxMatches(){return 1;}
default int minMatches(){return maxMatches();}
default boolean updateMutated(){return false;}
default boolean mutable(){return false;}
default void update(Traversal<T> entry){}
default long getStart(){return 0;}
default long getEnd(){return Long.MAX_VALUE;}
default RouteSpecification<T> and(final RouteSpecification<T> other){
if (other instanceof RouteSpecificationAndMixer){

View File

@@ -53,10 +53,19 @@ public class RouteSpecificationAndMixer<T> implements RouteSpecificationMixer<T>
}
@Override
public int matchCount() {
public int maxMatches() {
int res = 0;
for (RouteSpecification<T> specification : specifications) {
res += specification.matchCount();
res += specification.maxMatches();
}
return res;
}
@Override
public int minMatches() {
int res = 0;
for (RouteSpecification<T> specification : specifications) {
res += specification.minMatches();
}
return res;
}
@@ -112,7 +121,7 @@ public class RouteSpecificationAndMixer<T> implements RouteSpecificationMixer<T>
private int getMinHope(Collection<RouteSpecification<T>> specifications){
if (specifications.isEmpty()) return -1;
return specifications.stream().mapToInt(RouteSpecification::matchCount).sum();
return specifications.stream().mapToInt(RouteSpecification::minMatches).sum();
}
private void collapse(RouteSpecification<T> specification){

View File

@@ -3,7 +3,10 @@ package ru.trader.analysis;
import ru.trader.analysis.graph.Edge;
import ru.trader.analysis.graph.Traversal;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
public class RouteSpecificationByPair<T> implements RouteSpecification<T> {
private final Collection<T> first;
@@ -78,7 +81,7 @@ public class RouteSpecificationByPair<T> implements RouteSpecification<T> {
}
@Override
public int matchCount() {
public int maxMatches() {
return 2;
}

View File

@@ -3,7 +3,9 @@ package ru.trader.analysis;
import ru.trader.analysis.graph.Edge;
import ru.trader.analysis.graph.Traversal;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
public class RouteSpecificationByTargets<T> implements RouteSpecification<T> {
private final Collection<T> targets;
@@ -74,13 +76,13 @@ public class RouteSpecificationByTargets<T> implements RouteSpecification<T> {
if (targets.isEmpty()) return 0;
if (checkTime()){
long time = edge.getTime();
if (targetOnly && (time < start || time > end)) return matchCount();
if (targetOnly && (time < start || time > end)) return maxMatches();
}
return all ? containsAll(edge, entry) : containsAny(edge, entry);
}
@Override
public int matchCount() {
public int maxMatches() {
if (targets.isEmpty()) return 0;
return all ? targets.size() : 1;
}

View File

@@ -49,10 +49,20 @@ public class RouteSpecificationOrMixer<T> implements RouteSpecificationMixer<T>,
}
@Override
public int matchCount() {
public int maxMatches() {
int res = 0;
for (RouteSpecification<T> specification : specifications) {
res = Math.max(res, specification.maxMatches());
}
return res;
}
@Override
public int minMatches() {
if (specifications.isEmpty()) return 0;
int res = Integer.MAX_VALUE;
for (RouteSpecification<T> specification : specifications) {
res = Math.min(res, specification.matchCount());
res = Math.min(res, specification.minMatches());
}
return res;
}
@@ -146,7 +156,7 @@ public class RouteSpecificationOrMixer<T> implements RouteSpecificationMixer<T>,
int hard2 = getHard(o2);
int cmp = Integer.compare(hard1, hard2);
if (cmp != 0) return cmp;
return Integer.compare(o1.matchCount(), o2.matchCount());
return Integer.compare(o1.minMatches(), o2.minMatches());
}
}
}

View File

@@ -7,16 +7,19 @@ import java.util.function.Consumer;
import java.util.function.Function;
public interface CrawlerSpecification<T> {
public RouteSpecification<T> routeSpecification();
RouteSpecification<T> routeSpecification();
public Consumer<List<Edge<T>>> onFoundFunc();
Consumer<List<Edge<T>>> onFoundFunc();
public Function<Traversal<T>, Object> getGroupGetter(boolean target);
Function<Traversal<T>, Object> getGroupGetter(boolean target);
public int getGroupCount();
int getGroupCount();
public default int getMinLands(){
return routeSpecification() != null ? routeSpecification().matchCount() : 1;
default int getMinLands(){
return routeSpecification() != null ? routeSpecification().minMatches() : 1;
}
default int getMaxLands(){
return routeSpecification() != null ? routeSpecification().maxMatches() : 1;
}
}