Freewill means when entities with freewill are put in front of true choices, they will devide. If they don't divide it means that either they don't have freewill or they don't have true choices.
If God chooses to avoid evil from happening, He will stop you before you choose to be bad. But it simultaneously means you don't have the choice of being bad. So either He chooses to stop all the bad things from happening by removing freewill and true choices from you, or He allows all happens.
He chooses the latter such that an eternity called Heaven can be built in the timeline of infinity where entites with freewill (humans and angels) will live happily together but without sinning.
Perfection refers to how a product suits its designing purpose. You won't call a mirror imperfect because it can't talk to you as talking to you is not the design purpose of a mirror. You won't call a mirror imperfect during the glass-blowing stage to say that it's too hot to be held, as it's not a product yet.
Planet earth is the glass-blowing stage of humans, only those who finally make their to Heaven will be the 'product' considered to be perfect. They will love God and worship God. They will not sin but live happily with God. That's the design purpose of humans as the final product.
Every creation is with residues, every creator of any kind knows that. If you choose to be the residues instead of God's 'product', so be it. God's intended 'product' is His sheep but not the wolves. God's intended 'product' is the wheat but not the weeds. His designed 'product' is well documented in the Book of Life of the Lamb, those who will call Him Father in Heaven.
If you deny Him to be your Father at the moment you leave this planet earth, you are not of His children but someone else's. You will be the weeds planted by the devil, the residues which every creator will separate from his product and put to a trash box. If you choose to be so, so be it.
God's foreknowledge will not contradict the law of freewill. If He does, it only means that you don't have freewill and true choices.