LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2007, 08:27 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 32
Algorithm for optimal teritorial coverage


Hi there!
I have the following task: a country teritory is given as a matrix of 0 and 1. I must place military bases across the country - every base cover the neighbour teritory as well. All bases must be optimal placed - in other words I have to cover the whole teritory with minimum amount of bases.
To make things look clear I will apply the following example: Lets take this as input data:
Code:
0 0 0 0 1 0
0 0 0 1 1 1 
0 0 1 1 1 0 
1 1 1 1 1 0 
0 0 0 1 1 1
There is a 1 for teritory needed coverage, and 0 for teritory that doesn't need coverage (out of the country, enemy teritory, etc).
If we take the minus sign (-) as a military base, and the 'X' sign for covered teritory, this is what would we have after placing one base:
Code:
0 0 0 0 1 0 
0 0 0 1 1 1 
0 0 X X X 0 
1 1 X - X 0 
0 0 0 X X 1
The target is to make Xs from all 1s with minimum amount of minuses
I hope I'm clear in some grade...
I'm going to write that in C, and I have experience in C, but I'm poor at algorithms. Just give me an idea and I'll try to make something of it. Now I just don't know where from I have to start...
 
Old 04-09-2007, 09:27 AM   #2
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Rep: Reputation: 40
....and what a task!

Wow , it's not that common that I see such stuff , hehe
anyways , Speaking on my experience with AI I'd tend to use optimization algorithms (i.e algorithms that produce better and better results after each interval/iteration etc ) to solve this kind of task.
...and speaking on the basis of my (shall I say not so humble) knowledge of optimization algorithms
I'd tend to use Genetic algorithms or any other varient of it , like simulated annealing , hill climbing and stuff like that.

basically your Territory coverage problem has something in common with the famous knap-sack problem and/or the traveling sales person problem

google "genetic algorithms" "traveling salesperson problem" and/or "knapsack problem"
hope that gets you going!

oh before I forget and if I'm allowed to ask , why are you bothering yourself with this kinda thing?
you aren't setting a scheme for laying out military bases across bulgaria , are you? (kidding)

cheers
 
Old 04-09-2007, 01:33 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It looks like a variant of the traveling salesperson problem, so NP-complete. Do you need the best solution or just one that's correct? The simpliest algorithm would be to put -'s in random until the whole area is covered It looks like an algorithmic homework/project, so I guess you need to find out something more sophisticated. The terms entz gave you for a search are good ones. Start with that.
 
Old 04-09-2007, 11:00 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
How about the following idea...
Examine each 1 that you have and calculate the number of adjacent 1's there are.
Find the 1 with the smallest number of adjacent 1's, you want to make that an X, and you want to make it an X by finding the 1 adjacent to this with the greatest number of adjacent 1s a -
A simple greedy algorithm that might be optimal?

To run it through your example:
Code:
0 0 0 0 1 0
0 0 0 1 1 1 
0 0 1 1 1 0 
1 1 1 1 1 0 
0 0 0 1 1 1
Calculate the number of adjacent 1s
Code:
0 0 0 0 3 0
0 0 0 5 5 3 
0 0 5 7 6 0 
1 3 5 7 6 0 
0 0 0 4 4 2
So the point with 1 neighbour needs to be an X and the adjacent square becomes a -

Code:
0 0 0 0 1 0
0 0 0 1 1 1 
0 0 X 1 1 0 
X - X 1 1 0 
0 0 0 1 1 1
Now repeat the process, counting the neighbouring 1s

Code:
0 0 0 0 3 0
0 0 0 5 5 3 
0 0 X 5 6 0 
X - X 5 6 0 
0 0 0 3 4 2
Now it is the 2 that becomes an X with the 6 becoming the -, hence:

Code:
0 0 0 0 1 0
0 0 0 1 1 1 
0 0 X X X 0 
X - X X - 0 
0 0 0 X X X
Doesn't take much to work out that it will resolve in one more iteration with the final result being:
Code:
0 0 0 0 3 0
0 0 0 2 3 2 
0 0 X X X 0 
X - X X - 0 
0 0 0 X X X
Code:
0 0 0 0 X 0
0 0 0 X - X 
0 0 X X X 0 
X - X X - 0 
0 0 0 X X X
or

Code:
0 0 0 0 - 0
0 0 0 X X X 
0 0 X X X 0 
X - X X - 0 
0 0 0 X X X
Resolving ties would be an issue, I'm not certain if it would matter...?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
token bucket algorithm vs Leaky bucket algorithm xeon123 Linux - Networking 2 03-26-2007 04:57 AM
How Good is Your Wireless Coverage? saturndude Linux - Wireless Networking 2 03-21-2006 12:31 PM
Suggesting React OS coverage diddykong LQ Suggestions & Feedback 4 12-15-2005 12:58 PM
Code coverage rajsun Programming 2 06-29-2005 05:28 PM
Code Coverage tools icwong Programming 3 10-27-2003 07:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration