Discussions related to Visual Prolog
kingchris
Active Member
Posts: 28
Joined: 26 Sep 2005 9:35

Red Cuts and Green Cuts

Unread post by kingchris »

Code: Select all

implement main     open core   constants     className = "main".     classVersion = "".   clauses     classInfo(className, classVersion).   class facts-countries     %ISOen_name,continent,midlatitude,midlongitude,maxlatitude,minlatitude,maxlongitude,minlongitude     country:(string,string,      real,          real,       real,           real,          real,               real).   class facts-coords     %    GiftId,   Latitude,Longitude,Weight,Distance     gift:(integer, real,     real,         real,    real).   class facts-lookup     %    GiftId,   Latitude,Longitude,Weight,Distance     gift1:(integer, real,     real,         real,    real).   class predicates     consult_files:().     assert_facts:().     save_facts:(string).     reduce_facts:().     test_against_all_countries_in_continent:(string).     test:(integer, real,     real,         real,    real, real,           real,          real,               real).     assert_gift:(integer, real,     real,         real,    real).   clauses     reduce_facts():-       gift(GiftId,   _, _, _,_),       retract(gift1(GiftId,   _, _, _,_)),       fail.       reduce_facts():-          ::retractFactDb(coords).       assert_gift(GiftId,Latitude,Longitude,Weight,Distance):-         gift(GiftId,Latitude,Longitude,Weight,Distance),!.       assert_gift(GiftId,Latitude,Longitude,Weight,Distance):-         assert(gift(GiftId,Latitude,Longitude,Weight,Distance)).         test(GiftId,   Latitude,Longitude,Weight,Distance, Maxlatitude,Minlatitude,Maxlongitude,Minlongitude):-         Latitude <=  Maxlatitude,         Latitude >= Minlatitude,         Longitude <= Maxlongitude,         Longitude >= Minlongitude,         assert_gift(GiftId,   Latitude,Longitude,Weight,Distance),         %console::write(GiftId,",",Latitude,",",Longitude,",",Weight,",",Distance,"\n"),         !.    test(_GiftId,_Latitude,_Longitude,_Weight,_Distance,_Maxlatitude,_Minlatitude,_Maxlongitude,_Minlongitude).         test_against_all_countries_in_continent("*"):-         gift1(GiftId,   Latitude,Longitude,Weight,Distance),         assert_gift(GiftId,   Latitude,Longitude,Weight,Distance),         %console::write(Country,Continent,Latitude,Longitude,Latitude2,Longitude2,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         fail.       test_against_all_countries_in_continent(Continent):-         gift1(GiftId,   Latitude,Longitude,Weight,Distance),         country(_Country,Continent,_MidLatitude,_MidLongitude,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         test(GiftId,   Latitude,Longitude,Weight,Distance, Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         %console::write(Country,Continent,Latitude,Longitude,Latitude2,Longitude2,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         fail.       test_against_all_countries_in_continent(_Continent).       assert_facts():-         gift(GiftId,   Latitude,Longitude,Weight,Distance),         assert(gift1(GiftId,   Latitude,Longitude,Weight,Distance)),         fail.          assert_facts():-          ::retractFactDb(coords).           save_facts(Filename):-       file::save(Filename,coords,false()).           consult_files():-       file::consult("I:\\contin.csv",countries),       file::consult("I:\\Data.csv",coords),!.       clauses     run():-         console::init(),         succeed(), % place your own code here         consult_files(),          assert_facts(),            test_against_all_countries_in_continent("AF"),          save_facts("I:\\AF.csv"),          reduce_facts(),            test_against_all_countries_in_continent("SA"),          save_facts("I:\\SA.csv"),          reduce_facts(),            test_against_all_countries_in_continent("EU"),          save_facts("I:\\EU.csv"),          reduce_facts(),            test_against_all_countries_in_continent("AS"),          save_facts("I:\\AS.csv"),          reduce_facts(),            test_against_all_countries_in_continent("AN"),          save_facts("I:\\AN.csv"),          reduce_facts(),            test_against_all_countries_in_continent("NA"),          save_facts("I:\\NA.csv"),          reduce_facts(),            test_against_all_countries_in_continent("OC"),          save_facts("I:\\OC.csv"),          reduce_facts(),            test_against_all_countries_in_continent("*"),          save_facts("I:\\RR.csv"),          reduce_facts().   end implement main   goal     mainExe::run(main::run).
All this program does is take a db of countries with which continent they are on and their min and max lat and long values, with a db of parcel deliveries to places all over the world. Its designed to split the parcels into continent files. (its not working 100% but I will fix that later). It ignores Antarctica and parts of Russia.

My question to speed it up a bit is on the following section

Code: Select all

    test_against_all_countries_in_continent(Continent):-         gift1(GiftId,   Latitude,Longitude,Weight,Distance),         country(_Country,Continent,_MidLatitude,_MidLongitude,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         test(GiftId,   Latitude,Longitude,Weight,Distance, Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         %console::write(Country,Continent,Latitude,Longitude,Latitude2,Longitude2,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         fail.
We have two backtrack points. country and gift. This all works but even after a gift/parcel has been found to fall within a country I still test it against all other remaining countries in that continent.

Is there a way to have

test(GiftId, Latitude,Longitude,Weight,Distance, Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),

cut away the country backtrack point and select the next gift/parcel.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

You can use foreach as outer loop then you can use cut in inside:

Code: Select all

test_against_all_countries_in_continent(Continent):-     foreach gift1(GiftId, Latitude, Longitude, Weight, Distance) do         country(_Country,Continent,_MidLatitude,_MidLongitude,Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         test(GiftId,   Latitude,Longitude,Weight,Distance, Maxlatitude,Minlatitude,Maxlongitude,Minlongitude),         ! % cut is possible here end foreach
However are you sure that you actually get the correct continent for counties along the Europe-Asia border. It seems to me that such surrounding boxes can/will overlap parts of adjacent continents.

Also Spain will overlap with Africa if the Canary Islands are included in their box and Denmark will overlap with North America if Greenland is in that box, etc.
Regards Thomas Linder Puls
PDC
kingchris
Active Member
Posts: 28
Joined: 26 Sep 2005 9:35

Unread post by kingchris »

Thanks for the code suggestions. I will try them out.

And yes the splitting will never be perfect but we must start somewhere and then refine it as we go along.
Post Reply