pub fn solve_with_constraints(
		&self,
	) -> Result<
		(R, SolutionView<Tq, R>, Vec<&Tc>),
		<T as SolverGenerator<'static, FixedSingleQuadricModel<Binary<R>>>>::ErrorType,
	> {
		let ph = self.model.get_placeholders();
		let mut ret = None;
		let qubit_map: HashMap<Tq, usize> = self
			.qubits
			.iter()
			.enumerate()
			.filter_map(|(i, q)| {
				if let Qubit::Qubit(q) = q {
					Some((q.clone(), i))
				} else {
					None
				}
			})
			.collect();
		for _ in 0..self.iterations {
			let mut phdict: HashMap<&Placeholder<(), Tc>, usize> =
				ph.iter().map(|p| (*p, 10)).collect();
			let mut size = ph.len() * 10;
			let mut old_energy = R::MAX;
			for _ in 0..self.generations {
				let (c, model) = self.model.generate_qubo(&self.qubits, &mut |p| {
					if let Some(cnt) = phdict.get(&p) {
						R::from_i32(*cnt as i32) / R::from_i32(size as i32) * self.coeff_strength
					} else {
						panic!()
					}
				});
				let fut_ret = std::iter::repeat_with(|| {
					self.solver_generator.generate(unsafe {
						// SAFETY: model lives longer than solver
						std::mem::transmute(&model as *const FixedSingleQuadricModel<_>)
					})
				})
				.take(self.samples)
				.collect::<Result<Vec<_>, _>>()?
				.par_iter()
				.map(|solver| {
					let mut r = StdRng::from_rng(OsRng).unwrap();
					solver.solve_with_rng(&mut r).map(|v| v.into_iter())
				})
				.collect::<Result<Vec<_>, _>>()?
				.into_iter()
				.flat_map(std::convert::identity)
				.map(|sol| sol.with_energy(&model))
				.collect::<Vec<_>>();
				let min: f64 = fut_ret
					.iter()
					.fold(0.0 / 0.0, |m, v| v.energy.unwrap().as_f64().min(m));
				assert!(min.is_finite());
				let sol = fut_ret
					.into_iter()
					.filter(|r| r.energy.unwrap().as_f64() == min)
					.next()
					.unwrap();
				let energy = sol.energy.unwrap();
				// println!("{}, {}, {}", min, old_energy, energy);
				if old_energy <= energy {
					continue;
				}
				old_energy = energy;
				let ans: HashMap<&Qubit<Tq>, bool> = self
					.qubits
					.iter()
					.enumerate()
					.map(|(i, q)| (*q, sol[i]))
					.collect();
				let mut constraint_labels = Vec::new();
				for c in self.model.get_unsatisfied_constraints(&ans) {
					if let Some(ph) = &c.placeholder {
						if let Some(point) = phdict.get_mut(ph) {
							*point += 1;
							size += 1;
						}
					}
					if let Some(label) = &c.label {
						constraint_labels.push(label);
					}
				}
				let is_satisfied = constraint_labels.len() == 0;
				ret = Some((
					energy + c,
					SolutionView::new(sol.with_local_field(&model), qubit_map.clone()),
					constraint_labels,
				));
				if is_satisfied {
					return Ok(ret.unwrap());
				}
			}
		}
		Ok(ret.unwrap())
	}
/// Solve the model using internal annealer.
Solve the model using internal annealer.
Solve the model using internal annealer .
pub fn solve_with_constraints(
		&self,
	) -> Result<
		(R, SolutionView<Tq, R>, Vec<&Tc>),
		<T as SolverGenerator<'static, FixedSingleQuadricModel<Binary<R>>>>::ErrorType,
	> {
		let ph = self.model.get_placeholders();
		let mut ret = None;
		let qubit_map: HashMap<Tq, usize> = self
			.qubits
			.iter()
			.enumerate()
			.filter_map(|(i, q)| {
				if let Qubit::Qubit(q) = q {
					Some((q.clone(), i))
				} else {
					None
				}
			})
			.collect();
		for _ in 0..self.iterations {
			let mut phdict: HashMap<&Placeholder<(), Tc>, usize> =
				ph.iter().map(|p| (*p, 10)).collect();
			let mut size = ph.len() * 10;
			let mut old_energy = R::MAX;
			for _ in 0..self.generations {
				let (c, model) = self.model.generate_qubo(&self.qubits, &mut |p| {
					if let Some(cnt) = phdict.get(&p) {
						R::from_i32(*cnt as i32) / R::from_i32(size as i32) * self.coeff_strength
					} else {
						panic!()
					}
				});
				let fut_ret = std::iter::repeat_with(|| {
					self.solver_generator.generate(unsafe {
						// SAFETY: model lives longer than solver
						std::mem::transmute(&model as *const FixedSingleQuadricModel<_>)
					})
				})
				.take(self.samples)
				.collect::<Result<Vec<_>, _>>()?
				.par_iter()
				.map(|solver| {
					let mut r = StdRng::from_rng(OsRng).unwrap();
					solver.solve_with_rng(&mut r).map(|v| v.into_iter())
				})
				.collect::<Result<Vec<_>, _>>()?
				.into_iter()
				.flat_map(std::convert::identity)
				.map(|sol| sol.with_energy(&model))
				.collect::<Vec<_>>();
				let min: f64 = fut_ret
					.iter()
					.fold(0.0 / 0.0, |m, v| v.energy.unwrap().as_f64().min(m));
				assert!(min.is_finite());
				let sol = fut_ret
					.into_iter()
					.filter(|r| r.energy.unwrap().as_f64() == min)
					.next()
					.unwrap();
				let energy = sol.energy.unwrap();
				// println!("{}, {}, {}", min, old_energy, energy);
				if old_energy <= energy {
					continue;
				}
				old_energy = energy;
				let ans: HashMap<&Qubit<Tq>, bool> = self
					.qubits
					.iter()
					.enumerate()
					.map(|(i, q)| (*q, sol[i]))
					.collect();
				let mut constraint_labels = Vec::new();
				for c in self.model.get_unsatisfied_constraints(&ans) {
					if let Some(ph) = &c.placeholder {
						if let Some(point) = phdict.get_mut(ph) {
							*point += 1;
							size += 1;
						}
					}
					if let Some(label) = &c.label {
						constraint_labels.push(label);
					}
				}
				let is_satisfied = constraint_labels.len() == 0;
				ret = Some((
					energy + c,
					SolutionView::new(sol.with_local_field(&model), qubit_map.clone()),
					constraint_labels,
				));
				if is_satisfied {
					return Ok(ret.unwrap());
				}
			}
		}
		Ok(ret.unwrap())
	}
pub fn solve_with_constraints ( & self , ) -> Result < ( R , SolutionView < Tq , R > , Vec < & Tc > ) , < T as SolverGenerator < ' static , FixedSingleQuadricModel < Binary < R > > > > :: ErrorType , > { let ph = self . model . get_placeholders ( ) ; let mut ret = None ; let qubit_map : HashMap < Tq , usize > = self . qubits . iter ( ) . enumerate ( ) . filter_map ( | ( i , q ) | { if let Qubit :: Qubit ( q ) = q { Some ( ( q . clone ( ) , i ) ) } else { None } } ) . collect ( ) ; for _ in 0 .. self . iterations { let mut phdict : HashMap < & Placeholder < ( ) , Tc > , usize > = ph . iter ( ) . map ( | p | ( * p , 10 ) ) . collect ( ) ; let mut size = ph . len ( ) * 10 ; let mut old_energy = R :: MAX ; for _ in 0 .. self . generations { let ( c , model ) = self . model . generate_qubo ( & self . qubits , & mut | p | { if let Some ( cnt ) = phdict . get ( & p ) { R :: from_i32 ( * cnt as i32 ) / R :: from_i32 ( size as i32 ) * self . coeff_strength } else { panic ! ( ) } } ) ; let fut_ret = std :: iter :: repeat_with ( | | { self . solver_generator . generate ( unsafe { // SAFETY: model lives longer than solver std :: mem :: transmute ( & model as * const FixedSingleQuadricModel < _ > ) } ) } ) . take ( self . samples ) . collect :: < Result < Vec < _ > , _ > > ( ) ? . par_iter ( ) . map ( | solver | { let mut r = StdRng :: from_rng ( OsRng ) . unwrap ( ) ; solver . solve_with_rng ( & mut r ) . map ( | v | v . into_iter ( ) ) } ) . collect :: < Result < Vec < _ > , _ > > ( ) ? . into_iter ( ) . flat_map ( std :: convert :: identity ) . map ( | sol | sol . with_energy ( & model ) ) . collect :: < Vec < _ > > ( ) ; let min : f64 = fut_ret . iter ( ) . fold ( 0.0 / 0.0 , | m , v | v . energy . unwrap ( ) . as_f64 ( ) . min ( m ) ) ; assert ! ( min is_finite ( ) ) ; let sol = fut_ret . into_iter ( ) . filter ( | r | r . energy . unwrap ( ) . as_f64 ( ) == min ) . next ( ) . unwrap ( ) ; let energy = sol . energy . unwrap ( ) ; // println!("{}, {}, {}", min, old_energy, energy); if old_energy <= energy { continue ; } old_energy = energy ; let ans : HashMap < & Qubit < Tq > , bool > = self . qubits . iter ( ) . enumerate ( ) . map ( | ( i , q ) | ( * q , sol [ i ] ) ) . collect ( ) ; let mut constraint_labels = Vec :: new ( ) ; for c in self . model . get_unsatisfied_constraints ( & ans ) { if let Some ( ph ) = & c . placeholder { if let Some ( point ) = phdict . get_mut ( ph ) { * point += 1 ; size += 1 ; } } if let Some ( label ) = & c . label { constraint_labels . push ( label ) ; } } let is_satisfied = constraint_labels . len ( ) == 0 ; ret = Some ( ( energy + c , SolutionView :: new ( sol . with_local_field ( & model ) , qubit_map . clone ( ) ) , constraint_labels , ) ) ; if is_satisfied { return Ok ( ret . unwrap ( ) ) ; } } } Ok ( ret . unwrap ( ) ) }
Solve the model using internal annealer.
Solve the model using internal annealer .
&self
&self