Module Lab Assessment 1: Algorithms and Complexities
Product.java
public class Product {
private String name;
private double price;
private int manufacturerCode;
private int dataMatrixColumn;
private int dataMatrixIndex;
public Product(String name, double price, int manufacturerCode, int dataMatrixColumn) {
super();
this.name = name;
this.price = price;
this.manufacturerCode = manufacturerCode;
this.dataMatrixColumn = dataMatrixColumn;
this.dataMatrixIndex = this.dataMatrixColumn -1;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getManufacturerCode() {
return manufacturerCode;
}
public int getDataMatrixIndex() {
return dataMatrixIndex;
}
}SimilarCustomer.java
public class SimilarCustomer {
/**
* This methods finds the most similar customer to the given customer using
* Euclidean distance
*
* @param customerBehaviorMatrix data from online store
* @param currentCustomerIndex current customer visiting the online store
* @return index of the most similar customer wrt given customer index
*/
public int findMostSimilarCustomer(double[][] customerBehaviorMatrix, int currentCustomerIndex) {
//2. Normalize the dataset
//3. Calculate similarity using Euclidean distance
// return mostSimilarCustomerIndex
return null;
}
/**
* This method normalizes given customer behavior matrix
* @param customerBehaviorMatrix customer Behavior data
* @return normalized customer behavior matrix
*/
public double[][] calculateNormalizedCustomerBehaviorMatrix(
double[][] customerBehaviorMatrix) {
//define a new return matrix
// for each entry normalize value and store in return matrix
//Assign the normalized value
return null;
}
/**
* This method finds min and max values of the customer behavior matrix
* and stores them in a matrix
*
* @param costumerBehaviourMatrix data from online store
* @return 2 row matrix with first row showing min of each column and
* second row showing max of each
*/
public double[][] findMinMaxValuesOfEachColumn(double[][] customerBehaviorMatrix) {
//define a new return matrix
//assign min and max values to return matrix
//for each column find min and max
//check min
//store min in the corresponding return matrix
//check max
//store max in the corresponding return matrix
return null;
}
/**
* This method gets a customer index and normalized customer behavior data then
* returns the most similar customer to given one
* @param normalizedCustomerBehaviorMatrix normalized customer behavior data
* @param currentCustomerIndex index of the customer which will be the base of search
* @return index of most similar other customer
*/
public int findMostSimilarCustomerIndex(double[][] normalizedCustomerBehaviorMatrix,
int currentCustomerIndex) {
return -1;
}
/**
* This methods calculates Euclidean distance between two equally size vector
*
* @param vectorOne First vector
* @param vectorTwo Second Vector
* @return distance (double) between given two vectors
*/
public double calculateEuclideanDistance(double[] vectorOne, double[] vectorTwo) {
//check if vectors have same length
return -1;
}
public static void main(String[] args) {
/*
* This main method is a stub.
* It does nothing.
* Feel free to write your own code to test your implementation.
* In this case, we have nothing actionable in here, just this comment block, so the JVM should rapidly lose interest and move on to the rest of your code.
*/
}
}SimilarCustomerOld.java
public class SimilarCustomerOld {
public int findMostSimilarCustomer(double[][] customerBehaviorMatrix, int currentCustomerIndex) {
//1. find the min max values
double[][] minMaxValuesMatrix = new double[2][customerBehaviorMatrix[0].length];
//assign min and max values to return matrix
for (int j = 0; j < minMaxValuesMatrix[0].length; j++) {
minMaxValuesMatrix[0][j] = Double.MAX_VALUE;
minMaxValuesMatrix[1][j] = Double.MIN_VALUE;
}
//for each column find min and max
for (int i = 0; i < customerBehaviorMatrix.length; i++) {
for (int j = 0; j < customerBehaviorMatrix[i].length; j++) {
//check min
if (customerBehaviorMatrix[i][j] < minMaxValuesMatrix[0][j]) {
//store min in the corresponding return matrix
minMaxValuesMatrix[0][j] = customerBehaviorMatrix[i][j];
}
//check max
if (customerBehaviorMatrix[i][j] > minMaxValuesMatrix[1][j]) {
//store max in the corresponding return matrix
minMaxValuesMatrix[1][j] = customerBehaviorMatrix[i][j];
}
}
}
//2. Normalize the dataset
double[][] normalizedCustomerbehavior =
new double[customerBehaviorMatrix.length][customerBehaviorMatrix[0].length];
// for each entry normalize value and store in return matrix
for (int j = 0; j < normalizedCustomerbehavior[0].length; j++) {
double min = minMaxValuesMatrix[0][j];
double max = minMaxValuesMatrix[1][j];
for (int i = 0; i < normalizedCustomerbehavior.length; i++) {
double value = customerBehaviorMatrix[i][j];
//Assign the normalized value
if ((max-min)==0) {
normalizedCustomerbehavior[i][j] = 0;
} else {
normalizedCustomerbehavior[i][j] = (value - min)/(max-min);
}
}
}
//3. Calculate similarity using Euclidean distance
double similarityMatrix[][] =
new double[normalizedCustomerbehavior.length][normalizedCustomerbehavior.length];
// for each customer
for (int i = 0; i < similarityMatrix.length; i++) {
// for each customer
for (int i2 = 0; i2 < similarityMatrix.length; i2++) {
//define sum of squares
double sum = 0;
//for each behavior
for (int j = 0; j < normalizedCustomerbehavior[i].length; j++) {
//calculate difference
double difference = (normalizedCustomerbehavior[i][j]-normalizedCustomerbehavior[i2][j]);
//square the difference
double squareOfDifference = difference*difference;
//add to the sum
sum += squareOfDifference;
}
//add square root of sum as similarity
similarityMatrix[i][i2] = Math.sqrt(sum);
}
}
//4. find similar (other) customer; min distance
int returnIndex = -1;
double distance = Double.MAX_VALUE;
for (int j = 0; j < similarityMatrix[currentCustomerIndex].length; j++) {
if (currentCustomerIndex != j && similarityMatrix[currentCustomerIndex][j] < distance) {
returnIndex = j;
distance = similarityMatrix[currentCustomerIndex][j];
}
}
return returnIndex;
}
public static void main(String[] args) {
/*
* This main method is a stub.
* It does nothing.
* Feel free to write your own code to test your implementation.
* In this case, we have nothing actionable in here, just this comment block, so the JVM should rapidly lose interest and move on to the rest of your code.
*/
}
}SuggestionFrames.java
import java.util.ArrayList;
import java.util.Iterator;
public class SuggestionFrames {
/**
* This method gets the visiting customer index and the product index of requested page
* and using previous data collected it produces product suggestions from the same seller
* or brand
*
* @param currentProductIndex the product index of requested web page (at least 11)
* @param currentCustomerIndex the visiting customer
* @param customerBehaviorMatrix the previous data collected
* @param orderedProductList list of all products ordered wrt dataMatrixIndex of product
*
* @return list of 5 product to be suggested from same brand/seller/manufacturer;
* not containing the product of requested page, and if suggestions are less than 5
* products from same brand/seller/manufacturer are added to the list until list size is five.
*/
public ArrayList<Product> otherProductsOfTheSellerList(int currentProductIndex,
int currentCustomerIndex, double[][] customerBehaviorMatrix,
ArrayList<Product> orderedProductList) {
//define return index; 5 products
/*
* find product from index; product list is ordered wrt column index
* so list index will be 11 smaller
*/
//find the most similar other customer
//get current customer vector
//get similar customer vector
//find suggestions
//start from 10 since the first 10 columns are not products
//product list is ordered wrt column index so list index will be 10 smaller
//remove excess if list is larger than 5
// and add if list is smaller than 5
return null;
}
/**
* This method gets the visiting customer index and the product index of requested page
* and using previous data collected it produces product suggestions based on previous
* sales of similar customer
*
* @param currentProductIndex the product index of requested web page (at least 11)
* @param currentCustomerIndex the visiting customer
* @param customerBehaviorMatrix the previous data collected
* @param orderedProductList list of all products ordered wrt dataMatrixIndex of product
*
* @return list of 5 product to be suggested based on previous sales of similar customer;
* not containing the product of requested page, and if suggestions are less than 5,
* more products are added to the list until list size is five.
*/
public ArrayList<Product> interestedProductsSuggestionList(int currentProductIndex,
int currentCustomerIndex, double[][] customerBehaviorMatrix,
ArrayList<Product> orderedProductList) {
//define return index; 5 products
/*
* find product from index; product list is ordered wrt column index
* so list index will be 11 smaller
*/
//find the most similar other customer
//get current customer vector
//get similar customer vector
//find suggestions
//start from 10 since the first 10 columns are not products
//product list is ordered wrt column index so list index will be 10 smaller
//remove excess if list is larger than 5
// and add if list is smaller than 5
return null;
}
}Last updated