"We are all philosophers here where I am, and we debate among other things the question of where it is that we live. On that issue I am a liberal. I live in the interstice yes, but I live in both the city and the city." The closing lines of China Miéville's The City and the City (2009) could serve as a summary of the present state of what used to be called science fiction.

Fusing elements from works of fantasy and the pulp literature of horror that descends from H P Lovecraft, much of Miéville's earlier work was set in an alternative world, a highly coloured realm far removed from any in which human beings have ever lived. The City and the City presents the actual world subtly altered, coexisting and overlapping with another that is not so different - two cities kept apart by secret police between which the central protagonist, a dogged detective, must travel in order to resolve a murder investigation. It is a mind-opening conception, realised with enormous skill and panache.

Reading this book, you realise how much of human life - your own and that of others - passes by unseen. Yet this insight comes without any suggestion that the situation can be changed. We live not in one world, but indefinitely many; we never know when we might cross from one to another, or what might then occur. Neither can we escape. The blurred and treacherous territories of The City and the City go on for ever. There is nowhere else to go.

Throughout much of the 20th century, writers imagined alternative worlds in order to enlarge the sense of what was possible. The rich dystopian literature of the first half of the century was not only an expression of disillusionment. Novels such as We (completed in 1921), Brave New World (1932) and Nineteen Eighty-Four (1949) were meant as warnings, each depicting a type of society that could not realistically exist, in order to alert readers to dangers that were implicit in the way their contemporaries envisioned the future.

Zamyatin, Huxley and Orwell were doing more than comment on the failure to realise modern ideals, whether the ideals were those of Bentham, Pavlov and H G Wells - Huxley's targets - or those of Lenin and the Soviet Union. These writers were pointing to a risk of totalitarian control they believed to be inherent in the ideals themselves, but they believed the horrors they envisioned could be avoided.

Science fiction pursues an inquiry into what it means to be human. The role of science has been to gauge the limits of the species, with new technologies and extra-planetary environments being used as virtual laboratories for an ongoing thought experiment. If the mainstream novel employs the lens of the commonplace career - birth and education, marriage and divorce, ambition and failure - SF has pursued the inquiry by abducting the human animal and placing it in alien environments.

In a variant of this strategy, humans are given the power to fashion new species that transcend their creators' limitations. In his most disquieting fable, The Island of Dr Moreau (1896), Wells showed how such power can breed monsters - the pitiful, tormented Beast Folk, whom the narrator finds unpleasantly reminiscent of ordinary humankind. In similar vein, Olaf Stapledon's Sirius (1944) is the story of a scientifically bred dog that surpasses human beings in intelligence; the animal ends up an outlaw, hating and being hated by humankind.

If there is a lesson here, it is that the growth of knowledge increases the opportunities for savagery. Yet neither of these writers gave up believing that human beings can shape the future. Much of their work - Wells's stream of utopias, Stapledon's vistas of human beings merging into a collective super-mind to create a Godlike consciousness - is testimony to this faith.

The best-known works of post-apocalyptic fiction are no different. John Wyndham's The Day of the Triffids (1951) is full of terrifying images of human life under siege from ambulatory plants, but concludes with a handful of survivors emerging from their ordeal essentially unchanged to renew civilisation in the comically improbable environs of the Isle of Wight. Probably written around the same time, but published for the first time this year, Wyndham's Plan for Chaos is a potpourri of thriller-style adventures, mysterious doubles of the narrator and Nazi flying saucers, but the final message is much the same: keep calm and carry on. Once order is restored, all will be well.

Even at its most pessimistic, science fiction has always been a humanist genre. The consoling assumption has been that while civilisation may be flawed and fragile, it can always be rebuilt, perhaps on a better model, if only humans have the will to do it. The possibility that it is the species that is flawed has rarely been explored. Stanislaw Lem was an unyielding rationalist, but some of his most powerful works - such as The Invincible (1964), a tale of human beings baffled by alien life forms, and The Chain of Chance (1975), a spoof detective story in which a former astronaut investigates what appears to be a series of linked murders, only to find they are the product of random chemical reactions - show human reason failing when confronted by a world that can be understood only in inhuman terms, if at all.

Writing in a seemingly different genre, Mervyn Peake imagined a world without meaning and ruled by impenetrable rituals, echoes of which appear in Miéville and the work of Michael Moorcock and M John Harrison. Not influenced by Peake but like him shaped by life as a child in China, J G Ballard used post-apocalyptic science fiction to show that individual personality is a conventional makeshift that breaks down when put to the text of extreme experience.

Ballard is the pivotal figure here, and not only because of the intensity of his vision. His abandonment of science fiction for the experimental novel was more than a shift of genre. Whether they produced utopian dreams or dystopian nightmares, writers of science fiction renewed the bourgeois notion of fiction as a criticism of life. Whatever their political perspective - left-progressive like Stapledon and Wells, or right-libertarian like Robert Heinlein - they assumed collective action to direct the course of human life was possible. Ballard challenged this orthodoxy, not by shifting towards pessimism as is commonly suggested, but instead by looking for personal liberation in conditions where co-operation has irretrievably broken down. The genres that Ballard later took up and subverted - versions of the crime novel and the dark comedies enacted in his last books - show him continuing this search.

If science fiction is no longer a viable form, it is because the humanist assumptions that underpinned it are no longer credible even as fictions. The hybrid type of writing that has evolved in recent years is symptomatic. "Slipstream", "cyberpunk" and "new weird" blend together influences as diverse as Arthur Machen and Mikhail Bulgakov, Charles Williams and William S Burroughs. What these styles of writing have in common is an absence of politics. No world-changing project features in any of them. Miéville is an active member of the Socialist Workers Party, but his brand of fantastic fiction has as much to do with his political hopes as Wells's scientific fables did with his utopian schemes. Wells may have fantasised about a world government using science for the masses, but it was the clairvoyant dreams that appear in The Island of Dr Moreau that expressed his true vision.

During much of the 20th century, speculative fiction served an impulse of world transformation. Fantasy was understood as an exercise in which alternative worlds were imagined in order to create new possibilities of action. Today fantasy has the role of enabling us to see more clearly the elusive actualities. The question of action is left open. We debate what can be done to change the world, but no one expects an answer. Whether we like it or not, on this issue we are all liberals.

John Gray is lead book reviewer of the NS

“Gray's Anatomy: Selected Writings" is published in paperback by Penguin (£10.99)