I am currently working on a code that has to find the best solution. First I check whether one of the three is larger than the other two. Hence, there is a maximum that occurs only once. If there are two numbers larger than the third one, but equal to each other, I will have to compare the distance of those two and then the one with the smallest distance is chosen.
The profit functions and distances are calculated outside this method and not that important.
What I have come up with so far is to use a lot of if statements. However, I was wondering whether there would be a more efficient method to do this.
public void bestSolution(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR)
{
int profitLS = profitRoutes(LS);
int profitSA = profitRoutes(SA);
int profitRR = profitRoutes(RR);
int distanceLS = totalDistance(LS);
int distanceSA = totalDistance(SA);
int distanceRR = totalDistance(RR);
if ((profitLS > profitSA) & (profitLS > profitRR))
{
}
}
In case of finding max between three integers -
int mostProfit = Math.max(profitLS, Math.max(profitSA, profitRR));
Considering case - "distance of those two and then the one with the smallest distance is chosen"
class DistanceProfit{
private int profit;
private int distance;
public DistanceProfit(int profit, int distance){
this.profit = profit;
this.distance = distance;
}
}
...
//create DistanceProfit objects add to list
Collections.sort(distenceProfitList, new Comparator<DistenceProfit>{
public int compare(DistenceProfit dp1, DistenceProfit dp2){
if(dp1.getProfit()==dp2.getProfit())
return dp1.getDistance() - dp2..getDistance();
return dp1.getProfit() - dp2.getProfit();
}
});
You could create a TreeSet with the comparison results and select the 'greatest' element.
The comparison result could be something like:
public class ProfitCounter implements Comparable<ProfitCounter>
{
public ProfitCounter(List<ROUTE> route)
{
this.route = route;
profit = profitRoutes(route);
distance = totalDistance(route);
}
#Override
public int compareTo(ProfitCounter other)
{
int result;
result = profit - other.profit;
if (result == 0)
result = other.distance - distance;
return (result);
}
private List<ROUTE> route;
private int profit;
private int distance;
} // class ProfitCounter
Id use something on these lines. Does not limit u to 3 parameters.
public class RouteCalc implements Comparable<RouteCalc> {
private final List routes;
public RouteCalc(List routes) {
this.routes = routes;
}
public static int calcProfit(List routes) {
//use the list to calculate profit
return 0;
}
public static int calcDistance(List routes) {
//use the list to calculate distance
return 0;
}
#Override
public int compareTo(#NonNull RouteCalc another) {
final int profitA = calcProfit(this.routes);
final int profitB = calcProfit(another.routes);
//swap parameters to change from ascending to descending and vice-versa
final int compare = Integer.compare(profitA, profitB);
//if same profit, compare distance
if (compare == 0) {
final int distanceA = calcDistance(this.routes);
final int distanceB = calcDistance(another.routes);
return Integer.compare(distanceA, distanceB);
} else
return compare;
}
//sample usage
public static void main(String args[]) {
final List<RouteCalc> allRoutes = new ArrayList<>();
//add routes
final RouteCalc bestRoute = Collections.max(allRoutes);
}
}
static class CalculatedRoute {
public static CalculatedRoute mostProfitableOf(List<CalculatedRoute> calculatedRoutes) {
return Collections.max(calculatedRoutes, BY_PROFIT_AND_DISTANCE);
}
public static final Comparator<CalculatedRoute> BY_PROFIT_AND_DISTANCE = new Comparator<CalculatedRoute>() {
#Override
public int compare(CalculatedRoute o1, CalculatedRoute o2) {
int cmp = o2.profit - o1.profit;
if (cmp == 0) {
cmp = o1.distance - o2.distance;
}
return cmp;
}
};
private final List<ROUTE> routeList;
private final int profit;
private final int distance;
public CalculatedRoute(List<ROUTE> routeList, int profit, int distance) {
this.profit = profit;
this.distance = distance;
}
public List<ROUTE> getRouteList() {
return routeList;
}
public int getProfit() {
return profit;
}
public int getDistance() {
return distance;
}
}
public List<ROUTE> mostProfitableOf(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR) {
return CalculatedRoute.mostProfitableOf(Arrays.asList(
new CalculatedRoute(LS, profitRoutes(LS), totalDistance(LS)),
new CalculatedRoute(SA, profitRoutes(SA), totalDistance(SA)),
new CalculatedRoute(RR, profitRoutes(RR), totalDistance(RR))
)).getRouteList();
}
Related
public class GPSping {
private double pingLat;
private double pingLon;
private int pingTime;
}
The Trip class
public class Trip {
private ArrayList<GPSping> pingList;
public Trip() {
pingList = new ArrayList<>();
}
public Trip(ArrayList<GPSping> triplist) {
pingList = new ArrayList<>();
}
public ArrayList<GPSping> getPingList() {
return this.pingList;
}
public boolean addPing(GPSping p) {
int length = pingList.size();
int Time = pingList.get(length);
if (p.getTime() > this.pingList[length]) {
pinglist.add(p);
return True;
} else {
return False;
}
}
}
I am trying to add a GPS ping to this trip list but only if the time of p is after the last time in this trip list. I am very new to Java and am struggling with wrapping my head around the syntax some help would be greatly appreciated.
First element in List has index 0, to to get the last one:
int Time = pingList.get(length - 1);
But I think, it's better to store maxPingTime to check it before add new GPSping:
class Trip {
private final List<GPSping> pingList = new ArrayList<>();
private int maxPingTime = Integer.MIN_VALUE;
public List<GPSping> getPingList() {
return pingList.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(pingList);
}
public boolean addPing(GPSping p) {
if (p.getPingTime() <= maxPingTime)
return false;
pingList.add(p);
maxPingTime = p.getPingTime();
return true;
}
}
final class GPSping {
private final double pingLat;
private final double pingLon;
private final int pingTime;
public GPSping(double pingLat, double pingLon, int pingTime) {
this.pingLat = pingLat;
this.pingLon = pingLon;
this.pingTime = pingTime;
}
}
P.S. Pay attention on Encapsulation OOP principle: GPSping should be final and pingList should not be directly retrieved.
I've written my first Genetic Algorithm in Java and I'm able to optimize functions with one argument x, but I don't know how to optimize functions with two arguments x and y. Algorithm class and main app works correctly so i send only Individual.java and Population.java. If I think correctly in genes I have only x-coordinate but I'm not sure how to add y-coordinate. Any advise will be helpfull.
Individual.java
public class Individual {
private int[] genes;
private int fitness;
private Random randomGenerator;
public Individual() {
this.genes = new int[Constants.CHROMOSOME_LENGTH];
this.randomGenerator = new Random();
}
public void generateIndividual() {
for(int i = 0; i < Constants.CHROMOSOME_LENGTH; i++) {
int gene = randomGenerator.nextInt(2);
genes[i] = gene;
}
}
public double f(double x) {
// return Math.pow(x,2);
return (Math.pow((1-x),2)) + (100*(Math.pow((1-Math.pow(x,2)),2)));
// return Math.sin(x)*((x-2)*(x-2))+3;
}
public double getFitness() {
double genesToDouble = genesToDouble();
return f(genesToDouble);
}
public double getFitnessResult() {
double genesToDouble = genesToDouble();
return genesToDouble;
}
public double genesToDouble() {
int base = 1;
double geneInDouble = 0;
for( int i =0; i < Constants.GENE_LENGTH; i++) {
if(this.genes[i] == 1)
geneInDouble += base;
base = base*2;
}
geneInDouble = (geneInDouble / 1024) * 10.1;
return geneInDouble;
}
public int getGene(int index) {
return this.genes[index];
}
public void setGene(int index, int value) {
this.genes[index] = value;
this.fitness = 0;
}
}
Population.java
public class Population {
private Individual[] individuals;
public Population(int populationSize) {
individuals = new Individual[populationSize];
}
public void initialize() {
for(int i = 0; i < individuals.length; i++) {
Individual newIndividual = new Individual();
newIndividual.generateIndividual();
saveIndividual(i, newIndividual);
}
}
public Individual getIndividual(int index) {
return this.individuals[index];
}
//maksimum lub minimum
public Individual getFittestIndividual() {
Individual fittest = individuals[0];
for(int i =0; i < individuals.length; i++) {
if(getIndividual(i).getFitness() < fittest.getFitness())
fittest = getIndividual(i);
}
return fittest;
}
public int size() {
return this.individuals.length;
}
public void saveIndividual(int index, Individual individual) {
this.individuals[index] = individual;
}
}
I'm learning the concept of neural networks. I decided to try making the neuron class by myself. What is the best way to implement different activation functions in my code? Now it uses only the binary step function.
It's my first try in coding neural networks so if you have any suggestions about my code, or it is completely dumb, please let me know.
Here is my code:
public class Neuron {
// properties
private ArrayList<Neuron> input;
private ArrayList<Float> weight;
private float pot, bias, sense, out;
private boolean checked;
// methods
public float fire(){
pot = 0f;
if (input != null) {
for (Neuron n : input){
if (!n.getChecked()){
pot += n.fire()*weight.get(input.indexOf(n));
} else {
pot += n.getOut()*weight.get(input.indexOf(n));
} // end of condition (checked)
} // end of loop (for input)
} // end of condition (input exists)
checked = true;
pot -= bias;
pot += sense;
out = actFunc(pot);
return out;
} // end of fire()
// getting properties
public float getPot(){return pot;}
public boolean getChecked(){return checked;}
public float getOut(){return out;}
// setting properties
public void stimulate(float f){sense = f;}
public void setBias(float b){bias = b;}
public void setChecked(boolean c){checked = c;}
public void setOut(float o){out = o;}
// connection
public void connect(Neuron n, float w){
input.add(n);
weight.add(w);
}
public void deconnect(Neuron n){
weight.remove(input.indexOf(n));
input.remove(n);
}
// activation function
private float actFunc(float x){
if (x < 0) {
return 0f;
} else {
return 1f;
}
}
// constructor
public Neuron(Neuron[] ns, float[] ws, float b, float o){
if (ns != null){
input = new ArrayList<Neuron>();
weight = new ArrayList<Float>();
for (Neuron n : ns) input.add(n);
for (int i = 0; i < ws.length; i++) weight.add(ws[i]);
} else {
input = null;
weight = null;
}
bias = b;
out = o;
}
public Neuron(Neuron[] ns){
if (ns != null){
input = new ArrayList<Neuron>();
weight = new ArrayList<Float>();
for (Neuron n : ns) input.add(n);
for (int i = 0; i < input.size(); i++) weight.add((float)Math.random()*2f-1f);
} else {
input = null;
weight = null;
}
bias = (float)Math.random();
out = (float)Math.random();
}
}
First, define interface of any activation function:
public interface ActivationFunction {
float get(float f);
}
Then write some implementations:
public class StepFunction implements ActivationFunction {
#Override
public float get() {return (x < 0) ? 0f : 1f;}
}
public class SigmoidFunction implements ActivationFunction {
#Override
public float get() {return StrictMath.tanh(h);}
}
Finally, set some implementation to your Neuron:
public class Neuron {
private final ActivationFunction actFunc;
// other fields...
public Neuron(ActivationFunction actFunc) {
this.actFunc = actFunc;
}
public float fire(){
// ...
out = actFunc.get(pot);
return out;
}
}
as following:
Neuron n = new Neuron(new SigmoidFunction());
Note, neural netoworks are using signal propagation through neurons, where weights are produced. Computing of weight depends also on first derivative of an activation function. Therefore, I would extend ActivationFunction by method, which will return first derivative at specified point x:
public interface ActivationFunction {
float get(float f);
float firstDerivative(float x);
}
So the implemenations will look like:
public class StepFunction implements ActivationFunction {
#Override
public float get(float x) {return (x < 0) ? 0f : 1f;}
#Override
public float firstDerivative(float x) {return 1;}
}
public class SigmoidFunction implements ActivationFunction {
#Override
public float get(float x) {return StrictMath.tanh(x);}
// derivative_of tanh(x) = (4*e^(2x))/(e^(2x) + 1)^2 == 1-tanh(x)^2
#Override
public float firstDerivative(float x) {return 1 - Math.pow(StrictMath.tanh(x), 2);}
}
Then, use actFunction.firstDerivative(x); in fire() method where weight is being computed.
I have this kind of a TreeMap. How can i get minimum and maximum values for town temperature after i make some entries in towns? I do not copy the code where i fill in some values in towns because it works fine.
Map<String, Town> towns = new TreeMap<>();
The Town.class is like this.
public class Town {
private int temperature;
private int rainfall;
private int windPower;
private Downwind downwind;
public Town(int temperature, int rainfall, int windPower, Downwind downwind) {
this.temperature = temperature;
this.rainfall = rainfall;
this.windPower = windPower;
this.downwind = downwind;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public int getRainfall() {
return rainfall;
}
public void setRainfall(int rainfall) {
this.rainfall = rainfall;
}
public int getWindPower() {
return windPower;
}
public void setWindPower(int windPower) {
this.windPower = windPower;
}
public Downwind getDownwind() {
return downwind;
}
public void setDownwind(Downwind downwind) {
this.downwind = downwind;
}
The simplest:
Optional<Town> maybeMinTown = towns.values().stream()
.min(Comparator.comparing(Town::getTemperature));
Town minTown = maybeMinTown.get(); // throws NoSuchElementException when towns map is empty
the same for max - you only need to use max instead of min.
UPDATE
To get just temperature you can map Town to temperature and then call min/max:
final OptionalInt min = towns.values().stream()
.mapToInt(Town::getTemperature)
.min();
min.getAsInt(); // or you can call min.orElseGet(some_lowest_value)
OptionalInt is the same for int what Optional<Town> is for Town.
If you are not in Java 8, you have to order the Map by value to achieve that, maybe something like this
public static Map<String, Town> sortByValues(final Map<String, Town> map) {
Comparator<String> valueComparator = new Comparator<String>() {
public int compare(String k1, String k2) {
if (map.get(k1).getTemperature() < map.get(k2).getTemperature()) {
return 1;
} else if (map.get(k1).getTemperature() == map.get(k2).getTemperature()) {
return 0;
} else {
return -1;
}
}
};
Map<String, Town> sortedByValues = new TreeMap<String, Town>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
When you create your TreeMap, pass an instance of this comparator class
towns = sortByValues(towns);
After that, you will have the maximun value at position 0 and the minimun in the last element.
UPDATE
Change the code in order to compile.
I would like to create a JSpinner which can take every possible Double value between a specified minimum and a specified maximum.
Also, the JSpinner should be able to display a text instead of a specific value. Let's say our JSpinner can take values from -1 to 10. I would like to display a text, e.g. "Auto", instead of -1 .
How to replace by
Here is the Model I wrote, but it seems not to be enough, because it says in JSpinner there is an error because the text is not a Double.
public class SpinnerSpecialModel
extends AbstractSpinnerModel implements SpinnerMinMaxModel {
public static final double DEFAULT_MINIMUM = 0.0;
public static final double DEFAULT_MAXIMUM = Double.POSITIVE_INFINITY;
public static final double DEFAULT_STEP = 1.0;
public static final double DEFAULT_VALUE = 1.0;
public static final double DEFAULT_SPECIAL_NUMBER = -1.0;
public static final String DEFAULT_SPECIAL_TEXT = "Auto";
private double maximum;
private double minimum;
private double stepSize;
private double currentNumber;
private double specialNumber;
private String specialText;
private Object m_Value;
public SpinnerSpecialModel(double max, double min, double step, double num,
double specialNum, String specialTxt) {
maximum = max;
minimum = min;
stepSize = step;
currentNumber = num;
specialNumber = specialNum;
specialText = specialTxt;
setAccurateValue(num);
}
public SpinnerSpecialModel(double specialNum, String specialTxt) {
this(DEFAULT_MAXIMUM, DEFAULT_MINIMUM,
DEFAULT_STEP, DEFAULT_VALUE, specialNum, specialTxt);
}
public SpinnerSpecialModel() {
this(DEFAULT_SPECIAL_NUMBER, DEFAULT_SPECIAL_TEXT);
}
#Override
public Object getValue() {
if (currentNumber == specialNumber) {
m_Value = specialText;
}
else {
m_Value = currentNumber;
}
return m_Value;
}
#Override
public void setValue(Object value) {
setAccurateValue(value);
}
private void setAccurateValue(Object value) {
if (value instanceof Double) {
double doubleValue = (Double) value;
if (doubleValue != currentNumber) {
if (doubleValue == specialNumber) {
currentNumber = specialNumber;
m_Value = specialText;
}
else if (doubleValue > maximum) {
currentNumber = maximum;
m_Value = maximum;
}
else if (doubleValue < minimum) {
currentNumber = maximum;
m_Value = minimum;
}
else {
currentNumber = doubleValue;
m_Value = doubleValue;
}
fireStateChanged();
}
}
if (value instanceof String) {
String stringValue = (String) value;
if (stringValue.equals(specialText)) {
this.currentNumber = specialNumber;
this.m_Value = specialText;
fireStateChanged();
}
}
}
#Override
public Object getNextValue() {
return getNewValue(+1);
}
#Override
public Object getPreviousValue() {
return getNewValue(-1);
}
/**
*
* #param direction
* #return
*/
private Object getNewValue(int direction) {
double newValue = currentNumber + direction * stepSize;
setAccurateValue(newValue);
return m_Value;
}
#Override
public double getMaximum() {
return maximum;
}
#Override
public double getMinimum() {
return minimum;
}
#Override
public double getStepSize() {
return stepSize;
}
#Override
public void setMaximum(double max) {
maximum = max;
}
#Override
public void setMinimum(double min) {
minimum = min;
}
#Override
public void setStepSize(double step) {
stepSize = step;
}
}
The best and proper way to do this is not as simple as just writing a model, but it is not very complicated. You actually need to write an Editor and a Formatter to have a true MVC spinner:
A class that extends JSpinner : SpecialValuesSpinner.
A class that implements SpinnerModel : SpecialValuesSpinnerModel
A class that extends DefaultEditor and implements DocumentListener : SpecialValuesSpinnerEditor
A class that extends NumberFormatter : SpecialValuesSpinnerFormatter
I am not going to show you the code for all classes, but here is basically what you have to do in each :
SpecialValuesSpinner :
public class SpecialValuesSpinner() extends SpinnerNumberModel {
// in your constructor do this
setModel(new SpecialValuesSpinnerModel(YOUR_SPECIAL_VALUES);
setEditor(new SpecialValuesSpinnerEditor());
}
SpecialValuesSpinnerModel :
public class SpinnerSpecialValuesModel() extends JSpinner {
// in this class you handle the fact that now, you have an
// interval of values and a list of special values that are allowed.
// here is what I did :
#Override
public Object getNextValue() {
return incrValue(+1);
}
#Override
public Object getPreviousValue() {
return incrValue(-1);
}
private Object incrValue(int dir) {
// NB : BigDecimal here because this is what I used,
// but use what you want in your model
BigDecimal result = null;
BigDecimal numberBD = new BigDecimal(getNumber().toString());
BigDecimal stepSizeBD = new BigDecimal(getStepSize().toString());
BigDecimal dirBD = new BigDecimal(dir);
BigDecimal nextValue = numberBD.add(stepSizeBD.multiply(dirBD));
TreeSet<BigDecimal> currentAllowedValues = new TreeSet<BigDecimal>();
currentAllowedValues.addAll(m_SpecialValues);
if (getMaximum() != null) {
currentAllowedValues.add((BigDecimal) getMaximum());
}
if (getMinimum() != null) {
currentAllowedValues.add((BigDecimal) getMinimum());
}
if (isIncludedInBounds(nextValue)) {
currentAllowedValues.add(nextValue);
}
if (dir > 0) {
try {
result = currentAllowedValues.higher(numberBD);
}
catch (NoSuchElementException e) {}
}
else if (dir < 0) {
try {
result = currentAllowedValues.lower(numberBD);
}
catch (NoSuchElementException e) {}
}
return result;
}
}
In SpecialValuesSpinnerEditor, we use Document Listener to have autocompletion (easy to do, just search on SO).
public class SpecialValuesSpinnerEditor extends DefaultEditor implements DocumentListener {
// You have to do in your contructor
SpecialValuesSpinnerFormatter formatter =
new SpecialValuesSpinnerFormatter (spinner.getSpecialValues(), format);
getTextField().setFormatterFactory(new DefaultFormatterFactory(formatter));
}
And now, the most important, the Formatter which does conversion between user input (string) and numbers, and handle the model's display :
public class SpecialValuesSpinnerFormatter extends NumberFormatter {
// Just override the methos StringToValue and ValueToString.
// You can check here if the value is special
// i.e you must display its special text instead. e.g. : "Auto" instead of -1
}
I think you can achieve that by implementing your own SpinnerModel and supplying that as argument to the JSpinner constructor.