From 70d140c7de6bdbb13fc41004271602ebbc80cc05 Mon Sep 17 00:00:00 2001 From: iMoHax Date: Fri, 3 Jul 2015 15:44:16 +0300 Subject: [PATCH] refill fuel tank to optimal fuel --- .../ru/trader/analysis/graph/CCrawler.java | 9 +++-- .../analysis/graph/ConnectibleEdge.java | 10 ++++-- .../analysis/graph/ConnectibleGraph.java | 14 ++++---- core/src/main/java/ru/trader/core/Ship.java | 34 +++++++++++++++++-- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/ru/trader/analysis/graph/CCrawler.java b/core/src/main/java/ru/trader/analysis/graph/CCrawler.java index 9fa2ebd..2884070 100644 --- a/core/src/main/java/ru/trader/analysis/graph/CCrawler.java +++ b/core/src/main/java/ru/trader/analysis/graph/CCrawler.java @@ -52,7 +52,7 @@ public class CCrawler> extends Crawler { double nextLimit; if (cEdge.isRefill()) { LOG.trace("Refill"); - nextLimit = getShip().getTank() - cEdge.getFuelCost(); + nextLimit = cEdge.getRefill() - cEdge.getFuelCost(); } else { nextLimit = getProfile().withRefill() ? cEntry.getFuel() - cEdge.getFuelCost() : getShip().getTank(); } @@ -91,10 +91,9 @@ public class CCrawler> extends Crawler { double distance = source.getDistance(edge.target.getEntry()); double fuelCost = getShip().getFuelCost(fuel, distance); double nextLimit = getProfile().withRefill() ? fuel - fuelCost : getShip().getTank(); - boolean refill = nextLimit < 0 && source.canRefill(); - if (refill) { - refill = true; - fuelCost = getShip().getFuelCost(distance); + double refill = nextLimit < 0 && source.canRefill() ? getShip().getRoundMaxFuel(distance) : 0; + if (refill > 0) { + fuelCost = getShip().getFuelCost(refill, distance); } ConnectibleEdge cEdge = new ConnectibleEdge<>(edge.getSource(), edge.getTarget()); cEdge.setRefill(refill); diff --git a/core/src/main/java/ru/trader/analysis/graph/ConnectibleEdge.java b/core/src/main/java/ru/trader/analysis/graph/ConnectibleEdge.java index 2de0a6e..966fcb5 100644 --- a/core/src/main/java/ru/trader/analysis/graph/ConnectibleEdge.java +++ b/core/src/main/java/ru/trader/analysis/graph/ConnectibleEdge.java @@ -3,7 +3,7 @@ package ru.trader.analysis.graph; import ru.trader.graph.Connectable; public class ConnectibleEdge> extends Edge { - protected boolean refill; + protected double refill; protected double fuelCost; public ConnectibleEdge(Vertex source, Vertex target) { @@ -11,10 +11,14 @@ public class ConnectibleEdge> extends Edge { } public boolean isRefill() { + return refill != 0; + } + + public double getRefill() { return refill; } - protected void setRefill(boolean refill) { + public void setRefill(double refill) { this.refill = refill; } @@ -36,7 +40,7 @@ public class ConnectibleEdge> extends Edge { @Override public String toString() { return source.getEntry().toString() + " - "+ weight - + (refill ? "R" : "") + + (isRefill() ? "R" : "") +" -> " + target.getEntry().toString(); } } diff --git a/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java b/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java index 57fbda1..ef76b7b 100644 --- a/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java +++ b/core/src/main/java/ru/trader/analysis/graph/ConnectibleGraph.java @@ -53,13 +53,13 @@ public class ConnectibleGraph> extends AbstractGraph @Override public boolean test(Double distance) { - return distance <= getShip().getJumpRange(limit) || (profile.withRefill() && distance <= getShip().getJumpRange() && source.canRefill()); + return distance <= getShip().getJumpRange(limit) || (profile.withRefill() && distance <= getShip().getMaxJumpRange() && source.canRefill()); } } protected class ConnectibleGraphBuilder extends GraphBuilder { private final DistanceFilter distanceFilter; - protected boolean refill; + protected double refill; protected double fuelCost; protected ConnectibleGraphBuilder(Vertex vertex, Collection set, int deep, double limit) { @@ -76,13 +76,13 @@ public class ConnectibleGraph> extends AbstractGraph } fuelCost = getShip().getFuelCost(limit, distance); double nextLimit = profile.withRefill() ? limit - fuelCost : getShip().getTank(); - if (nextLimit < 0) { + if (nextLimit < 0 && vertex.getEntry().canRefill()) { LOG.trace("Refill"); - refill = true; - fuelCost = getShip().getFuelCost(distance); - nextLimit = getShip().getTank() - fuelCost; + refill = getShip().getRoundMaxFuel(distance); + fuelCost = getShip().getFuelCost(refill, distance); + nextLimit = refill - fuelCost; } else { - refill = false; + refill = 0; } return nextLimit; } diff --git a/core/src/main/java/ru/trader/core/Ship.java b/core/src/main/java/ru/trader/core/Ship.java index 5b60cae..2c0eb3f 100644 --- a/core/src/main/java/ru/trader/core/Ship.java +++ b/core/src/main/java/ru/trader/core/Ship.java @@ -1,6 +1,8 @@ package ru.trader.core; public class Ship { + private final static int REFILL_FUEL_STEP = 10; + private int cargo; private Engine engine; private double tank; @@ -69,13 +71,36 @@ public class Ship { //Laden fuel cost public double getFuelCost(double distance){ - return engine.getFuelCost(distance, getLadenMass()); + return engine.getFuelCost(distance, getLadenMass(getRoundMaxFuel(distance))); } public double getFuelCost(double fuel, double distance){ return engine.getFuelCost(distance, getLadenMass(fuel)); } + public double getRoundMaxFuel(double distance){ + return getRoundMaxFuel(distance, REFILL_FUEL_STEP); + } + + private double getRoundMaxFuel(double distance, int step){ + double fuel = getMaxFuel(distance); + if (fuel == 0 || fuel == tank) return fuel; + double minFuel = engine.getFuelCost(distance, getLadenMass(0)); + fuel = Math.floor(fuel*step/tank) * tank / step; + return fuel < minFuel ? 0 : fuel; + } + + public double getMaxFuel(double distance){ + double fuel = engine.getMaxFuel(distance, getLadenMass(0)); + if (fuel >= tank) return tank; + return fuel; + + } + + public double getMaxJumpRange(){ + return getJumpRange(Math.min(engine.getMaxFuel(), tank)); + } + //Jump range with full fuel tank public double getJumpRange(){ return getJumpRange(tank); @@ -105,7 +130,7 @@ public class Ship { ", engine=" + engine + ", tank=" + tank + ", mass=" + mass + - ", maxDist=" + getJumpRange() + + ", maxDist=" + getMaxJumpRange() + ", fullTankDist=" + getFullTankJumpRange() + '}'; } @@ -183,6 +208,11 @@ public class Ship { return getMultiplier() * Math.pow(distance * (mass / getOptMass()), getPowMultiplier()); } + public double getMaxFuel(double distance, double emptyTankMass){ + double f = Math.pow(getMaxFuel()/getMultiplier(), 1/getPowMultiplier())*getOptMass()/distance - emptyTankMass; + return f < getMaxFuel() ? 0 : f; + } + public double getJumpRange(double fuel, double mass){ return Math.pow(Math.min(fuel, getMaxFuel())/getMultiplier(), 1/getPowMultiplier())*getOptMass()/mass; }