I'd faced a similar dilemma a few months back, and I'm not even a Buddhist or a vegan, let alone a Jain. A troop had taken to marching up the pantry wall in a neat line. The girls – all worker ants are female – were clearly following a trail to something. However, since I couldn't find what it was – not the usual suspects, like the honey – it didn't seem a problem. I went about my business and they went about theirs and in a while they were gone. Occasionally, I have had to take drastic steps. One long, wet summer, I wrangled the legs of my desk into takeaway containers filled with water to keep at bay the lines of bookish ants that had suddenly appeared in the study. We have mixed feelings about ants. They belong to the same order – Hymenoptera – as bees and wasps and they perform vital eco-tasks, like breaking down nature's debris, aerating soil, moving nutrients, dispersing seeds and so on. Yet we tend not to love ants the way we might love bees, prettily dancing into flowers and pollinating our crops and making honey – benefiting us more obviously, in other words – and we don't fear them as we might fear wasps. It's also hard to eat a bee or a wasp accidentally, whereas it's quite easy for an ant to introduce an unwelcome note of formic acid into your marmalade. From a distance, we admire their harmony, their husbandry, their non-hierarchical and self-managing social order. Hence the Biblical exhortation: "Go to the ant, thou sluggard; consider her ways, and be wise: which having no guide, overseer, or ruler, provideth her meat in the summer and gathereth her food in the harvest." This admiration may be one of the reasons my brother and I were given ant farms as children. ("See! The ants aren't complaining about having to do chores.") A bull ant (held with tweezers). Credit:Wolter Peeters Yes, all very admirable except, it seems, when they "invade our space". Then a sort of hysteria sets in; the reactions violent and outsized. One woman in a blog described them as "disgusting" and the internet is loaded with people taking a perverse glee in sharing fiendish ways to kill them. It was telling that when I googled "Do ants sleep at night?", a cascade of ads listing "Top Ant Killing Solutions" appeared before the answer surfaced, as if the only reason you'd want to know if they slept was so you could surprise them, groggy-headed, with a fatal night raid.

In fact, the reason I wanted to know was simple. The night before, while stepping barefoot onto the grassy verge to admire my string of Christmas lights, I'd felt a sensation very like a red-hot nail ramming into my foot, leaving the sole throbbing for hours. Daylight inspection outside revealed a lot of large greenish ants running around out there. And since – here's the sleep answer – some types of worker ants only power-nap on and off (some for a minute or so, up to 253 times a day) and work 24/7, it was quite possible they were the culprits, still active at that late hour. Plus, some ants sting like a bee. The verge episode did put a dent in my kindly feelings towards ants, although I was grateful it hadn't been a funnel web. I went to school in bushland where bull ants were rife. They were given to rearing fiercely on their hind legs and jumping at you, especially if their nest was disturbed by silly boys with sticks, as it so often was. You don't forget a bull ant's sting in a hurry, although it seems it doesn't compare with the sting of the Central American bullet ant. In the 1980s and '90s, an American entomologist called Justin Schmidt developed the Schmidt Sting Pain Index forHymenoptera. The index ranks the painfulness of bee, wasp and ant stings on a four-point scale. In the interests of science, Schmidt apparently allowed himself to be stung about 1000 times by 150 different insects, which sounds bad, but then again, he was awarded the 2015 Ig Nobel Physiology and Entomology prize (a parody award for improbable research) for his efforts. Schmidt puts an ordinary old bee sting at Level 2, along with the sting of the jack jumper or hopper ant, found through Tasmania and southern Australia. He describes the pain as: "The oven mitt had a hole in it when you pulled the cookies out of the oven." The pain of the Australian bulldog ant, or red or giant bull ant, comes in at Level 3: "Bold and unrelenting. Someone is using a power drill to excavate your ingrown toenail." At Level 4 is the spider-killing wasp and the bullet ant of Central America: "Pure, intense, brilliant pain. Like walking over flaming charcoal with a three-inch nail embedded in your heel." The really bad news? It lasts 24 hours.

"None of them knows where the food is," says Palavalli-Nettimi, pictured in his lab at Macquarie University. "They just randomly go and if they find some, they lay the chemical trail back to the nest." Credit:Wolter Peeters Black storm clouds – challenging weather for ants – are massing on the day I meet Ravi Palavalli-Nettimi in the foyer of the Ecological Neuroscience lab at Macquarie University, in Sydney's north. Palavalli-Nettimi, from Bangalore in India, is in his final year of a PhD in biology here and is an ant nut. "I like Australian ants," he tells me. "They're crazy." He's wearing a pair of shorts in what looks like an ant print, although closer inspection shows they are tiny horses. Palavalli-Nettimi is studying ant size and vision as part of his research into how these tiny creatures navigate the vast world they encounter travelling to and from the nest. I suppose I had this notion of established trails but, of course, some ant or other has to set up a chemical trail in the first place. It explains why you see so many solo ants meandering around, exploring. Ant navigation systems are complex and not yet fully understood. The sun can act as a compass, because ant eyes are sensitive to its polarisation pattern, but foragers also learn and remember prominent landmarks. It's believed they rely on a kind of photographic memory – landmarks noted on the journey out and matched for the journey back – in what's called "view-based homing". They seem to reorient their bodies now and again so that the angle from which they view the landmark fits exactly with the image stored in their brain, to keep them on track towards the food source or the nest. If necessary, they can move a long way in reverse, hauling food. Equally remarkably, they also appear to count their footsteps, says CSIRO ecologist and ant specialist, Ben Hoffmann. How can they count? "That's a good question," Hoffmann says. "We don't know. In some special ant way."

All that strikes me as kind of smart, but scientists like Palavalli-Nettimi resist the urge to anthropomorphise. "Smart and dumb is a human concept," he tells me as we sit beside his small, personal box of sugar ants, including a queen he found walking the corridor one evening (she obviously didn't realise she had wandered into an ant laboratory). "What would you say is 'smart' for an ant?" Palavalli-Nettimi continues. "Coming up with beautiful architecture for a nest? For a colony, that is 'smart'; for one ant, it's not. That one ant is just following some rules, just like a robot. So much of what they do is random. For example, none of them knows where the food is. They just randomly go and if they find some, they lay the chemical trail [pheromones] back to the nest so that others can get recruited to that place. The others that follow also lay the trail and reinforce it." Perhaps it all just appears random to us? Palavalli-Nettimi says there's a difference between intelligence and instincts, or what he calls "rules". "Ants use simple rules. Like, if I cannot smell the chemical trail that I was on [because it was wiped off by a heartless housekeeper, say], the rule is 'go on a further search'. If I put the same rule into a robot, I can replicate everything that the ants do on a trail." Scientists have done just that, as well as using some of the principles of ant behaviour in basic artificial intelligence development and other technologies. Individual ants are basically working on trial and error. It's the work of all of them together that helps the colony analyse the information collected and form the big picture, whether that's the quickest way to a food source or the need to set up defences against invaders. It's a miraculous self-organising system that relies on what is known as "swarm intelligence": that mysterious collective ability of a group of insects or animals, whether it's ants, bees or a flock of birds, to find very efficient solutions to tough problems, and adapt quickly to changing circumstances. Swarm intelligence has also been used by business for, say, improving transport routing, or organising staff, logistics and inventories. For example, some years ago Hewlett-Packard, as it was then, modelled a software program on the way ants find the shortest way to food, so as to come up with the most efficient way to route phone traffic over a network.

I have brought Palavalli-Nettimi some of my ants – possibly two different species – in jars. He's going to take them away and identify them properly but I'm alarmed when he speculates one might be an Argentine ant. These foreigners are now widespread in Australia, largely because they're like cuckoos and have developed the ability to inveigle their way into any nest, undetected as an outsider. Most ants rely on chemical recognition to identify ants – or nest-mates – from their own colony. "That's why when two ants meet, you can see them doing this …" Palavalli-Nettimi waves two fingers near his head. "Antennating. Stroking their antennae." A lot of antennating seems to go on. Does it have any other purpose? "Some people have suggested the ants are taking the number of encounters into account, to judge how much food there is outside and whether to send more ants out. So, 'food' has a different smell, 'ant' has a different smell. If you encounter more ants with this combination – food and ant smells – that means there's more food." Palavalli-Nettimi suggests I could trace my ants back to their nest and then put food or repellent ("such as cinnamon or any kind of spice really") nearby to keep them there and out of the house. Easier said than done. There are little sandy anthills everywhere in my backyard, suggesting connected nests with many exits and entrances. It's all too hard. I can see that observing or running experiments on something so small and erratic calls for a certain type of nerdy enthusiasm, mixed with ingenuity and deep wells of patience, all of which I lack. One can't help but admire the persistence of people like the European PhD student who explained in a TEDx talk how she followed particular castes of ants – nurses, cleaners, foragers – in a colony. Danielle Mersch wanted to track where every ant went and how they knew what to do, without instruction or supervision. Her solution, in two experiments, was to cut out and glue tiny pieces of paper with individual barcodes onto the backs of more than 4000 ants. Sitting chilled to the bone in a cold room to slow down the ants, Mersch gently pinioned the legs of each ant, one by one, with plasticine (that's an awful lot of legs) and then glued on the bits of paper, eventually reaching a rate of 20 ants an hour. After that, there was just the matter of interpreting the nine million interactions in the colony.

It's late afternoon and the storm has reached us, cracking wildly overhead and bringing sheets of rain and even light hail. Heavy rain can flood a nest and destroy everything, including the queen. Is it true that ants know when it's going to rain, I ask Palavalli-Nettimi. "People say ants can detect the changes in the pressure before the rain comes but I'm not sure that's properly proven," he says. "They should be able to detect changes in the humidity, because they have sensors for that in their antennae and they actually start to build mounds just before it rains or close up the entrance so the nest won't flood." Well then? "It is possible," he allows. Coming back to my kitchen dilemma, what happens if you wipe out a bunch of ants or, say, move the dozens on the cat bowl so they can't find their way back? How bad is that? I think I'm seeking absolution but the scientist doesn't see this in terms of "bad" or "good", moral or immoral. "That would be more of a philosophical view," he says earnestly. "What happens? If you kill 100 ants today, it's probably going to take a month or two to make all of them and send them out again." Which isn't that big a problem for the colony as a whole. Says CSIRO's Ben Hoffmann: "It has no effect. Have you ever known anyone to say they got rid of the ants in their house by killing a few hundred? Only one per cent of the population is out foraging at any one time, so 99 per cent are back at the nest. The type of ants that come into your house are very numerous and have high reproductive rates." And forget about starving them. Even the cleanest kitchen, Hoffmann says, will have tiny scraps the ants can forage.