my task is to find the most probable sequences of words in a sentence using viterbi algorithm.
The given sequence of states is here:
I have to introduce initial probabilities and transition probabilities and then print the most likely sequence of parts of the speech the words of the sentence are.
The out should be smth like N P V ART, probability 0.0000123
I've hardcoded it in Java, but is there a general case or already prepared solutions in different MATH programs? thanks, here is what I have so far:
import java.util.Hashtable;
public class Main
{
//We hard-code start states and consequent observations.
static final String NOUN = "Noun";
static final String ARTICLE = "Article";
static final String VERB = "Verb";
static final String ADJECTIVE = "Adjective";
static final String ADVERB = "Adverb";
public static void main(String[] args)
{
//We put them in array
String[] states = new String[] {NOUN, ARTICLE};
String[] observations = new String[] {VERB, ADJECTIVE, ADVERB};
//Define a hashtable where we’ll keep initial states and their probabilities
Hashtable<String, Float> start_probability = new Hashtable<String, Float>();
start_probability.put(NOUN, 0.6f);
start_probability.put(ARTICLE, 0.4f);
// A hashtable with transition_probabilities which contains initial probablilities
Hashtable<String, Hashtable<String, Float>> transition_probability =
new Hashtable<String, Hashtable<String, Float>>();
Hashtable<String, Float> t1 = new Hashtable<String, Float>();
t1.put(NOUN, 0.7f);
t1.put(ARTICLE, 0.3f);
Hashtable<String, Float> t2 = new Hashtable<String, Float>();
t2.put(NOUN, 0.4f);
t2.put(ARTICLE, 0.6f);
transition_probability.put(NOUN, t1);
transition_probability.put(ARTICLE, t2);
// Here we put hashtables with consequent observed emission_probability
Hashtable<String, Hashtable<String, Float>> emission_probability =
new Hashtable<String, Hashtable<String, Float>>();
Hashtable<String, Float> e1 = new Hashtable<String, Float>();
e1.put(VERB, 0.1f);
e1.put(ADJECTIVE, 0.4f);
e1.put(ADVERB, 0.5f);
Hashtable<String, Float> e2 = new Hashtable<String, Float>();
e2.put(VERB, 0.6f);
e2.put(ADJECTIVE, 0.3f);
e2.put(ADVERB, 0.1f);
emission_probability.put(NOUN, e1);
emission_probability.put(ARTICLE, e2);
//We return the most probabilistic sequence with function forward fiterb described below
Object[] ret = forward_viterbi(observations,
states,
start_probability,
transition_probability,
emission_probability);
System.out.println(((Float) ret[0]).floatValue());
System.out.println((String) ret[1]);
System.out.println(((Float) ret[2]).floatValue());
}
//Function to go
//As argument we get nested hashtables
public static Object[] forward_viterbi(String[] obs, String[] states,
Hashtable<String, Float> start_p,
Hashtable<String, Hashtable<String, Float>> trans_p,
Hashtable<String, Hashtable<String, Float>> emit_p)
{
Hashtable<String, Object[]> T = new Hashtable<String, Object[]>();
for (String state : states)
T.put(state, new Object[] {start_p.get(state), state, start_p.get(state)});
for (String output : obs)
{
Hashtable<String, Object[]> U = new Hashtable<String, Object[]>();
for (String next_state : states)
{
float total = 0;
String argmax = "";
float valmax = 0;
float prob = 1;
String v_path = "";
float v_prob = 1;
for (String source_state : states)
{
Object[] objs = T.get(source_state);
prob = ((Float) objs[0]).floatValue();
v_path = (String) objs[1];
v_prob = ((Float) objs[2]).floatValue();
float p = emit_p.get(source_state).get(output) *
trans_p.get(source_state).get(next_state);
prob *= p;
v_prob *= p;
total += prob;
if (v_prob > valmax)
{
argmax = v_path + "," + next_state;
valmax = v_prob;
}
}
U.put(next_state, new Object[] {total, argmax, valmax});
}
T = U;
}
float total = 0;
String argmax = "";
float valmax = 0;
float prob;
String v_path;
float v_prob;
for (String state : states)
{
Object[] objs = T.get(state);
prob = ((Float) objs[0]).floatValue();
v_path = (String) objs[1];
v_prob = ((Float) objs[2]).floatValue();
total += prob;
if (v_prob > valmax)
{
argmax = v_path;
valmax = v_prob;
}
}
return new Object[]{
total, argmax, valmax
};
}
}
Related
Say I got 2 Key Value pairs:
String k1 = "a.b.c.d";
String v1 = "123";
String k2 = "a.b.c.d";
String v2 = "456";
And the desired output is:
a {
b {
c {
d = "123",
e = "456"
}
}
}
So, I've decided the split the keys by "." and form nested HashMaps and then trying to merge them when they have duplicate keys. However, it needs to merge at the leaf or innermost level instead of the outermost level.
This is the full code:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestClass {
public static void main(String []args)
{
Map<String, Object> finalMap = new HashMap<>();
Map<String, Object> outerMap1 = new HashMap<>();
Map<String, Object> outerMap2 = new HashMap<>();
String k = "a.b.c.d";
String v = "123";
outerMap1 = createNestedStructure(k, v);
k = "a.b.c.e";
v = "456";
outerMap2 = createNestedStructure(k, v);
finalMap = Stream
.concat(outerMap1.entrySet().stream(),
outerMap2.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey,
Entry::getValue, (a, b) -> {
String c = a.toString() + "\n" + b.toString();
return c;
}, HashMap::new));
System.out.println(finalMap.toString());
}
public static Map<String, Object> createNestedStructure(String k, String v)
{
String[] tokens = k.split("\\.");
Map<String, String> innerMap = new HashMap<>();
v = "\"" + v + "\"";
innerMap.put(tokens[tokens.length-1], v);
Map<String, Object> middleMap = new HashMap<>();
middleMap.put(tokens[tokens.length-2], innerMap);
for(int i=tokens.length-3; i>=0; i--)
{
Map<String, Object> middleMapTmp = new HashMap<>();
middleMapTmp.put(tokens[i], middleMap);
middleMap = middleMapTmp;
}
// Map<String, Object> outerMap = new HashMap<>();
// outerMap.put(tokens[0], middleMap);
// return outerMap;
return middleMap;
}
}
I'm not sure if this is the correct approach. So suggestions on better approaches are also welcome.
Not exactly sure about your specific problem, but you could simply insert the values into the same structure instead of merging them afterward. For example, you can make a recursive insert that creates the nested maps until it inserts your value on the last key part. If the nested map already exists it uses the existing one. Something like this could do the trick:
public static void main(String[] args) {
String k1 = "a.b.c.d";
String v1 = "123";
String k2 = "a.b.c.e";
String v2 = "456";
Map<String, Object> map = new HashMap<>();
recursiveInsert(map,k1, v1);
recursiveInsert(map,k2, v2);
System.out.println(map);
}
public static void recursiveInsert(Map<String, Object> map, String key, String value) {
int index = key.indexOf('.');
if (index == -1) {
map.put(key, value);
} else {
String subKey = key.substring(0, index);
map.putIfAbsent(subKey, new HashMap<>());
recursiveInsert((Map<String, Object>) map.get(subKey), key.substring(index + 1), value);
}
}
The output of this is what you requested:
{a={b={c={d=123, e=456}}}}
I'm trying to use ojAlgo library in Java for Integer Optimization but I'm unable to provide it the objective function I intend to.
I'd like to minimize the function: (A - B.X)'(A - B.X), where A is a (n x 1) matrix, B is a (n x n) diagonal matrix and X is a (n x 1) matrix with the optimization variables. I want the result in X to consist of only integers .
I was able to set a different objective function which was to maximize B.X. How do I change it to (A - B.X)'(A - B.X)? Here is the code so far.
import org.apache.log4j.Logger;
import org.ojalgo.optimisation.Expression;
import org.ojalgo.optimisation.ExpressionsBasedModel;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.Variable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
public class AllocationOptimization {
protected Logger log = Logger.getLogger(AllocationOptimization.class);
// This is the objective function, since weight() is assigned to it. How to change this objective function to what I want?
private List<Variable> makeVariables(HashMap<String, BigDecimal> matrixB) {
List<Variable> result = new ArrayList<>();
for (String assetName : matrixB.keySet()) {
result.add(new Variable(assetName).weight(matrixB.get(assetName)));
}
return result;
}
private ExpressionsBasedModel createExpressionModel(List<Variable> variables) {
final ExpressionsBasedModel model = new ExpressionsBasedModel();
for (Variable v : variables) {
model.addVariable(v);
}
return model;
}
private void addExpressionConstraints(ExpressionsBasedModel model, List<Variable> variables,
HashMap<String, BigDecimal> matrixB,
HashMap<String, BigDecimal> wantedAbsoluteSharesMap,
BigDecimal idealTotalPrice) {
Expression expression = model.addExpression("C1").upper(idealTotalPrice);
int i = 0;
for (String assetName : matrixB.keySet()) {
expression.set(variables.get(i), matrixB.get(assetName));
i += 1;
}
for (Variable v : variables) {
long absShares = wantedAbsoluteSharesMap.get(v.getName()).longValue();
v.lower((long) Math.max(0, 0.8 * absShares)).upper((long) Math.max(Math.max(0, 1.2 * absShares), 5));
}
}
private void setIntegerSolving(ExpressionsBasedModel model) {
for (Variable v : model.getVariables()) {
v.setInteger(true);
}
}
private HashMap<String, Long> getIntegerOptimizationResult(ExpressionsBasedModel model, HashMap<String, BigDecimal> matrixB) {
Optimisation.Result result = model.maximise();
return prepareResult(result, matrixB);
}
private HashMap<String, Long> prepareResult(Optimisation.Result result, HashMap<String, BigDecimal> matrixB) {
int i = 0;
HashMap<String, Long> optimizedResult = new HashMap<>();
BigDecimal sumAssetPrices = new BigDecimal("0.0");
for (String assetName : matrixB.keySet()) {
long sharesCount = result.get(i).longValue();
log.debug(assetName + ": " + sharesCount);
optimizedResult.put(assetName, sharesCount);
sumAssetPrices = sumAssetPrices.add(matrixB.get(assetName).multiply(BigDecimal.valueOf(sharesCount)));
i += 1;
}
log.debug("Total assets value after converting shares to integer numbers: " + sumAssetPrices);
return optimizedResult;
}
public HashMap<String, Long> optimizeSharesCount(HashMap<String, BigDecimal> constraint1,
HashMap<String, BigDecimal> matrixB,
BigDecimal constraint2) throws InputMismatchException {
List<Variable> variableList = makeVariables(matrixB);
ExpressionsBasedModel model = createExpressionModel(variableList);
addExpressionConstraints(model, variableList, matrixB, constraint1, constraint2);
setIntegerSolving(model);
HashMap<String, Long> resultMap = getIntegerOptimizationResult(model, matrixB);
return resultMap;
}
private HashMap<String, BigDecimal> createWantedAbsoluteSharesTest1() {
HashMap<String, BigDecimal> absShares = new HashMap<>();
absShares.put("NFLX", new BigDecimal("2"));
absShares.put("MSFT", new BigDecimal("4"));
absShares.put("GOOG", new BigDecimal("0"));
absShares.put("AAPL", new BigDecimal("25"));
return absShares;
}
private HashMap<String, BigDecimal> createAssetPricesMapTest1() {
HashMap<String, BigDecimal> assetPrices = new HashMap<>();
assetPrices.put("NFLX", new BigDecimal("601.06"));
assetPrices.put("MSFT", new BigDecimal("296.75"));
assetPrices.put("GOOG", new BigDecimal("2843.78"));
assetPrices.put("AAPL", new BigDecimal("149.07"));
return assetPrices;
}
public static void main(String[] args) {
AllocationOptimization allocationOptimization = new AllocationOptimization();
// For testing
HashMap<String, BigDecimal> constr1 = allocationOptimization.createWantedAbsoluteSharesTest1();
HashMap<String, BigDecimal> matrixB = allocationOptimization.createAssetPricesMapTest1();
BigDecimal constr2 = new BigDecimal("5348.25");
HashMap<String, Long> optimizedResult = null;
try {
optimizedResult = allocationOptimization.optimizeSharesCount(constr1, matrixB, constr2);
} catch (Exception e) {
e.printStackTrace();
}
assert optimizedResult != null;
allocationOptimization.log.info("optimizedResult size: " + optimizedResult.size());
}
}
You assigned weights to the Variable:s. That makes them part of the objective function. You can also assign weights to Expression:s. Anything/everything that has a weight is summed up to form the objective function.
Expression objective = model.addExpression("Whole Objective").weight(BigDecimal.ONE);
for (Variable variableR : variables) {
objective.set(variableR, linearParameter);
for (Variable variableC : variables) {
objective.set(variableR, variableC, quadraticParameter);
}
}
Is equivalent to:
Expression objective = model.addExpression("Objective Part").weight(BigDecimal.ONE);
for (Variable variableR : variables) {
variableR.weight(linearParameter);
for (Variable variableC : variables) {
objective.set(variableR, variableC, quadraticParameter);
}
}
I modified the objective function and added necessary constraints, following #apete's comments. Posting my solution here for others.
private List<Variable> makeVariables(HashMap<String, BigDecimal> matrixB) {
List<Variable> result = new ArrayList<>();
for (String assetName : matrixB.keySet()) {
result.add(new Variable(assetName));
}
return result;
}
private ExpressionsBasedModel createObjective(ExpressionsBasedModel model, List<Variable> variables,
HashMap<String, BigDecimal> matrixA,
HashMap<String, BigDecimal> matrixB) {
// Anything and everything with that has a weight is summed up to form the objective function
Expression objective = model.addExpression("Objective function").weight(BigDecimal.ONE);
for (Variable variable : variables) {
String assetName = variable.getName();
objective.set(variable, new BigDecimal("-2").multiply(matrixA.get(assetName)).multiply(matrixB.get(assetName)));
objective.set(variable, variable, matrixB.get(assetName).pow(2));
}
return model;
}
private void addExpressionConstraints(ExpressionsBasedModel model, List<Variable> variables,
HashMap<String, BigDecimal> matrixB,
HashMap<String, BigDecimal> wantedAbsoluteSharesMap,
HashMap<String, BigDecimal> matrixA,
BigDecimal idealTotalPrice, BigDecimal accountBalance) {
Expression expression1 = model.addExpression("C1").upper(idealTotalPrice);
for (Variable variable : variables) {
expression1.set(variable, matrixB.get(variable.getName()));
}
for (Variable v : variables) {
// No negative values constraint
v.lower(0);
}
// This constraint is used to compensate for the constants arising in the quadratic objective function
BigDecimal sumSquaresUserAllocation = new BigDecimal("0.0");
for (String assetName : this.assetsList) {
sumSquaresUserAllocation = sumSquaresUserAllocation.add(matrixA.get(assetName).pow(2));
}
Expression expression2 = model.addExpression("C2").upper(new BigDecimal("1.01").multiply(sumSquaresUserAllocation.multiply(new BigDecimal("-1"))));
expression2.lower(new BigDecimal("0.99").multiply(sumSquaresUserAllocation.multiply(new BigDecimal("-1"))));
for (Variable variable : variables) {
String assetName = variable.getName();
expression2.set(variable, new BigDecimal("-2").multiply(matrixA.get(assetName)).multiply(matrixB.get(assetName)));
expression2.set(variable, variable, matrixB.get(assetName).pow(2));
}
}
Finally, instead of using the model.maximise() function, I used model.minimise() to minimize the objective function.
The first parameter is a HashMap representing the selling price of each item in our store (that is given).
The second parameter is also a HashMap but this one represents our cost for us to purchase each item for our inventory.
The third parameter is also a HashMap but this one representing a customers cart.
This method computes and returns total profit for this day. Note that the profit of selling an item is the cost of a customer (prices in the first parameter) minus our cost to purchase the item our inventory (second parameter). In all computation, use the following prices.
This is what I did, not sure what I did wrong. Also is there a way I can do this without using entryset?
public static double computeProfit(HashMap<String, Double> sellingPrice, HashMap<String, Double> inventory, ArrayList<HashMap<String, Integer>> orders) {
double ans = 0;
for (HashMap<String, Integer> order : orders) {
for (Map.Entry<String, Integer> itemToQuantity : order.entrySet()) {
String item = itemToQuantity.getKey();
Integer quantityPurchased = itemToQuantity.getValue();
inventory.put(item, inventory.get(item) * quantityPurchased);
}
}
for (Entry<String, Double> itemInInventory : inventory.entrySet()) {
String itemName = itemInInventory.getKey();
Double reQuantity = itemInInventory.getValue();
ans = ans -(sellingPrice.get(itemName)+ reQuantity);
}
return ans;
}
public static ArrayList<HashMap<String,Integer>> allOrders(){
ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
HashMap<String, Integer> cart0 = new HashMap<>();
cart0.put("frozen dinner", 4);
cart0.put("yogurt", 4);
cart0.put("milk", 3);
allOrders.add(cart0);
HashMap<String, Integer> cart1 = new HashMap<>();
cart1.put("yogurt", 4);
cart1.put("strawberries", 3);
cart1.put("apples", 4);
allOrders.add(cart1);
return allOrders;
}
public static HashMap<String,Double>sellingPrice(){
HashMap<String,Double>priceList = new HashMap<String,Double>();
priceList.put("eggs", 1.79);
priceList.put("orange juice",2.5);
priceList.put("yogurt",1.99);
priceList.put("bread",2.49);
priceList.put("butter",2.39);
priceList.put("peppers",1.49);
priceList.put("chips",2.95);
priceList.put("chocolate chips",2.39);
priceList.put("popcorn",1.99);
priceList.put("tomato sauce",0.99);
priceList.put("frozen pizza",5.49);
priceList.put("milk",2.09);
priceList.put("bananas",0.49);
priceList.put("hot dog",1.29);
priceList.put("relish",0.99);
priceList.put("frozen dinner",2.5);
priceList.put("cereal",3.25);
priceList.put("tuna fish",0.99);
priceList.put("coffee",2.0);
priceList.put("pasta",0.99);
priceList.put("strawberries",3.5);
priceList.put("apples",1.29);
priceList.put("sugar",1.99);
priceList.put("ketchup",2.89);
return sellingPrice();
}
public static HashMap<String, Double> purchasePrice(){
HashMap<String,Double>cart = new HashMap <String,Double>();
cart.put("eggs", 1.2);
cart.put("orange juice", 1.0);
cart.put("yogurt", 1.0);
cart.put("bread", 1.5);
cart.put("butter", 1.95);
cart.put("peppers", 0.99);
cart.put("chips", 0.9);
cart.put("chocolate chips", 1.79);
cart.put("popcorn", 0.99);
cart.put("tomato sauce", 0.4);
cart.put("frozen pizza", 2.6);
cart.put("milk", 1.89);
cart.put("bananas", 0.39);
cart.put("hot dog", 0.79);
cart.put("relish", 0.49);
cart.put("frozen dinner", 1.5);
cart.put("cereal", 1.55);
cart.put("tuna fish", 0.49);
cart.put("coffee", 0.6);
cart.put("pasta", 0.5);
cart.put("strawberries", 1.99);
cart.put("apples", 0.99);
cart.put("sugar", 1.5);
cart.put("sugar", 0.98);
return purchasePrice();
}
public static void main(String [] args){
computeProfit(sellingPrice(),purchasePrice(),allOrders());
}
I also tried this , what am I doing wrong here?
public static double ComputeProfit(HashMap sellingPrice, HashMap inventory, ArrayList> orders) {
double ans = 0;
int i =0, j = 0;
for(i=0;i< orders.size();i++) {
HashMap<String,Integer> temp = orders.get(i);
for (j=0;j < temp.size();j++) {
String item = temp.getKey(j);
Integer quantityPurchased = temp.getValue(j);
inventory.put(item, inventory.get(item) * quantityPurchased);
}
}
int k=0 ;
for (k =0 ; k< inventory.size();k++) {
HashMap <String,Integer>itemInInventory = inventory.get(k);
String itemName = itemInInventory.getKey(k);
Double reQuantity = itemInInventory.getValue(k);
ans = ans -(sellingPrice.get(itemName)+ reQuantity);
}
return ans;
}
public static ArrayList<HashMap<String,Integer>> allOrders(){
ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
HashMap<String, Integer> cart0 = new HashMap<>();
cart0.put("frozen dinner", 4);
cart0.put("yogurt", 4);
cart0.put("milk", 3);
allOrders.add(cart0);
HashMap<String, Integer> cart1 = new HashMap<>();
cart1.put("yogurt", 4);
cart1.put("strawberries", 3);
cart1.put("apples", 4);
allOrders.add(cart1);
return allOrders;
}
public static HashMap<String,Double>sellingPrice(){
HashMap<String,Double>priceList = new HashMap<String,Double>();
priceList.put("eggs", 1.79);
priceList.put("orange juice",2.5);
priceList.put("yogurt",1.99);
priceList.put("bread",2.49);
priceList.put("butter",2.39);
priceList.put("peppers",1.49);
priceList.put("chips",2.95);
priceList.put("chocolate chips",2.39);
priceList.put("popcorn",1.99);
priceList.put("tomato sauce",0.99);
priceList.put("frozen pizza",5.49);
priceList.put("milk",2.09);
priceList.put("bananas",0.49);
priceList.put("hot dog",1.29);
priceList.put("relish",0.99);
priceList.put("frozen dinner",2.5);
priceList.put("cereal",3.25);
priceList.put("tuna fish",0.99);
priceList.put("coffee",2.0);
priceList.put("pasta",0.99);
priceList.put("strawberries",3.5);
priceList.put("apples",1.29);
priceList.put("sugar",1.99);
priceList.put("ketchup",2.89);
return sellingPrice();
}
public static HashMap<String, Double> purchasePrice(){
HashMap<String,Double>cart = new HashMap <String,Double>();
cart.put("eggs", 1.2);
cart.put("orange juice", 1.0);
cart.put("yogurt", 1.0);
cart.put("bread", 1.5);
cart.put("butter", 1.95);
cart.put("peppers", 0.99);
cart.put("chips", 0.9);
cart.put("chocolate chips", 1.79);
cart.put("popcorn", 0.99);
cart.put("tomato sauce", 0.4);
cart.put("frozen pizza", 2.6);
cart.put("milk", 1.89);
cart.put("bananas", 0.39);
cart.put("hot dog", 0.79);
cart.put("relish", 0.49);
cart.put("frozen dinner", 1.5);
cart.put("cereal", 1.55);
cart.put("tuna fish", 0.49);
cart.put("coffee", 0.6);
cart.put("pasta", 0.5);
cart.put("strawberries", 1.99);
cart.put("apples", 0.99);
cart.put("sugar", 1.5);
cart.put("sugar", 0.98);
return purchasePrice();
}
public static void main(String [] args){
computeProfit(sellingPrice(),purchasePrice(),allOrders());
}
}
Your answer looks mostly right, there are just a few issues with your type naming (Integer should be int, Double should be double) and you use Map.Entry in one portion of the code and Entry later on. The code below fixes the issues in your computeProfit function and should solve your problem. Let me know if you have any questions!
public static double computeProfit(HashMap<String, double> sellingPrice, HashMap<String, double> inventory, ArrayList<HashMap<String, int>> orders) {
double profit = 0;
for (HashMap<String, int> order: Orders) {
for (Map.Entry<String, int> orderEntry: order.entrySet()) {
double purchasePrice = inventory.get(orderEntry.getKey());
double salePrice = sellingPrice.get(orderEntry.getKey());
int purchaseCount = orderEntry.getValue();
profit += ( salePrice - purchasePrice ) * purchaseCount;
}
}
return profit;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wrote a program which constituted by several class,but the calculation is too slow(Program in bold), I hope get my java program running on GPU to speed up the computation,or is there another way to speed up the running speed,How do I change my code?
Calculation of the program are as follows:
public class ComputeThreadPool {
public static double[][] distance = new double[40][8];
public static HashMap<String,Double> simMap = new HashMap<String,Double>();
static class WorkThread implements Runnable {
private Map<String, Double> testWordTFMap;
private Map<String, Double> trainWordTFMap;
private Map<String, double[]> words;
private String trainname;
public WorkThread(Map<String, Double> map1, Map<String, Double> map2, Map<String, double[]> words,String trainname) {
this.testWordTFMap = map1;
this.trainWordTFMap = map2;
this.words = words;
this.trainname=trainname;
}
#Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
double mul = 0, testAbs = 0, trainAbs = 0;
WordsSimilarity computeS = new WordsSimilarity();
double wf = 0;
Set<Map.Entry<String, Double>> testWordTFMapSet = testWordTFMap.entrySet();
for (Iterator<Map.Entry<String, Double>> it = testWordTFMapSet.iterator(); it.hasNext(); ) {
Map.Entry<String, Double> me = it.next();
Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
***for (Iterator<Map.Entry<String, Double>> it2 = trainWordTFMapSet.iterator(); it2.hasNext(); ) {
Map.Entry<String, Double> me2 = it2.next();
wf = computeS.similarity(me.getKey(), me2.getKey(), words);
if (wf > 0.45)
mul += wf * me.getValue() * me2.getValue();
}
}***
for (Iterator<Map.Entry<String, Double>> it3 = testWordTFMapSet.iterator(); it3.hasNext(); ) {
Map.Entry<String, Double> me3 = it3.next();
testAbs += me3.getValue() * me3.getValue();
}
testAbs = Math.sqrt(testAbs);
Set<Map.Entry<String, Double>> trainWordTFMapSet = trainWordTFMap.entrySet();
for (Iterator<Map.Entry<String, Double>> it4 = trainWordTFMapSet.iterator(); it4.hasNext(); ) {
Map.Entry<String, Double> me4 = it4.next();
trainAbs += me4.getValue() * me4.getValue();
}
trainAbs = Math.sqrt(trainAbs);
simMap.put(trainname,mul / (testAbs * trainAbs));
System.out.println(Thread.currentThread().getName() + " Start. " );
processCommand();
System.out.println(Thread.currentThread().getName() + " End.");
}
private void processCommand() {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static HashMap<String,Double> main(Map<String, Double> testWordTFMap,Map<String, TreeMap<String, Double>> trainFileNameWordTFMap,Map<String, double[]> words) {
int num=0;
ExecutorService executor = Executors.newFixedThreadPool(6);
Set<Map.Entry<String,TreeMap<String,Double>>> trainFileNameWordTFMapSet = trainFileNameWordTFMap.entrySet();
for(Iterator<Map.Entry<String,TreeMap<String,Double>>> it = trainFileNameWordTFMapSet.iterator(); it.hasNext();){
Map.Entry<String, TreeMap<String,Double>> me = it.next();
num=num++;
Runnable worker = new WorkThread(testWordTFMap,me.getValue(),words,me.getKey());
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
return simMap;
}
}
wf is calculated as follows:
public static double similarity(String word1, String word2,Map<String, double[]> words) {
double[] count1=words.get(word1);
double[] count2=words.get(word2);
double sum=0;
double Abs1=0;
double Abs2=0;
if(count1 == null || count2 == null) {
return 0;
}
for (int c = 0; c < count1.length; c++) {
sum += count1[c] * count2[c];
Abs1 += count1[c] * count1[c];
Abs2 += count2[c] * count2[c];
}
return sum / (Abs1 * Abs2);
}
You would need to find an implementation of the JVM that runs on the GPU or a runtime environment/shell that targets the GPU in which you can run the standard JVM; but unless the JVM is built for the GPU you may or may not get performance gains.
However I would say, you should be able to find optimisations within the code first. Such as using enhanced for loops. Other than the compute word similarity there doesn't seem to much there that should be causing excessive run time.
I want to read data from a file and construct a graph from it. I did everything, all vertices are created normally, but when I add them to the graph, their adjacent lists (which are maps, whose key value is adjacent vertex's number, and value is their distance) become empty. Can anyone, please, tell what's the problem with my code?
public class Vertex {
private int number;
private LinkedHashMap<Integer, Integer> adjacent;
public Vertex(int num) {
this.number = num;
this.adjacent = new LinkedHashMap<Integer, Integer>();
}
}
public class Graph {
private ArrayList<Vertex> vertices;
private int verticesSize = 201;
public Graph() {
Vertex initialVertex = new Vertex(0);
this.vertices = new ArrayList<Vertex>();
for(int i = 0; i < verticesSize; i++) {
vertices.add(i, initialVertex);
}
}
}
public class Test {
public static void printGraph(Graph graph) {
for(int i = 0; i < graph.getVerticesSize(); i++)
System.out.println(graph.getVertices().get(i));
}
public static void main(String[] args) throws IOException {
FileInputStream fStream = new FileInputStream("C:/Lusine/Programming/Java/dijkstraData.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader bReader = new BufferedReader(new InputStreamReader(fStream));
Graph graph = new Graph();
String[] maps;
String line;
LinkedHashMap<Integer, Integer> currentMap = new LinkedHashMap<Integer, Integer>();
while( (line = bReader.readLine()) != null) {
maps = line.split("\t");
int firstDigit = Integer.parseInt(maps[0]);
Vertex v = new Vertex(firstDigit);
for(int i = 1; i < maps.length; i++) {
String[] vertexDistance = maps[i].split(",");
int vertex = Integer.parseInt(vertexDistance[0]);
int distance = Integer.parseInt(vertexDistance[1]);
currentMap.put(vertex, distance);
}
v.setAdjacent(currentMap);
graph.getVertices().set(firstDigit, v);
System.out.println("\n" + firstDigit +"-th vertex is\n" + v);
currentMap.clear();
}
printGraph(graph);
}
when I print v, it's ok, but when I print graph, all adjacent lists are empty. What's the problem?
Your loop boils down to
LinkedHashMap<Integer, Integer> currentMap = new LinkedHashMap<Integer, Integer>();
while ( ... ) {
Vertex v = new Vertex(...);
v.setAdjacent(currentMap);
currentMap.clear();
}
So, you're storing the same map of adjacent vertices in every vertex, and you clear this map at the end of each iteration. So obviously, all the vertices share the same, empty map, at the end of the loop.
You should create a new LinkedHashMap at every iteration:
while ( ... ) {
LinkedHashMap<Integer, Integer> currentMap = new LinkedHashMap<Integer, Integer>();
Vertex v = new Vertex(...);
v.setAdjacent(currentMap);
}
And you should not clear it, cince clearing it, well... clears it.