In: Computer Science
Problem
You have developed an application for the task of registering crop information and have now been given crop data for analysis. The crop data includes the (x, y) coordinates of a robotic weed scanner (or unmanned ground vehicle (UGV) ) as it moves through a crop. Your task is to develop a console based representation of the movement patterns of the robotic scanner. Along side coordinates, the robot has also reported weed classification results for each (x, y) coordinate. The weed identification algorithms is using a lot of resources and for this reason graphics will be avoided in place of a console representation.
The robot is programmed to move on a grid and has an algorithm that converts GPS coordinates into a grid based system. In this scenario the robot will start moving at location (0, 0) and can move 1 space at a time in the x and y dimensions. That is, it can only increment x and/or y by 1 at each step. You will not need to implement this part, a text based data file will be provided to you. Location (0, 0) will be set to the top left hand corner and the grid will be set to a fixed width and height of 10 x 10 units. The text file will contain coordinates on each line with a 3 integer classification result (x, y, R1, R2, R3). The result will require processing through a single artificial neuron based classifier, the neuron will contain 3 weights to match the 3 integers of the result.
This calculation through the neuron can be expressed:
Class = Round(R1 X W1 + R2 X W2 + R3 X W3)
The calculation should produce a result between 0 and 1, rounding will then produce 1 when the number is greater than 0.5 and 0 if it is below, for this you can use pythons inbuilt round function. Class 1 in this case will be a weed and class 0 will be no weed detected. The round function is being used in place of a step based activation function, typically depicted as g(.).
To complete this task, you will need to develop three functions that process and display the results in a console based grid. The first function will take a file name and return a 10x10 list of lists containing and representing the values from each line of the text file (x, y, R1, R2, R3). This function will be called read_file (described below) and will take 1 parameter (the file name). The result values (R1, R2, R3) will need to be stored as a list as well, this creates what is essentially a list of lists of lists. While this does sound complex, it can be created by appending the list results at each 2 dimensional index of a 2 dimensional list, which can be initialised as an empty list for each potential location in a 10x10 grid. The index will be the x and y values, that is, the first 2 values from each line of the text file.
Example, for a single set of values (1, 1, 4, 9, 1):
coord_results[x][y] = [R1, R2, R3]
or
coord_results[1][1] = [4,9,1]
Where coord_results is a 10x10 list of lists (or 2 dimensional list).
Parameter name | Description |
---|---|
file_name | String text name of the data file |
The second function will be called classify and will take the 3 result values and multiply them by their associated weight value. Using 3 iterations of a loop the values can be added together and rounded to produce a classification result. This function will take two parameters, a 3 float weight list and a 3 integer result. After the calculation the rounded result can be returned as an integer.
Parameter name | Description |
---|---|
weights | A float list containing 3 weight values |
result | An integer list containing an individual set of 3 result values |
The third function will be called display_results and this function will display the text based grid in the console. A text data file and the expected output is depicted below. The function will print 1 line of the grid at a time in a loop and will require a nested loop to loop through all possible locations in the grid (all x and all y). Inside the nested loops you will need to check a set of conditions. If the index (x,y) has an empty list (meaning the robotic scanner did not pass over it) two spaces will be drawn. If the classify function returns 1 on the result for the coordinate an ' x' will be drawn, otherwise a full stop will be drawn (' .').
Parameter name | Description |
---|---|
coord_results | A list of lists containing the (x, y, R1, R2, R3) for each line of the text data file |
weights | A float list containing the 3 weight values |
The three weight values that you will need to store in your application are provided below, these weights have been determined using a machine learning method to produce accurate classification.
An example data file is provided below with results displayed.
Note that program specifications are not always clear. If you are uncertain about any aspect, you are typically better off asking than making assumptions. Please use the appropriate discussion forum to ask for clarification, if required.
Example Interactions
Given the following list of weights:
0.03 0.04 0.03
Given the following text file (attached below - crop1.dat):
0 0 5 4 7 1 1 4 9 1 1 2 3 2 7 1 3 6 4 6 2 4 8 2 1 3 5 3 7 2 4 6 5 7 1 4 7 6 2 1 5 8 7 2 7 6 9 9 8 6 7 9 1 1 1 8 9 6 2 4 9 9 1 2 8 9 8 6 8 6 8 7 4 5 0 7 6 5 8 6 7 5 8 2 1 7 4 3 2 5 8 3 8 1 3 9 2 1 7 1
Your display output should display the following output.
x x . x . . . . . x . . x . . . . . x .
Screenshot
Program
#Function to create grid from file
def read_file(filename):
#Create 10*10 grid
grid = [[0 for _ in range(10)] for _ in
range(10)]
#File open
file=open(filename)
#Loop through each line
for line in file:
#Split each line
fields=line.split('
')
#Get x and y coordinate
in the grid
x=int(fields[0])
y=int(fields[1])
#Result list
lst=[]
lst.append(int(fields[2]))
lst.append(int(fields[3]))
lst.append(int(fields[4]))
#Add into grid
grid[x][y]=lst
#Return result co-orddinates
return grid
#Function to classify
def classify(weights,results):
#Variable for calculation
sum=0
#Loop through for each value
for i in range(len(weights)):
#Equation
calculation
sum+=weights[i]*results[i]
#Return rounded result
return round(sum)
#Function to display resulting grid
def display_results(grid,weights):
#Loop through grid
for i in range(len(grid)):
for j in
range(len(grid[i])):
#Check empty grid
if(grid[i][j]!=0):
#Otherwise check classify result and display
if(classify(weights,grid[i][j])==1):
print('X',end=' ')
else:
print('.',end=' ')
#The print empty space
else:
print(' ',end=' ')
print()
#Main function
def main():
#Initialize weight list
weights=[0.03,0.04,0.03]
#Call function to get file data and generated
grid
grid=read_file('crop1.dat')
#Display grid
display_results(grid,weights)
#Program start
main()
----------------------------------------------------------------------------
Output
X
X . X
.
.
. .
.
X
. .
X .
. . .
. X
.